A few issues with your code, the main one being that it only detects custom formats that have been defined.
This works:
%macro var_hascustfmt(ds, var);
%local dsid result;
%let dsid=%sysfunc(open(&ds.));
%if &dsid. %then %do;
%let fmtname=%sysfunc(varfmt(&dsid.,%sysfunc(varnum(&dsid.,&var.)))); %* Get format name from source table;
%let fmtname=%sysfunc(prxchange(s/[\d\.]*$//, 1, &fmtname.)); %* Remove format length & final dot ;
%let dsid =%sysfunc(close(&dsid.)); %* Close source table ;
%if %length(&fmtname.) %then %do; %* Var format found, vet it ;
%let dsid =%sysfunc(open(SASHELP.VFORMAT(where=(FMTNAME="&fmtname" & SOURCE in ('U','B'))))); %* See if format is standard ;
%let result=%sysfunc(ifc(%sysfunc(attrn(&dsid.,nlobsf)), 0, 1)); %* Not standard => set flag to 1 ;
%let dsid =%sysfunc(close(&dsid.)); %* Close format table ;
%end; %* End of 'var format found' ;
%else %let result=0; %* No format => set flag to 0 ;
%end; %* End of format search ;
%else %let result=-1; %* No data set => set flag to -1 ;
&result.
%mend;
proc format;
value $gender
'F'='Female'
'M'='Male'
;
run;
data HAVE;
SUBJECT =1;
GENDER ='F';
BIRTHDATE='01jan2000'd;
WEIGHT =59;
format SUBJECT z13.2 GENDER $gender. BIRTHDATE e8601da. AGE sssss.;
run;
%put %var_hascustfmt(HAVE,SUBJECT); %* z13.2 ;
%put %var_hascustfmt(HAVE,GENDER); %* Custom ;
%put %var_hascustfmt(HAVE,BIRTHDATE); %* e8601da. ;
%put %var_hascustfmt(HAVE,WEIGHT); %* Unformatted ;
%put %var_hascustfmt(HAVE,AGE); %* Undefined ;
%put %var_hascustfmt(HAVEXX,WEIGHT); %* No data set ;
This should be further improved by checking that the variable exists.
... View more