DATA Step, Macro, Functions and more

How to pass a column from a table as a parameter in a macro

Reply
Occasional Contributor
Posts: 10

How to pass a column from a table as a parameter in a macro

How can I pass a table column as a parameter in a macro?

Example of what I intend to do:

data xyz;

   input userid;

   datalines;

1

2

;

run;

%macro looper (userid);

...

...

%mend looper;

Super User
Super User
Posts: 7,407

Re: How to pass a column from a table as a parameter in a macro

Thats exactly the way.  Describe further what you problems are, test data/required output is also good.

Super User
Posts: 5,085

Re: How to pass a column from a table as a parameter in a macro

Do you want to call %LOOPER just once, or do you want to call it for each observation in XYZ?

Occasional Contributor
Posts: 10

Re: How to pass a column from a table as a parameter in a macro

I want to call % LOOPER for each observation in table XYZ

Super User
Super User
Posts: 7,407

Re: How to pass a column from a table as a parameter in a macro

data _null_;

     set XYZ;

     call execute('%looper ('||strip(user)||');');

run;

However there are probably better ways of doing it, but without the rest of the information its hard to say.

Respected Advisor
Posts: 3,777

Re: How to pass a column from a table as a parameter in a macro

One again I will remind you of the importance of timing with the macro language and the fact that CALL EXECUTE has subtleties that most do NOT understand.

T

The following example uses the %NRSTR macro quoting function to mask the macro statement. This function will delay the execution of macro statements until after a step boundary.

call execute('%nrstr(%sales('||month||'))');

While it may not matter for the programs that you write we have not seen the OP's program and have no idea what the OP will come up with. 

SAS(R) 9.4 Macro Language: Reference, Third Edition

Occasional Contributor
Posts: 10

Re: How to pass a column from a table as a parameter in a macro

Thanks; this will do it.

Super Contributor
Super Contributor
Posts: 3,174

Re: How to pass a column from a table as a parameter in a macro

It would be considerate as well to explain why you want to perform such processing, as much as there may be a more effective / efficient technical approach - specifically what is your objective and how might you intend to use the SAS observation content with each macro invocation?  It appears that LOOPER is intended to be a code-piece executed within the SAS DATA step you have illustrated, although you have the %MACRO %MEND definition coded after the RUN;  statement which will most definitely short-circuit the processing you desire to achieve.

Ask a Question
Discussion stats
  • 7 replies
  • 353 views
  • 3 likes
  • 5 in conversation