Help using Base SAS procedures

How to run the same code for different data sets?

Reply
New Contributor
Posts: 4

How to run the same code for different data sets?

I have around 50 data sets with same variables. I have to add, drop and rename some variables in each of these data sets. Can I run a macro to run the code simultaneously on all files? Or is there any other way?

PS I am pretty new to SAS; please try to explain in detail.

PROC Star
Posts: 1,817

Re: How to run the same code for different data sets?

Posted in reply to abubakrayesh

You should learn to call- call execute after having your dataset names list in a variable. To learn how to call call execute, google it. I don't know how many would have the time or will to explain in detail Anyway good luck

 

Super User
Posts: 23,754

Re: How to run the same code for different data sets?

[ Edited ]
Posted in reply to abubakrayesh

First rule - get it working for one data set first, with no bugs. Then try and change that for another data set and note all the things you have to change. Then you can work on automating it. 

 

A walk through of macros is here

https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/

 

If you need help, I suggest posting your working code, highlighting what you need to be dynamic, which you identified from the first step and we can help with the automation.

 


abubakrayesh wrote:

I have around 50 data sets with same variables. I have to add, drop and rename some variables in each of these data sets. Can I run a macro to run the code simultaneously on all files? Or is there any other way?

PS I am pretty new to SAS; please try to explain in detail.


 

 

PROC Star
Posts: 1,283

Re: How to run the same code for different data sets?

Posted in reply to abubakrayesh

Do you know the names of all the data sets? And if yes, are these names in a data set? 

PROC Star
Posts: 266

Re: How to run the same code for different data sets?

Posted in reply to abubakrayesh

If you have the program for one dataset, I would suggest trying this: Write another SAS program, which writes the first program, and put the variable parameters in datastep variables, e.g.:

filename tempsas temp; /* you can also allocate a permanent file here */
data _null_;
  input dsname $;
  file tempsas;
  put 
    'data outlib.' dsname ';'  /
    '  set inlib.' dsname '(keep=a b c d f);' /
    '  where c<33;' /
    'run;' /
    ;
cards;
mydata
yourdata
hisdata
herdata
ourdata
;run;

You can then take a look at the code that is generated (open the TEMPSAS file in an editor window). If it looks OK, try submitting one code section (one datastep in the example), and see if the results are what you expected. If it looks OK, submit some more and check the log and the output. If something goes wrong, go back and change the original program (the one that wrote the code) and submit it again.

When all looks OK, you can insert the line "%include tempsas;" at the bottom of your original program.

A lot of people want to use macros and call execute. That may work too, but you have a lot less control of the process, and it is much harder to debug.

 

Ask a Question
Discussion stats
  • 4 replies
  • 193 views
  • 4 likes
  • 5 in conversation