08-15-2015 11:51 PM
I have a question about resolving macro variables in SAS. I have the following code, which is a simplified version of a much larger block. For practical reason, I cannot change the structure of the code.
%let a = x1 x2 x3;
%let b = y1 y2 y3;
%let c = a b;
%macro test (input);
%local i;
%let string_c = %str(&input);
%do i=1 %to 2;
%put &%qscan(&string_c, &i); /* ? */
%end;
%mend test;
%test(&c);
In Step ? above, I would like to resolve a and b as macro variables and have the system print out
x1 x2 x3
and then
y1 y2 y3
However, the code above does not reslove a and b as macro variables and the system prints out
&a
&b
I am wondering if there is any solution to this problem.
Thanks very much!
08-16-2015 04:42 PM
Separating the &, and quoting the string, can be undone pretty easily:
%put %unquote(&%qscan(&string_c, &i));
I can't test it right now, but it should work. Good luck.
08-16-2015 10:26 PM
Thank you, Astounding. It seems that I need to put two ampers for it to work: %put %unquote(&&%qscan(&string_c, &i)); I am wondering if it is because of the %qscan function?
08-17-2015 08:01 AM
I guess not . You need && to scan twice to get what you need .
08-17-2015 08:30 AM
What is it your trying to do? There doesn't appear to be any value in creating this macro variable + macro + scanning structure. Present some test data and what you want out, as there is no reason it can't be done in base SAS.
08-17-2015 09:27 AM
Without the %UNQUOTE() the macro variable resolution and the macro function resolutions are treated as two independent things by the macro processor. By adding the %UNQUOTE() you force the macro processor to resolve the text inside the function call as one unit.
Symbolgen can show you why you need the double ampersand. With just a single ampersand the bare & has nothing after it that looks like a macro variable name so it is just emitted. When there are two then they resolve to a single & and the processor knows that it needs to do another pass. On that second pass there is now text after the & so it can be resolved.
153 %*** One ***;
154 %put %unquote(&%scan(&b,1)) ;
SYMBOLGEN: Macro variable B resolves to A
&A
155 %*** Two ***;
156 %put %unquote(&&%scan(&b,1)) ;
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable B resolves to A
SYMBOLGEN: Macro variable A resolves to 1
1