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

Dear ALL:

I want to make a Variable macro that the Input parameters can be changed (here i want to control the default value of every parameter) ,so that the macro can be changed:


I try like this :


%let name=%str(name1=zs,name2=ls,name3=,name4=,name5=hh);
%macro test(&name.);
%put &name1. is a good man;
%put &name2. is a good man;

%put &name3. is a good man;

%put &name4. is a good man;

%put &name5. is a good man;

%mend;


but failed;


ANY suggestion is welcome !Thanks in advance!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You cannot use macro code to generate the %MACRO statement.

You could try use some other method of code  generation to build your macro definition, such as writing code to a file and %INCLUDING it.

 

But it is very likely there is another way to do what you really want to do. So if you can explain that then perhaps someone can help you.

http://xyproblem.info/

View solution in original post

6 REPLIES 6
hashman
Ammonite | Level 13

@duanzongran:

In the %macro statement, the macro processor looks for (a) macro name that is a valid SAS identifier and and (b) parameter name that is a valid SAS identifier. Your "parameter name" starts with &, which is not valid. If you want, as you say, to control the default value of every parameter, just define the macro with:

%macro test (name1=zs, name2=ls, name3=, name4=, name5=hh) ;

and change the parameter values as needed at macro invocations.

 

Kind regards

Paul D.

   

Patrick
Opal | Level 21

Only use below if you really need to pass-in a variable list of parameters. This is rarely required.

 

The %macro parmbuff options allows you to pass in a variable list of parameters.

These parameters then become available in your macro via automatic macro variable &syspbuff.

 

%macro test/parmbuff;
    %put macro contains : &syspbuff;
    %let cnt = %sysfunc(countw(&syspbuff));
    %do i = 1 %to &cnt;
        %put %scan(&syspbuff, &i) is a good man;
    %end;
%mend test;                                                                                                                            
 
%test(zs,ls,hh);

https://go.documentation.sas.com/?docsetId=mcrolref&docsetTarget=p1nypovnwon4uyn159rst8pgzqrl.htm&do...

Tom
Super User Tom
Super User

You cannot use macro code to generate the %MACRO statement.

You could try use some other method of code  generation to build your macro definition, such as writing code to a file and %INCLUDING it.

 

But it is very likely there is another way to do what you really want to do. So if you can explain that then perhaps someone can help you.

http://xyproblem.info/

duanzongran
Obsidian | Level 7

THANK YOU @Tom ;

here i do a further describtion and hope to get  your Valuable advice  ;

 

HERE IS WHAT I HAVE 

I have three macros ,macro3 refers to macro2 and macro2 refers to macro1,

and They share the same input parameters (name1=,name2=,name3=,name4=,name5=);

 

%macro macro1(name1=,name2=,name3=,name4=,name5=);
*codes......;
%mend;


%macro macro2(x1=,x2=,name1=,name2=,name3=,name4=,name5=);
*other codes.......;
%macro1(name1=&name1.,name2=&name2.,name3=&name3.,name4=&name4.,name5=&name5.);
*other codes.......;
%mend;

 

%macro macro3(y1=,y2=,x1=,x2=,name1=,name2=,name3=,name4=,name5=);
*other codes.......;
%macro2(x1=&x1.,x2=&x2.,name1=&name1.,name2=&name2.,name3=&name3.,name4=&name4.,name5=&name5.);
*other codes.......;

%mend;

 

 

HERE IS WHAT I WANT 

CONTROL THE INPUT parameters OF EVERY MACRO BY MACRO VARIABLE ;

the number of  input parameters of macro1 may be changed to  (name1=,name2=,name3=,name4=,name5=,name6=,name7=) or more,just like 

 

%macro macro1(name1=,name2=,name3=,name4=,name5=,name6=,name7=);
*codes......;
%mend;

 

 

and i wish macro2 could be changed as 

 

%macro macro2 (x1=,x2=,name1=,name2=,name3=,name4=,name5=,name6=,name7=);
*other codes.......;
%macro1(name1=&name1.,name2=&name2.,name3=&name3.,name4=&name4.,name5=&name5.,name6=&name6.,name7=&name7);
*other codes.......;
%mend;

 

and i wish macro3  could be changed as 

 

%macro macro3(y1=,y2=,x1=,x2=,name1=,name2=,name3=,name4=,name5=,name6=,name7=);
*other codes.......;
%macro2(x1=&x1.,x2=x2.,name1=&name1.,name2=&name2.,name3=&name3.,name4=&name4.,name5=&name5.,name6=&name6.,name7=&name7);
*other codes.......;
%mend;

 

 

 

 

 

 

 

 

Tom
Super User Tom
Super User

That doesn't make any sense.  When you write the macro you decide the what input it takes.  Why would you want to change that without changing the macro?

 

Is the issue that you want to pass a variable number of VALUES to the macro when your CALL the macro?  If so then just design the macro to accept a variable number of values.  The easiest what to do that is to just use one parameter and pass the values in as a list.  You can use space or | or some other character as the delimiter between the values.  You could  even use comma as the delimiter, but then the call is going to be difficult.

 

%macro macro1(name_list);
%local i n_names name;
%let n_names=%sysfunc(countw(&name_list,%str( )));
%do i=1 %to &n_names;
  %let name=%scan(&name_list,&i,%str( ));
   ...
%end;
%mend;

%macro macro2(x_list,name_list);
...
  %macro1(name_list=&name_list)
...
%mend;

%macro2(x_list=x1_value x2_value,name_list=Name1 Name2 Name3);
duanzongran
Obsidian | Level 7
Thank U ! variable_list is a good method!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 6 replies
  • 855 views
  • 0 likes
  • 4 in conversation