I don't use an open (i.e. outside of macro definition) %IF much.
Today I was playing with open %IF for a communities question, and hit a problem where it looks like open %ELSE can't be used to generate part of a statement. Feels like perhaps a bug? Unless it's documented that open %IF/%ELSE shouldn't be used to generate part of a SAS statement.
This step won't compile (9.4M7):
data want ;
set
%if 0 %then %do ;
sashelp.class
%end ;
%else %do ;
sashelp.shoes
%end ;
;
run ;
The error is:
480 data want ;
481 set
482 %if 0 %then %do ;
483 sashelp.class
484 %end ;
485 %else %do ;
486 sashelp.shoes
-------------
557
ERROR 557-185: Variable sashelp is not an object.
487 %end ;
ERROR: DATA STEP Component Object failure. Aborted during the COMPILATION phase.
NOTE: The SAS System stopped processing this step because of errors.
488 ;
489 run ;
If I put the beginning of the SET statement in the %IF block it works. But there is another oddity. Below code works, even though there is no semicolon to end the SET statement:
data want ;
%if 0 %then %do ;
set sashelp.class
%end ;
%else %do ;
set sashelp.shoes
%end ;
run ;
and below code will error:
data want ;
%if 0 %then %do ;
set sashelp.class
%end ;
%else %do ;
set sashelp.shoes
%end ;
sashelp.cars ;
run ;
Interestingly, the problem seems to be related to the code generated by the open %ELSE. If I change the first %IF to %IF 1, then all of the above examples give the results I would expect.
data want ;
set
%if 1 %then %do ;
sashelp.class
%end ;
%else %do ;
sashelp.shoes
%end ;
;
run ;
*errors because of missing semicolon for SET statement;
data want ;
%if 1 %then %do ;
set sashelp.class
%end ;
%else %do ;
set sashelp.shoes
%end ;
run ;
data want ;
%if 1 %then %do ;
set sashelp.class
%end ;
%else %do ;
set sashelp.shoes
%end ;
sashelp.cars
;
run ;
Heard back from Tech Support already. Macro legend Russ Tyndall explained this is a known bug, already documented:
Your exact code inside a macro works, so yes, your example does feel like a bug to me.
%macro dothis;
data want ;
set
%if 0 %then %do ;
sashelp.class
%end ;
%else %do ;
sashelp.shoes
%end ;
;
run ;
%mend;
%dothis
Thanks, will submit it as a bug.
Heard back from Tech Support already. Macro legend Russ Tyndall explained this is a known bug, already documented:
Russ updated me that the bug is fixed in 9.4M8.
Support for open code %IF is very limited.
I would suggest using a macro variable so that you have complete statements inside the %DO/%END blocks.
663 %if 0 %then %do ;
664 %let dsn=sashelp.class;
665 %end ;
666 %else %do ;
667 %let dsn=sashelp.shoes;
668 %end ;
669 data want ;
670 set &dsn ;
671 run ;
NOTE: There were 395 observations read from the data set SASHELP.SHOES.
NOTE: The data set WORK.WANT has 395 observations and 7 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.