|
In <199602201450.AA21567@gate1.health.state.ny.us>, Suhua Hu
<sxh04@HEALTH.STATE.NY.US> writes:
> a macro
>
>Could somebody explain the difference between "if... then..." and "%if...
>%then...," "put" and "%put" within a macro? I found sometimes they are
>exchangeable. Thanks a lot.
Well, you're asking a question on which books and many, many usenet
postings have been written. So the first item is to suggest that you
might want ot read the SAS Macro book. It will do a good job of
going through the SAS Macro facility from syntax to a good
description of what is going on behind the scenes.
IF ... THEN ... ELSE is a statement that can be used in a data
step.
%IF ... %ELSE ... %ELSE is a statement that can only be used in a
MACRO definition.
Examples:
DATA xyz ;
SET something ;
%IF var1 = var2 %THEN Put var1= var2= ;
will get you an error message telling you that you have tried to use
a macro statement in opem code (term used for PROC ... DATA ...
sequences).
On the other had something like:
%MACRO _ifstate(state) ;
%IF &state EQ TX %THEN PUT 'Texas' ;
%MEND _ifstate ;
DATA xyz ;
SET something ;
%_ifstate(AR) ;
%_ifstate(MD) ;
%_ifstate(TX) ;
is perfectly legit - I defined a macro procedure - and will resolve
to:
DATA xyz ;
SET something ;
; /* results from AR */
; /* results from MD */
PUT 'Texas' ; /* results from TX */
I like to think of Macro variables and procedures as shorthand -
they are abbreviations that the SAS macro facility fills in prior to
running the DATA or PROC step (with all deference to the current
thread on macro execution ;-)).
So when I said "%_ifstate(...)", the macro processor was "smart"
enough to know I really meant to test the value of &state and if it
equals TX, then really say "PUT 'Texas'" (no semicolon!).
I know this post hasn't been very cogent, but you are asking a very
broad question.
I keep blowing off my connection so I better post this quickly.
Richard Wright - rcw@tenet.edu
|