BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
stataq
Quartz | Level 8

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

 

View solution in original post

9 REPLIES 9
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
stataq
Quartz | Level 8
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.
PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
svh
Lapis Lazuli | Level 10 svh
Lapis Lazuli | Level 10

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)
ballardw
Super User

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
Quartz | Level 8
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.👍
ballardw
Super User

@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;
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



stataq
Quartz | Level 8

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. 😀

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
  • 9 replies
  • 343 views
  • 4 likes
  • 5 in conversation