BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BruceBrad
Lapis Lazuli | Level 10

Thanks for all your suggestions. jwillis' use of the %unquote statement does what I want. (And yes, I probably could do this with arrays, but my original problem get very unwieldy using array statements).

Now, can anyone explain why it works like this?  Presumably there is some automatic quoting going on, but I can't see why.

Fraktalnisse
SAS Employee

I thought that i gave the %unquote answer in reply 6.

BruceBrad
Lapis Lazuli | Level 10

So you did. I missed that. Credit now duly assigned 🙂

Thanks again.

BruceBrad
Lapis Lazuli | Level 10

Below is a response from SAS support on this. In short, & substitution will cause a rescan of the code, but % substitution will not. (I haven't found this difference documented anywhere, so this might be of general interest).

"I have an answer for you regarding what is happening with the macro issue you raised with Leon.

I simplified your example and added another statement to aid in the explanation.

1 %let B = b;

2 %macro A();

3 a

4 %mend A;

5

6 data d;

7 bzz=2;

8 v2=&B.zz;

9 azz=1;

10 v1=%A()zz;

__

22

ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, **, +, -, /, <, <=, <>,

              =, >, ><, >=, AND, EQ, GE, GT, IN, LE, LT, MAX, MIN, NE, NG, NL, NOTIN, OR, ^=, |,

              ||, ~=.

In the first instance (v2=&B.zz), the variable substitution operator & causes a rescan of the source starting at the point of the &.  The rescan then sees the resultant text bzz as a single token and the Data Step compiler recognizes it as a known variable name.

In the second instance (v1=%A()zz;), the generated text from the execution of %A does not cause a rescan.  The generated “a”, is consumed by the Data Step compiler as the next token and it then sees the “zz” as a separate subsequent token. This causes the error.

The %UNQUOTE causes a rescan which has the side effect of the “a” and “zz” getting merged into a single token and the Data Step compiler is happy.

This is a subtle difference between using % and &.  In this case, the & is the appropriate mechanism to use.  Please let me know if you have any additional questions regarding this issue.

Regards,

Chris Warters

jwillis
Quartz | Level 8

Bruce, thank you for posting!   All the responders, Thank you for educating me!Smiley Happy

%macro test(n);

%if &n=QTR %then MSRP;

%if &n=MONTH %then INVOICE;

%mend test;

%LET version = QTR;  *%LET version=MONTH;

proc sql;

  create table cars_total as

  select distinct sum(%unquote(%test(&VERSION.))) as &version._%unquote(%test(&VERSION.))_total, Make

   from sashelp.cars

   group by make

   ;

  quit;

  proc print data=cars_total(obs=9);run;

jakarman
Barite | Level 11

Yes fraktalnisse Unquoting (or othere macro func) as having te string resolved by the macro-processor seem easier to me too.
All is about understanding the timing with macro-s http://blogs.sas.com/content/publishing/2015/04/01/sas-authors-tip-macro-language-timing-is-everythi...

And if it is a real coding problem needing a bypass. Than knowing the token as root cause you can avoid that in many ways.
My approach:
- Code the fixed value part in the macro so it can return to variable as one token for the datastep compiler.

44         %macro test(n,var=zz);

45         %if &n=1 %then a&var;

46         %if &n=2 %then b&var;

47         %mend test;

48       

49       

50         data  sim ;

51         azz=0;

52         v1 = %test(1) ;  /* produces no error */

53         run;

---->-- ja karman --<-----

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
  • 20 replies
  • 2386 views
  • 4 likes
  • 7 in conversation