BookmarkSubscribeRSS Feed
mamin088
Fluorite | Level 6

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!

10 REPLIES 10
PaigeMiller
Diamond | Level 26

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 */
--
Paige Miller
mamin088
Fluorite | Level 6
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?
PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
Tom
Super User Tom
Super User

Why not just run it once for all 20 datasets?

data XX1;
  set dataset2 ds2 dataset3 ;
PaigeMiller
Diamond | Level 26

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;
--
Paige Miller
mamin088
Fluorite | Level 6
I did that once before for another study and it was very time-consuming. Also, it is subject to higher errors!
ballardw
Super User

@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.

mamin088
Fluorite | Level 6
Not really! mostly because of typing and repeating! Besides, if you need to change one thing in the statement you will have to change it for all dataset!
mamin088
Fluorite | Level 6
Thank you so much!
Reeza
Super User

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 2256 views
  • 4 likes
  • 5 in conversation