Hello,
I have a question on how to set up macro parameters.
If I set up my marco as following (just an idea):
%macro m_test(varlst=); /*if input varlst=var1 var2*/ ... y=('ID', &varlst.); /*I want 'var1', 'var2' here*/ by &varlst. /*var1 var2*/ ... %mend;
current code won't work as &varlst.=var1 var2 only. Is it a way to get " 'var1', 'var2'" part done without setting up additional parameters? try to see whether I use minimal parameters to setup most codes and stuck at this situation.
current code won't work as &varlst.=var1 var2 only. Is it a way to get " 'var1', 'var2'" part done without setting up additional parameters? try to see whether I use minimal parameters to setup most codes and stuck at this situation.
Is your actual question "how do I place quotes around the elements of the variable varlist"?
Here's one way placing the quoted values into a new variable to use.
%macro junk(varlist=); %let newlist=; %do i = 1 %to %sysfunc(countw(&varlist.)); %let word=%scan(&varlist.,&i.); %let newlist = &newlist. %sysfunc(quote(&word.)); %end; /* just to show the result*/ %put &newlist.; %mend; %junk(varlist= var1 var2);
But to reiterate, your example "code' with "y= ('ID',&newlist.) " using the new variable with quoted values is still invalid SAS code.
Please explain what this line of code is trying to do.
y=('ID', &varlst.);
Please note that as shown, this line of code is not valid working legal SAS code, and will never work regardless of the value of &VARLST, it will never work either in a macro or outside of a macro. So you not only need to explain what this line of code is trying to do, but you need to do it in a way that is legal valid working SAS code.
@stataq wrote:
Just want to know whether there is a way i can get " 'var1', 'var2' " within my marco setup if I call %m_test(varlst=var1 var2); additional work will be needed.
I asked what this line of code is trying to do. Please reply.
I'm not sure what the purpose of this macro is, but I'm not sure you can use positional parameters when you want a macro variable to be several different variables. Using keyword parameters might work (keyword parameters are not defined with an = sign in the %macro statement). Does this help?
%macro test(varlist);
proc means data=sashelp.class maxdec=2;
var &varlist;
run;
%mend;
%test(age height weight)
current code won't work as &varlst.=var1 var2 only. Is it a way to get " 'var1', 'var2'" part done without setting up additional parameters? try to see whether I use minimal parameters to setup most codes and stuck at this situation.
Is your actual question "how do I place quotes around the elements of the variable varlist"?
Here's one way placing the quoted values into a new variable to use.
%macro junk(varlist=); %let newlist=; %do i = 1 %to %sysfunc(countw(&varlist.)); %let word=%scan(&varlist.,&i.); %let newlist = &newlist. %sysfunc(quote(&word.)); %end; /* just to show the result*/ %put &newlist.; %mend; %junk(varlist= var1 var2);
But to reiterate, your example "code' with "y= ('ID',&newlist.) " using the new variable with quoted values is still invalid SAS code.
@stataq wrote:
Your suggestion solved 50% question. Now we need to find a way to add "," in between. 🤣 A great mind twister. Much appreciated your input and kindness.👍
extremely simple
%let newlist = %sysfunc(catx(',',&newlist., %sysfunc(quote(&word.))));
The catx function places the first parameter between the following values. If there is only one Var in the var list then no comma would be inserted because the first time the loop executes the &newlist is blank and so nothing to insert between. For some reason that calls %sysevalf. Gets the correct result though shows an error in the log.
%do i = 1 %to %sysfunc(countw(&varlist.)); %let word=%scan(&varlist.,&i.); %if &i=1 %then %let newlist=%sysfunc(quote(&word.)); %else %let newlist = &newlist.,%sysfunc(quote(&word.)); %end;
If you can use double quotes ""
try this:
%macro m_test(varlst=); /*if input varlst=var1 var2*/
%local new_varlst;
%let new_varlst = "%sysfunc(tranwrd(&varlst.,%str( ),%str(%", %")))";
%put &=varlst.;
%put &=new_varlst.;
%mend;
%m_test(varlst=a b c d e f g)
If you need single try this:
%macro m_test2(varlst=); /*if input varlst=var1 var2*/
%local new_varlst;
%let new_varlst = %str(%')%qsysfunc(tranwrd(&varlst.,%str( ),%str(%', %')))%str(%');
%put &=varlst.;
%put &=new_varlst.;
%mend;
%m_test2(varlst=a b c d e f g)
But if you would tell us what do you want to do, maybe we could suggest something that is less "scratch left year with right... foot" approach.
Bart
Your answer solved my question 100%. 👍🌹. My intend is to have a setup where I have "var1 var2" and "'var1','var2'" at the same time and only need to setup one macro parameter "varlst=var1 var2". It is a hypothetical situation. I am really appreciate @yabwon and @ballardw 's kindness and willingness to such question. 😀
If you told us upfront that you need it for a hash table it could be even easier, because it doesn't requires quotes and commas at all...
Bart
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: