BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
podarum
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

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

7 REPLIES 7
Linlin
Lapis Lazuli | Level 10

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)

podarum
Quartz | Level 8

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

podarum
Quartz | Level 8

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

Linlin
Lapis Lazuli | Level 10

You welcome. I am glad it works.

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

it may cause problem.

podarum
Quartz | Level 8

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

Linlin
Lapis Lazuli | Level 10

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)

Cynthia_sas
Diamond | Level 26

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

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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