Reputation: 243
I've been struggling with this problem for a while now. I have written a macro lets call it macro1
and I want to run it on a list of years lets say 1989 1990 1995
using a do
or %do
loop in open code but I cannot get it to work. I have been trying things along the lines of
%let years = 1989 1990 1995
%local i next_year
%do i=1 %to %sysfunc(%countw(&years));
next_year = %scan(&years, &i);
%macro1(&next_year);
%end
to no avail. Does anyone have a convenient solution to this problem? It seems like something that another programming language would be able to do in 3 lines (I have to use SAS though as its the only available language). Any help would be great.
Upvotes: 0
Views: 219
Reputation: 51581
Your code could work inside a macro definition if you include the required %LET
needed to create a macro variable. And terminate your statements with the required semi-colons. And also leave off the semi-colons you don't need.
%macro test(years);
%local i next_year;
%do i=1 %to %sysfunc(%countw(&years));
%let next_year = %scan(&years, &i);
%macro1(&next_year)
%end;
%mend test;
%test(1989 1990 1995)
You could also eliminate the unneeded NEXT_YEAR variable completely.
%macro1(%scan(&years, &i))
Really you should just avoid using macro coding. Stick to learning how to write actual SAS code first. Wait to learn how to use macros to generate code until you know what code you want to generate. Complex DO loops are much easier to create using the real language of a data step instead of trying to code in a macro processor.
data _null_;
do year=1989, 1990, 1995 ;
call execute(cats('%nrstr(%macro1)(',year,')'));
end;
run;
But sometimes wallpaper code is easier than over complicating things. Here is the three line program.
%macro1(1989)
%macro1(1990)
%macro1(1995)
Upvotes: 2