DATA Step, Macro, Functions and more

Macro

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 19
Accepted Solution

Macro

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;


Accepted Solutions
Solution
‎10-09-2017 12:52 AM
Trusted Advisor
Posts: 1,022

Re: Macro

Posted in reply to Arun_shSAS

 

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

 

 

View solution in original post


All Replies
Solution
‎10-09-2017 12:52 AM
Trusted Advisor
Posts: 1,022

Re: Macro

Posted in reply to Arun_shSAS

 

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

 

 

PROC Star
Posts: 283

Re: Macro

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

Super User
Posts: 5,509

Re: Macro

Posted in reply to Arun_shSAS

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 130 views
  • 2 likes
  • 4 in conversation