BookmarkSubscribeRSS Feed
abubakrayesh
Fluorite | Level 6

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.

4 REPLIES 4
novinosrin
Tourmaline | Level 20

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

 

Reeza
Super User

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.


 

 

PeterClemmensen
Tourmaline | Level 20

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

s_lassen
Meteorite | Level 14

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.

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1890 views
  • 4 likes
  • 5 in conversation