I am trying to write a macro to run through a list of variables and calculate descriptive statistics. I have run into a problem, I think with variable scope, that I can't get past, and I would greatly appreciate any help. The problem boils down to this:
%MACRO TEST();
DATA _NULL_;
%LET I = 1;
DO INC=1 TO 3;
%LET I = %EVAL(&I + 1);
PUT "&I";
END;
RUN;
%MEND;
%TEST();
This generates 2, 2, 2 rather than 2, 3, 4.
The full code I am trying to use, if that helps, is this:
%MACRO DATAINFO(DATA, VARS);
DATA _NULL_;
%LET FILENAME = "&SASDATA.MACROS\PRG.SAS";
FILE &FILENAME;
%LET VAR = %SYSFUNC(SCAN(%QUOTE(&VARS), 1));
PUT "PROC SQL;";
PUT "SELECT "; PUT """&VAR"""; PUT " AS VAR,";
PUT "COUNT(&VAR) LABEL='Total of Obs',";
PUT "COUNT(DISTINCT &VAR) LABEL='Unique Values',";
PUT "FROM &DATA";
PUT "GROUP BY VAR";
%LET COUNT = %SYSFUNC(COUNTW(%QUOTE(&VARS)));
IF &COUNT > 1 THEN DO;
%LET I = 1;
DO INC=2 TO &COUNT;
%LET I = %EVAL(&I + 1);
%PUT &I;
%LET VAR = %SYSFUNC(SCAN(%QUOTE(&VARS), &I));
PUT "UNION";
PUT "SELECT "; PUT """&VAR"""; PUT " AS VAR,";
PUT "COUNT(&VAR) LABEL='Total of Obs',";
PUT "COUNT(DISTINCT &VAR) LABEL='Unique Values',";
PUT "FROM &DATA";
PUT "GROUP BY VAR";
END;
END;
PUT ";QUIT;";
RUN;
*%INCLUDE &FILENAME;
%MEND;
/*USAGE*/
/*
%LET INITIAL = AGE HAZARD_GROUP CLASSCOUNT;
%DATAINFO(MODEL.DATA, &INITIAL);
*/
The problem is, whether I try to use I or INC, I can't get a variable to increment in this line:
%LET VAR = %SYSFUNC(SCAN(%QUOTE(&VARS), &I));
If I use INC, it says this is not numeric. If I use I, I can't get it to increment. I would appreciate ANY suggestions.
Thanks,
Chris
Christopher Johnson
www.codeitmagazine.com