DATA Step, Macro, Functions and more

Unquoting a Macro variable in a Macro

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

Unquoting a Macro variable in a Macro

[ Edited ]

Hello, I'm trying to enter in a macro with a variable that normally has commas and trying to come up with a macro that can use it.

 

Example:

 

 

data subjects;
	do subject_id = 1 to 8;
		output;
	end;
run;
proc surveyselect data=subjects groups=(2,2,2,2) reps=3 noprint out=randomizer(rename=groupId=reader); run;

 

Here, I want to create a macro variable that contains 2,2,2,2

Here is what I came up with after discovering %unquote

 

%macro m(var);
	%let var=&var;

			proc surveyselect data=subjects groups=(%unquote(&var)) reps=3 noprint
			    out=randomizer(rename=groupId=reader); 
			run;
%mend m;

%m("2,2,2,2")

Am I using %unquote incorrectly or is there another way to perform this?


Accepted Solutions
Solution
‎01-19-2018 02:32 PM
PROC Star
Posts: 1,555

Re: Unquoting a Macro variable in a Macro

Posted in reply to Learning_S

still the same thing:

 

%macro m(var);
	%let var=&var;

			proc surveyselect data=subjects groups=(&var) reps=3 noprint
			    out=randomizer(rename=groupId=reader); 
			run;
%mend m;%m(%str(2,2,2,2))

View solution in original post


All Replies
PROC Star
Posts: 1,555

Re: Unquoting a Macro variable in a Macro

[ Edited ]
Posted in reply to Learning_S

I would think of using keyword parameter as opposed to positional to make it easy

 

%macro m(var=%str(2,2,2,2));
	%let var=&var;/* don;t need this since you are defining var as macro parameter in macro stmt*/

			proc surveyselect data=subjects groups=(&var) reps=3 noprint
			    out=randomizer(rename=groupId=reader); 
			run;
%mend m;%m

 

Occasional Contributor
Posts: 12

Re: Unquoting a Macro variable in a Macro

Posted in reply to novinosrin

Thanks for the reply. My goal is to make it generalizable hence my desire to use it directly into the function. Would you know if that would be possible?

PROC Star
Posts: 1,555

Re: Unquoting a Macro variable in a Macro

Posted in reply to Learning_S

Learning_S wrote:

Thanks for the reply. My goal is to make it generalizable hence my desire to use it directly into the function. Would you know if that would be possible?


I am not quite sure what you mean by generalizable  in your example. My thought was basically to avoid using so called manual unquote and let the macro processor unquote automatically as the text is passed to the compiler for execution. 

 

 why do you need to have quotations here "2,2,2,2"

 

 

 

Occasional Contributor
Posts: 12

Re: Unquoting a Macro variable in a Macro

Posted in reply to novinosrin

My goal is to be able to call the macro and use it solely with %m([numbers and commas]); where %m(2,2,2,2); was an example of that.

 

"2,2,2,2" doesn't necessarily need to have quotes. I included them to my understanding of the %unquote() function and to have it written into the groups() option.

 

I would like to have the ability to put in different combinations of numbers into the % m(); macro

 

e.g. 2,1,2  or  4,4,3,2,3,2

Solution
‎01-19-2018 02:32 PM
PROC Star
Posts: 1,555

Re: Unquoting a Macro variable in a Macro

Posted in reply to Learning_S

still the same thing:

 

%macro m(var);
	%let var=&var;

			proc surveyselect data=subjects groups=(&var) reps=3 noprint
			    out=randomizer(rename=groupId=reader); 
			run;
%mend m;%m(%str(2,2,2,2))
Occasional Contributor
Posts: 12

Re: Unquoting a Macro variable in a Macro

Posted in reply to novinosrin

I apologize. I misunderstood and thought that it could not be used in the macro. Thank you.

PROC Star
Posts: 1,555

Re: Unquoting a Macro variable in a Macro

Posted in reply to Learning_S

It's all good. No big words like apologies needed. Makes mistakes to learn and share here. I was dumb 3 years ago when i wrote my first real code lol

Super User
Super User
Posts: 7,931

Re: Unquoting a Macro variable in a Macro

[ Edited ]
Posted in reply to Learning_S

The easiest way is to not include commas in your macro parameter values. That is easy to do as in most cases SAS usually uses spaces as delimiters instead of commas. That is true for the IN () operator and also for the GROUPS= option on PROC SURVEYSELECT. 

Example:

proc surveyselect data=sashelp.class(obs=4) groups=(2 2) reps=3 noprint
  out=randomizer(rename=groupId=reader)
;
run;

If you really did need to pass in commas than find another way to protect that does not require macro quoting.  For example you could have the macro user pass in the parenthesis.

%macro m(var);
proc surveyselect data=subjects
    groups=&var reps=3 noprint
    out=randomizer(rename=groupId=reader)
;
run;
%mend m;
%m(var=(2,2,2,2))

 In fact SAS doesn't mind if you end up with a second set of parenthesis.

%macro m(var);
proc surveyselect data=subjects
    groups=(&var) reps=3 noprint
    out=randomizer(rename=groupId=reader)
;
run;
%mend m;
%m((2,2,2,2))

 

So your current macro would work fine without the %UNQUOTE() if you just called it without macro quoting the parameter values.

%m(2 2 2 2);
☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 134 views
  • 2 likes
  • 3 in conversation