BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Arun_shSAS
Fluorite | Level 6

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;

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

 

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

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

3 REPLIES 3
mkeintz
PROC Star

 

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

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
novinosrin
Tourmaline | Level 20

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

Astounding
PROC Star

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.

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
  • 3 replies
  • 831 views
  • 2 likes
  • 4 in conversation