Jan 24, 2013

Programatically determine if a variable exists in a data set

Use the OPEN and VARNUM functions to determine if a variable exists in a SAS data set. The OPEN function opens a SAS data set and returns a data set identifier. The VARNUM function returns the variable’s position in the data set. If the variable does not exist then VARNUM returns a 0.
/* Create sample data */
 
data test;
  input fruit $ count;
datalines;
apple 12
banana 4
coconut 5
date 7
eggs 9
fig 5
;
 
/* Specify the data set to open in the OPEN function.  Specify the  */
/* variable to be located in the second parameter of the VARNUM     */
/* function. If the variable does not exist, the value of check     */
/* will be zero.                                                    */
 
%let dsid = %sysfunc(open(test));
%let chk = %sysfunc(varnum(&dsid, count));
%let rc = %sysfunc(close(&dsid));
%put &chk;
There is another Method with a datastep explained at SAS’s website ( http://support.sas.com/kb/26/003.html)