Dear Members,
I came across a silly query. I hope I get a better solution.
Consider this code. I define a macro var a whose value is 12(which is text for SAS).
when I compare this macro var through where statement then how sas is transforming this 12(text) to numerical 12?
%let a= 12;
proc print data=sashelp.class;
where age <= &a;
run;
%let a=12 is text for macro, but not for SAS. The expression
where age < &a
is translated by the macro processor to
where age < 12;
which is specifying the numeric literal 12.
If you wanted text, you could have used
where age <= "&a" ;
Of course SAS would have objected to comparing numeric variable AGE to a character literal value.
%let a=12 is text for macro, but not for SAS. The expression
where age < &a
is translated by the macro processor to
where age < 12;
which is specifying the numeric literal 12.
If you wanted text, you could have used
where age <= "&a" ;
Of course SAS would have objected to comparing numeric variable AGE to a character literal value.
Also, this is a classic example of timing, and tokenisation. At compile time the macro resolution occurs and the tokens or in other words text remains as in the input stack. At this moment, the compiler is requesting tokens from the input stack. When resolution completes i.e &a becomes 12, the statements move to the complier and the 12 along with moves as a numeric token. Now, your compiled text executes. I hope this helps.
I can see and appeciate the beauty of tokenisation.
Best way to remember this, when macro triggers and macro processor are in action, tokenisation pauses and everything in the input stack pretty much sleeps. Once, the macro resolution completes, tokenisation resumes and each token is sent to compiler for execution where operands and operators work as expected
I have no disagreements with the previous responders. I just think this explanation would help you understand.
Consider this slightly different program:
%let a=12;
proc print data=sashelp.class;
where age <= &a;
title "Results for Age < &a";
run;
Macro language makes no judgement about &a being numeric or character. Instead, it replaces "&a" with "12" so that SAS language sees:
proc print data=sashelp.class;
where age <= 12;
title "Results for Age < 12";
run;
SAS language interprets the program, after macro language has finished substituting for &a. SAS language alone makes the judgment about whether it is encountering text or a number.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.