BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Learning_S
Obsidian | Level 7

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?

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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

8 REPLIES 8
novinosrin
Tourmaline | Level 20

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

 

Learning_S
Obsidian | Level 7

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?

novinosrin
Tourmaline | Level 20

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

 

 

 

Learning_S
Obsidian | Level 7

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

novinosrin
Tourmaline | Level 20

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))
Learning_S
Obsidian | Level 7

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

novinosrin
Tourmaline | Level 20

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

Tom
Super User Tom
Super User

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);

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 8 replies
  • 1043 views
  • 2 likes
  • 3 in conversation