Resolve Macro Variable in SAS

Reply
New Contributor
Posts: 2

Resolve Macro Variable in SAS

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!

Super User
Posts: 5,370

Re: Resolve Macro Variable in SAS

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.

New Contributor
Posts: 2

Re: Resolve Macro Variable in SAS

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?

Super User
Posts: 9,878

Re: Resolve Macro Variable in SAS

I guess not . You need && to scan twice to get what you need .

Super User
Super User
Posts: 7,725

Re: Resolve Macro Variable in SAS

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.

Super User
Super User
Posts: 6,849

Re: Resolve Macro Variable in SAS

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

Ask a Question
Discussion stats
  • 5 replies
  • 268 views
  • 0 likes
  • 5 in conversation