Hello fellow SAS users,
I have a question that might be pretty simply, but I can't find out how to do it.
I have a lot of data sets, one for every year. Let's say they are called:
data1990, data1991, data1992,...,data2019, data2020
I want to perform the same data step on each of these without writing out 30 data steps.
I am thinking something like:
%macro mymacro(year);
data new_data_&year.;
set data_&year.;
SomedatastepthatIneed;
run;
%do i=1990 %to 2020 %by 1;
%mend;
%mymacro(i);
But this does not work. Can someone help me perform a macro that works?
I would combine all datasets to one (assuming that they have the same structure), add a variable containing the year and process all in one step. But maybe that is not possible, so try something like:
%macro mymacro(year);
data new_data_&year.;
set data_&year.;
SomedatastepthatIneed;
run;
%mend;
%macro looper;
%local i;
%do i=1990 %to 2020;
%mymacro(&i);
%end;
%mend;
%looper;
I would combine all datasets to one (assuming that they have the same structure), add a variable containing the year and process all in one step. But maybe that is not possible, so try something like:
%macro mymacro(year);
data new_data_&year.;
set data_&year.;
SomedatastepthatIneed;
run;
%mend;
%macro looper;
%local i;
%do i=1990 %to 2020;
%mymacro(&i);
%end;
%mend;
%looper;
Thank you very much. When I plug your code in to my program however, it does not actually run anything and give me data sets - it merely shows me some logs.
Do you have any idea how that can be?
Posting the log as text could help, please use the "insert code" function.
@niki0209 wrote:
Woops, it works now. I just restarted SAS EG and ran the program again. No clue what was wrong. Thank you so much! 🙂
There are a number of fairly common mistakes people make when learning the macro language that can render a SAS session unstable. Sometimes they are unmatched quotes or parentheses/brackets/braces or missing %end or %mend.
What happens is the macro processor is still looking for something to "complete" an action/compilation.
In a small number of cases if you realize what you left out sometimes you can submit the proper missing code but often the practical solution is 1) save your work and 2) restart SAS.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.