I want to develop a macro, named %var_hascustfmt , to judge a variable has custom format or not. It is designed to return 1 or 0, for variable has custom format or not.
This is the source code:
%macro var_hascustfmt(data,var);
%local did did2 result;
%let did=%sysfunc(open(&data.));
%if &did.^=0 %then %do;
%let fmtname=%sysfunc(varfmt(&did.,%sysfunc(varnum(&did.,&var.))));
%let did=%sysfunc(close(&did.));
%let result=0;
%if &fmtname.^= %then %do;
%let did2=%sysfunc(open(sashelp.vcformat(where=(trim(fmtname)||'.'="&fmtname."))));
%if %sysfunc(attrn(&did2.,nlobsf))^=0 %then %let result=1;
%let did2=%sysfunc(close(&did2.));
%end;
%end;
&result.
%mend;
Test data:
proc format;
value $gender
'F'='Female'
'M'='Male'
;
run;
data have;
subject=1;
gender='F';
birthdate='01jan2000'd;
weight=59;
format subject z3. gender $gender. birthdate e8601da.;
run;
Test code and result:
%put %var_hascustfmt(have,subject); 0
%put %var_hascustfmt(have,gender); 1
%put %var_hascustfmt(have,birthdate); 0
%put %var_hascustfmt(have,weight); 0
So far, it works good. As you can see, the macro return value by query name of variable's format in sashelp.vcformat, which is a view of dictionary.formats.
My question is:
1. Is this way always reliable? What does sashelp.vcformt stores in official definition?
2. Is there any other implements? Access to dictionary table is not always fast enough.
... View more