I need a way to dynamically return the number of variables in the current data step.
Using SAS NOTE 24671: Dynamically determining the number of observations and variables in a SAS data set, I have come up with the following macro.
%macro GetVarCount(dataset);
/* Open assigns ID to open data set. Assigns 0 if DNE */
%let exists = %sysfunc(open(&dataset));
%if &exists %then
%do;
%let returnValue = %sysfunc(attrn(&exists, nvars));
%let closed = %sysfunc(close(&exists));
%end;
/* Output error if no dataset */
%else %put %sysfunc(sysmsg());
&returnValue
%mend;
Unfortunately, this errors out on an initial pass of a data set since the data set has not yet been created. After the first pass, and a dataset with 0 observations has been created, the macro can access the table and the number of variables.
For instance,
data example;
input x y;
put "NOTE: [DEV] There are %GetVarCount(example) variables in the EXAMPLE data set.";
datalines;
1
2
;
run;
The first run produces:
ERROR: File WORK.EXAMPLE.DATA does not exist.
WARNING: Apparent symbolic reference RETURNVALUE not resolved.
NOTE: [DEV] There are &returnValue variables in the EXAMPLE data set.
The second run produces:
NOTE: [DEV] There are 2 variables in the EXAMPLE data set.
Is there a way to get the number of variables in a data set first time the data step is run?