2

I have a question on an exam paper which asks what is read in the SAS log

%let test=one;
%let one=two;
%let two=three;
%let three=last;

%put what displays is &&&&&test;

I was very surprised to find the answer was: two as I would have thought that this reference would fully resolve to last. SAS also agrees with the answer to be two.

Can anyone please explain how SAS arrives at the answer two as all theory notes I have read suggests that the macro processor should do the following

  • scan1 &&&&&test - > &&&&test ( i.e && resolves to & and tells processor to continue to scan from right to left)
  • scan2 &&&&test - > &&&one
  • scan3 &&&one - > &&two
  • scan4 &&two - > &three
  • scan5 &three - > last
Joe
  • 62,789
  • 6
  • 49
  • 67

1 Answers1

6

Using the symbolgen option can help see what is happening in the log:

1    options symbolgen;

2    %let test=one;
3    %let one=two;
4    %let two=three;
5    %let three=last;
6
7    %put what displays is &&&&&test;
SYMBOLGEN:  && resolves to &.
SYMBOLGEN:  && resolves to &.
SYMBOLGEN:  Macro variable TEST resolves to one
SYMBOLGEN:  && resolves to &.
SYMBOLGEN:  Macro variable ONE resolves to two
what displays is two

Going left to right and using brackets to show the tokens:

&&&&&test
(&&)(&&)(&test)
(&) (&) (one)
&&one
(&&)(one)
(&)(one)
&one
two
Amir
  • 880
  • 1
  • 6
  • 15
  • 1
    Thanks for your answer. However SAS Certification Prep guid explains this logic differently. They say the 1st scan resolves double ampersand to single ampersand, and then form then one scans for mright to left. your logic ( which is obviously correct) seems ot resolve form left rto right, and coninuesto resolve && to & .. strange. Still unclrea on how this works generally but thansk for your answer :-) – user3661461 May 22 '14 at 09:18
  • 1
    The [SAS Macro Language Reference](http://support.sas.com/documentation/cdl/en/mcrolref/64754/HTML/default/viewer.htm#n0vl7e3du5f7cjn1g4ajozqxe6b6.htm) says "It resolves the entire reference from left-to-right." I don't have the SAS Certification Prep Guide, but what you have quoted about right to left is new to me. – Amir May 22 '14 at 13:06
  • Look at the question this is dup'ed from; I go over the answer in a bit more detail there with some examples. I would say that the prep guide is probably not wrong, but is probably worded confusingly - but also don't have the prep guide, so don't know for sure. – Joe May 22 '14 at 14:23