Pages

Friday, July 26, 2013

Widgets

How to check for the existence of a variable in SAS?

If you want to check if a variable exists in a dataset or not, you can simply use the below macros which uses open and varnum function, to open a dataset   and check for the presence. Varnum function returns the variable's position in a dataset. So it its vale is greater than 0 then the variable is present.


Below is the macro:

*************************************************************
%macro VarExist(ds,var);
 %local rc dsid result;
 %let dsid=%sysfunc(open(&ds));
 %if %sysfunc(varnum(&dsid,&var)) > 0 %then %do;
  %let result=1;
  %put NOTE: Var &var exists in &ds;
 %end;
 %else %do;
  %let result=0;
  %put NOTE: Var &var not exists in &ds;
 %end;
 %let rc=%sysfunc(close(&dsid));
 &result
%mend VarExist;
*************************************************************

No comments:

Post a Comment