Hello, I'm don't know SAS Macro and appreciate if you help me with the below questions.
I have 20 datasets with the same set of variables. I need to run a set of SAS statements for each data set. Here are an example of each statement:
dataset1......dataset20
1- recode and define the variables
data XX1;
set dataet1;
if var1 in ('A', 'B', 'C') then Var1=1; else Var1=0;
if var2 in ('D', 'E', 'F') then Var2=1; else Var2=0;
if Var3 .....
run;
2- estimate freq or mean..... of a set of variables for each data set:
proc freq data=XX1; tables var1 var2 var3 ; run;
Thank you so much!
Here's one way
%macro dothis(dsnames);
%do i=1 %to %sysfunc(countw(&dsnames));
%let thisname=%scan(&dsnames,&i,%str( ));
data xx&i;
set &thisname;
...
run;
proc freq data=xx&i;
tables var var2 var3;
run;
%end;
%mend;
%dothis(dataset1 dataset2 dataset3) /* Add as many data set names inside the parenthesis as needed */
@mamin088 wrote:
Thank you so much for the quick reply! As I said I'm very new in SAS macro, so would you please walk me through the code a little bit?
First try the non-macro code I posted in message 4 of this thread.
Why not just run it once for all 20 datasets?
data XX1;
set dataset2 ds2 dataset3 ;
A non-macro solution
data all;
set dataset1 dataset2 dataset3 /* Add as many datasets here as needed */
indsname=dsname;
whichdsn=dsname;
run;
proc freq data=all;
by whichdsn;
table var1 var2 var3;
run;
@mamin088 wrote:
I did that once before for another study and it was very time-consuming. Also, it is subject to higher errors!
Errors perhaps of different variable types because attention was not paid when reading the variables to begin with?
Proc IMPORT is not your friend when reading multiple files of similar structure that should have the same variables and properties for the variables. It makes a different set of guesses for each an every file imported.
Here's a short tutorial I wrote that illustrates how to turn a program into a macro, step by step:
https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md
And here's a tutorial from UCLA that will help:
https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/
@mamin088 wrote:
Hello, I'm don't know SAS Macro and appreciate if you help me with the below questions.
I have 20 datasets with the same set of variables. I need to run a set of SAS statements for each data set. Here are an example of each statement:
dataset1......dataset20
1- recode and define the variables
data XX1;
set dataet1;
if var1 in ('A', 'B', 'C') then Var1=1; else Var1=0;
if var2 in ('D', 'E', 'F') then Var2=1; else Var2=0;
if Var3 .....
run;
2- estimate freq or mean..... of a set of variables for each data set:
proc freq data=XX1; tables var1 var2 var3 ; run;
Thank you so much!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.