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 ;

 

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
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

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.

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.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
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

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
Quentin
Super User

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

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
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

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 949 views
  • 4 likes
  • 4 in conversation