Your SAS programs, embedded in web apps and elsewhere

Passing a Comma Delimited Variable to a Macro

Accepted Solution Solved
Reply
Super Contributor
Posts: 497
Accepted Solution

Passing a Comma Delimited Variable to a Macro

How can I pass a macro variable that has commas into a macro?  I tried using %quote and oddly it does not work.

%Global degStudLev;


%Macro convertCommaListToSpaced(commaList, spacedList =);

           

%Mend;

%let degStudLevComma = D1,D2,D3;

%convertCommaListToSpaced(%quote(&degStudLevComma), degStudLev);


Accepted Solutions
Solution
‎06-24-2015 11:42 AM
Respected Advisor
Posts: 3,799

Re: Passing a Comma Delimited Variable to a Macro

Posted in reply to DavidPhillips2

Another way to mask a comma in a macro parameter value is to simply use parenthesis around the value.  The problem is that the parenthesis become part of the value.

Since you have the value in symbol X another way would be to pass the symbol name and let the macro resolve it using the proper "protection".

%macro test(value, var2 =);

  
%let value=&&&value;
     %put &value;
%mend;
%let variableToBeUpdated =;
%let x=a,b,c;
%test(x,var2=variableToBeUpdated)

23         %macro test(value, var2 =);
24        
25            %let value=&&&value;
26              %put &value;
27         %mend;
28         %let variableToBeUpdated =;
29         %let x=a,b,c;
30         %test(x,var2=variableToBeUpdated)
a,b,c

%macro test(value, var2 =);
    
%put &value;
%mend;
%let variableToBeUpdated =;
%let x=a,b,c;
%test((&x),var2=variableToBeUpdated)

23         %macro test(value, var2 =);
24              %put &value;
25         %mend;
26         %let variableToBeUpdated =;
27         %let x=a,b,c;
28         %test((&x),var2=variableToBeUpdated)
(a,b,c)



View solution in original post


All Replies
Super Contributor
Posts: 497

Re: Passing a Comma Delimited Variable to a Macro

Posted in reply to DavidPhillips2

I found this.  Bquote should work.  Not sure how to unquote this when using bquote.

http://support.sas.com/kb/31/012.html

/*example 1*/

   %macro test(value);

     %put &value;

   %mend;

   %test(%str(a,b,c))

In example 2, an execution-time function is needed. %BQUOTE can be used to mask the commas in the resolved value of the macro variable X.

/*example 2*/

   %macro test(value);

     %put &value;

   %mend;

   %let x=a,b,c;

   %test(%bquote(&x))

Super Contributor
Posts: 497

Re: Passing a Comma Delimited Variable to a Macro

Posted in reply to DavidPhillips2

When I try to alter a variable by reference it breaks.  "more positional parameters found than defined."

%macro test(value, var2 =);

     %put &value;

%mend;

%let variableToBeUpdated =;

%let x=a,b,c;

%test(%bquote(&x), variableToBeUpdated)

Super User
Posts: 11,343

Re: Passing a Comma Delimited Variable to a Macro

Posted in reply to DavidPhillips2

The second variable must be referenced as var2=variableToBeUpdated since you defined it as a keyword parameter not positional.

%test(%bquote(&x), var2=variableToBeUpdated)

Solution
‎06-24-2015 11:42 AM
Respected Advisor
Posts: 3,799

Re: Passing a Comma Delimited Variable to a Macro

Posted in reply to DavidPhillips2

Another way to mask a comma in a macro parameter value is to simply use parenthesis around the value.  The problem is that the parenthesis become part of the value.

Since you have the value in symbol X another way would be to pass the symbol name and let the macro resolve it using the proper "protection".

%macro test(value, var2 =);

  
%let value=&&&value;
     %put &value;
%mend;
%let variableToBeUpdated =;
%let x=a,b,c;
%test(x,var2=variableToBeUpdated)

23         %macro test(value, var2 =);
24        
25            %let value=&&&value;
26              %put &value;
27         %mend;
28         %let variableToBeUpdated =;
29         %let x=a,b,c;
30         %test(x,var2=variableToBeUpdated)
a,b,c

%macro test(value, var2 =);
    
%put &value;
%mend;
%let variableToBeUpdated =;
%let x=a,b,c;
%test((&x),var2=variableToBeUpdated)

23         %macro test(value, var2 =);
24              %put &value;
25         %mend;
26         %let variableToBeUpdated =;
27         %let x=a,b,c;
28         %test((&x),var2=variableToBeUpdated)
(a,b,c)



Super User
Super User
Posts: 7,977

Re: Passing a Comma Delimited Variable to a Macro

Posted in reply to DavidPhillips2

Got to say, I really don't see why you would want to do this.  There are numerous other alternatives - keeping parameters in a dataset for instance, or %sysfunc(tranwrd()).  Also, if you have a list of comma separated data and want a list of space why not use parmbuff?

%macro test/parmbuff;

   %let num=1;

   %let param=%scan(&syspbuff,&num);

   %do %while(&param. ne);

     %put &param.;

     %let num=%eval(&num. + 1);

     %let param=%scan(&syspbuff,&num);

   %end;

%mend test;

%let x=a,b,c;

%test (&x.);

PROC Star
Posts: 1,322

Re: Passing a Comma Delimited Variable to a Macro

Posted in reply to DavidPhillips2

Agree with what you found, %BQUOTE() should work.  %QUOTE() is old and I believe deprecated.  %SUPERQ() is another alternative, if you want to mask & or %.

In theory, the macro processor should unquote values for you automatically before returning them to SAS.  But sometimes it doesn't work, and it is necessary to unquote with %UNQUOTE().  The rule I learned was: if the SAS code in the MPRINT log looks correct but you still get an error message, try %UNQUOTE().

Super Contributor
Posts: 497

Re: Passing a Comma Delimited Variable to a Macro

After passing in variableToBeUpdated to be used as var2;  I learned that I can set variableToBeUpdated in the macro by using

%let &var2 = the cat ran;

How can I set something like

%let &var2 = the cat ran;

%let &var2 = &var2 and jumped;


I found that using the &&& worked


%Global variableToBeUpdated;

%macro test(value, var2 =);

     %let &var2 = &&&var2 Test Part2;

%mend;

%let variableToBeUpdated = Test Part 1;

%let x=a,b,c;

%test((&x), var2 =variableToBeUpdated);

%macro displayStuff();

  %put &variableToBeUpdated;

%mend;

%displayStuff();

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 4657 views
  • 0 likes
  • 5 in conversation