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.
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.
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.