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!
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.
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.
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);
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.
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;
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);
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
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.
Ready to level-up your skills? Choose your own adventure.