Not sure why you need so many macro variables. Personally I do not like the idea of macro "arrays" as I find that most programs are easier to maintain if you use a different coding pattern. The problem with using a macro to generate macro variables that you want to use later is that by default the macro variables are defined in the scope of the sub-macro and so disappear when it ends. One way to deal with this is to force the macros to be made in the GLOBAL macro scope as others have posted above. This can cause trouble with other parts of your code that might want to use those same macro variable names for some other meaning. Here are two methods to allow you define the new macros in the current macro scope (not the scope of the sub macro and not necessarily the GLOBAL scope either). 1) Is to take advantage of a feature of the fact that if a sub macro does not have any macro variables at all then it will not generate a new macro scope. Note that this means that the macro cannot take any parameters either. So your number of months will have to placed into another macro variable first before calling the new macro. %macro lookback; data _null_; do i=1 to input(symget('months'),comma32.); call symputx(cats('date_',i),putn(intnx('month',today(),-1*i,'e'),'yymmddn8.')); end; run; %mend lookback ; %let months=6; %lookback; 2) Another method is to have the macro return the text of the %LET statements (without actually running) them as quoted macro text. And then use %UNQUOTE() to execute the %LET statements in the calling environment. This will let you define the macro with parameters and local macro variables. %macro lookback(months,start=); %local i str; %if ^%length(&start) %then %let start=%sysfunc(today()); %do i=1 %to &months ; %let str=&str.%nrstr(%let )date_&i=%sysfunc(intnx(month,&start,-&i,e),yymmddn8.)%nrstr(;); %end; &str. %mend; %unquote(%lookback(6));
... View more