I'm so not good with macros, but trying to learn..
Would this be possible? I have 5 different tables that I have to run throuhg first a proc transpose then a data step and lastly in a proc expand....long codes. I guess I can copy the code 5 times and insert each table name, but if instead I can use macors, that would be much better. Oh and one more thing the new output from each step to also be 'macroed' so I don't have to type the new table name in... so the output from the proc transpose, to be somehow picked up by the second data step ? I know I'm probably asking too much.. Thanks
Yes, you can.
%macro test(indsn, outdsn);
/* put all your sas code here, then replace your input dataset with &indsn, output dateset with &outdsn */
%mend test;
/*apply your macro */
%test(in1,out1)
....
%test(in5,out5)
/* example */
%macro test(indsn,outdsn);
data &outdsn;
set sashelp.&indsn;
proc print data=&syslast (obs=5);
run;
%mend;
%test(class,out1)
Yes, you can.
%macro test(indsn, outdsn);
/* put all your sas code here, then replace your input dataset with &indsn, output dateset with &outdsn */
%mend test;
/*apply your macro */
%test(in1,out1)
....
%test(in5,out5)
/* example */
%macro test(indsn,outdsn);
data &outdsn;
set sashelp.&indsn;
proc print data=&syslast (obs=5);
run;
%mend;
%test(class,out1)
Something like this ? it didn't work..
%macro test (indsn, outdsn);
proc transpose data=&indsn
OUT=&outdsn (drop=_NAME_ rename=(F1=CMA_NAME _label_=Date col1=HAM));
var Jan_1985--Jul_2011;
by F1 notsorted;
run;
%mend test;
%test (Data.A_B,Data.A_B2);
Thanks linlin, it actually did work.. i was having problem with my program.. thanks
You welcome. I am glad it works.
don't add ";" to "%test (Data.A_B,Data.A_B2)"
it may cause problem.
To add to this, how could I make sure that each of my new 5 tables, get a new varaible inserted? For example out1 would have NewVar = "Red' and for out2 would have NewVar="Blue"... thanks
you can add another macro variable;
%macro test(indsn, outdsn,nvar);
/* put all your sas code here, then replace your input dataset with &indsn, output dateset with &outdsn
replace newvar="Blue" with newvar="&nvar" */
%mend test;
%test (Data.A_B,Data.A_B2,Blue)
Hi:
This is a very good example of using SAS Macro processing. While this paper won't answer all your specific questions, I believe it has enough good examples to get you what you want.
http://www2.sas.com/proceedings/sugi28/056-28.pdf
The key to success is to start with a working SAS program. Then print it out and grab a yellow highlighter and highlight all the code snippets like dataset names that you do not want to type. Those highlighted places now become the places where you might use a macro variable to substitute for the hard-coded name. Then go on from there, to introduce conditional logic if you need it and make a macro program, if you need it. The above paper takes you from the very beginning to having a complete macro program with conditional processing. Reading that, plus looking at some other papers will help you a lot.
cynthia
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.