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

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)

 

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

@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

7 REPLIES 7
Reeza
Super User

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

ttxq
Fluorite | Level 6

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

 

Thanks for your replying.

 

 

Reeza
Super User

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

data_null__
Jade | Level 19

@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
ttxq
Fluorite | Level 6
It works. Thank you so much.
ballardw
Super User

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.

ttxq
Fluorite | Level 6
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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 13828 views
  • 3 likes
  • 4 in conversation