DATA Step, Macro, Functions and more

Dataset observations as macro parameters

Reply
Contributor
Posts: 40

Dataset observations as macro parameters

Hi all,

I have a simple macro

%macro m1(parm1,parm2,parm3);

Process report data = have;

Where id=&parm1 and phone=&parm2 and name="&parm3";

Run;

%mend;

Data have;

Input Id 8. Phone 10. Name $30.;

Cards;

1 1234567890 Rajesh

2 9876543210 Mohan

;

Run;

Now I want to pass values of these observations as parameters to macro.. So my macro calls go like this

%m1(1,1234567890,"Rajesh");

%m1(2,9876543210,"Mohan");

But this is not efficient...Please help

Super User
Posts: 17,837

Re: Dataset observations as macro parameters

Use CALL EXECUTE

Example #2 is your situation.

SAS(R) 9.2 Macro Language: Reference

Trusted Advisor
Posts: 1,130

Re: Dataset observations as macro parameters

I agree with Reeza about using the call execute. Alternatively in similar lines we could use the macro variables.

create macro variables with existing data

proc sql;

select catx(',',id,phone,name) into : par1 - Smiley Tonguear&sysmaxlong from have;

quit;

%put &par1 &par2. ;

The &apar1 and &par2 will resolve to the data in respective observation separated by comma.

After this create a new macro test within this macro you could call the previous macro as below.

%macro test;

%do  i = 1 %to 2;

%m1(&&par&i.);

%end;

%mend;

%test;

Please try and let em know if it helps.

Thanks,

Jag

Thanks,
Jag
Contributor
Posts: 65

Re: Dataset observations as macro parameters

If you want to generate a report for each and every observation then try this

Data have;

Input Id Phone Name $;

format phone best10.;

call execute("proc report data = have;where id = "||strip(id)||"and phone = "||strip(phone)||"and name = '"||strip(name)||"';run;");

Cards;

1 1234567890 Rajesh

2 9876543210 Mohan

;

Super User
Super User
Posts: 7,401

Re: Dataset observations as macro parameters

As with the above posters, I would also suggest to use call execute.  I would also point out that you can do by group printing as well (and if you want seperate sheets there is an option in excelxp.tagsets):

proc report data=....;

     columns ...;

     by var1 var2;

     title "#byvar1 #byvar2";

     define ...;

run;

Regular Contributor
Posts: 198

Re: Dataset observations as macro parameters

Here is a tool I wrote recently to accomplish your task

http://www.sascommunity.org/wiki/Macro_CallMacr

Ask a Question
Discussion stats
  • 5 replies
  • 273 views
  • 0 likes
  • 6 in conversation