Help using Base SAS procedures

how to pass a parameter with macro variables into macro

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

how to pass a parameter with macro variables into macro

Hi all,

I got a problem. I want to create a sas macro and the parameters are also macro variables, so how can I pass these macro variables into macro?

Thanks.

for example:

%let a=c1,c2,c3,c4,c5;

%let b=d1,d2,d3,d4,d5;

%macro input(data1,data2);

  ... &data1;(I want to use a here)

  ... &data2;(I want to use b here)

%mend;

%input(?,?); (what should I fill in here)

 


Accepted Solutions
Solution
‎02-08-2017 09:15 AM
Respected Advisor
Posts: 3,799

Re: how to pass a parameter with macro variables into macro


ttxq wrote:

yes, I want to know how to pass them into the Macro.

 

Thanks for your replying.

 

 


Pass the names not the value.  Then you don't have to worry about quoting in the macro call.  Also you can't name a macro %INPUT.

 

26         %let a=c1,c2,c3,c4,c5;
27         %let b=d1,d2,d3,d4,d5;
28         %macro xinput(data1,data2);
29            %put NOTE: &&&data1 &&&data2;
30            %mend;
31         %xinput(a,b);
NOTE: c1,c2,c3,c4,c5 d1,d2,d3,d4,d5

View solution in original post


All Replies
Super User
Posts: 19,804

Re: how to pass a parameter with macro variables into macro

Do you need to pass them in? They exist and you can use them in the macro as shown.

Occasional Contributor
Posts: 10

Re: how to pass a parameter with macro variables into macro

yes, I want to know how to pass them into the Macro.

 

Thanks for your replying.

 

 

Super User
Posts: 19,804

Re: how to pass a parameter with macro variables into macro

[ Edited ]

Ok, add two extra parameters to your macro and then pass the two variable lists.

 

You'll need to mask the commas or use a different separator. I think using a different separator is easier but if you want comma's for some reason, you can mask them using the techniques here:

http://support.sas.com/kb/31/012.html

Solution
‎02-08-2017 09:15 AM
Respected Advisor
Posts: 3,799

Re: how to pass a parameter with macro variables into macro


ttxq wrote:

yes, I want to know how to pass them into the Macro.

 

Thanks for your replying.

 

 


Pass the names not the value.  Then you don't have to worry about quoting in the macro call.  Also you can't name a macro %INPUT.

 

26         %let a=c1,c2,c3,c4,c5;
27         %let b=d1,d2,d3,d4,d5;
28         %macro xinput(data1,data2);
29            %put NOTE: &&&data1 &&&data2;
30            %mend;
31         %xinput(a,b);
NOTE: c1,c2,c3,c4,c5 d1,d2,d3,d4,d5
Occasional Contributor
Posts: 10

Re: how to pass a parameter with macro variables into macro

Posted in reply to data_null__
It works. Thank you so much.
Super User
Posts: 11,343

Re: how to pass a parameter with macro variables into macro

Placing commas inside a variable is often a poor idea. If you intend to use it as a macro parameter it will fail because the macro variable resolves with the commas and then each value after a comma is treated as a another parameter. Generally this results in the number of parameters passed not matching the macro definition.

%let a=c1,c2,c3,c4,c5;

 

What kind of macro are you building that requires the commas?

 

And this may help

%let a = c1 c2 c3 c4;
%let ab = %sysfunc(translate(&a,',',' '));

%put &ab;

if you place exactly one space between elements.

 

Or learn to use the macro quoting functions.

Occasional Contributor
Posts: 10

Re: how to pass a parameter with macro variables into macro

I create the macro that is used in proc sql select statement. so I need the comma. I'll try your method. Thank you for replying.
☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 476 views
  • 0 likes
  • 4 in conversation