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.
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
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.
Do you know the names of all the data sets? And if yes, are these names in a data set?
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.
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
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.
Ready to level-up your skills? Choose your own adventure.