BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.

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 ;

 

BASUG is hosting free webinars Next up: Mike Raithel presenting on validating data files on Wednesday July 17. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

Heard back from Tech Support already.  Macro legend Russ Tyndall explained this is a known bug, already documented:

https://support.sas.com/kb/65/310.html

BASUG is hosting free webinars Next up: Mike Raithel presenting on validating data files on Wednesday July 17. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

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
--
Paige Miller
ErikLund_Jensen
Rhodochrosite | Level 12

Hi @Quentin 

 

It works in Linux but not in Windows, so I think you are right in assuming it is a bug. 

 

ErikLund_Jensen_0-1679672667987.png

 

 

Quentin
Super User

Thanks, will submit it as a bug.

BASUG is hosting free webinars Next up: Mike Raithel presenting on validating data files on Wednesday July 17. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Quentin
Super User

Heard back from Tech Support already.  Macro legend Russ Tyndall explained this is a known bug, already documented:

https://support.sas.com/kb/65/310.html

BASUG is hosting free webinars Next up: Mike Raithel presenting on validating data files on Wednesday July 17. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Quentin
Super User

Russ updated me that the bug is fixed in 9.4M8.

BASUG is hosting free webinars Next up: Mike Raithel presenting on validating data files on Wednesday July 17. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Tom
Super User Tom
Super User

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

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 659 views
  • 4 likes
  • 4 in conversation