Help using Base SAS procedures

macros in proc/data steps

Accepted Solution Solved
Reply
Super Contributor
Posts: 401
Accepted Solution

macros in proc/data steps

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


Accepted Solutions
Solution
‎12-15-2011 10:08 AM
Super Contributor
Posts: 1,636

Re: macros in proc/data steps

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)

View solution in original post


All Replies
Solution
‎12-15-2011 10:08 AM
Super Contributor
Posts: 1,636

Re: macros in proc/data steps

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)

Super Contributor
Posts: 401

Re: macros in proc/data steps

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);

Super Contributor
Posts: 401

Re: macros in proc/data steps

Thanks linlin, it actually did work.. i was having problem with my program.. thanks

Super Contributor
Posts: 1,636

Re: macros in proc/data steps

You welcome. I am glad it works.

don't add ";" to "%test (Data.A_B,Data.A_B2)"

it may cause problem.

Super Contributor
Posts: 401

Re: macros in proc/data steps

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

Super Contributor
Posts: 1,636

Re: macros in proc/data steps

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)

SAS Super FREQ
Posts: 8,864

macros in proc/data steps

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

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 203 views
  • 0 likes
  • 3 in conversation