BookmarkSubscribeRSS Feed
hashman
Ammonite | Level 13

@David_Billa:

In this situation, using WHERE instead of IF (as @Kurt_Bremser has offered) doesn't win you much. But it can be done in a single step. One way:

data inc exl ;                                                                                                                          
  set sashelp.class (where=(sex eq "M") in=inc)                                                                                         
      sashelp.class (where=(sex ne "M"))                                                                                                
  ;                                                                                                                                     
  if inc then output inc ;                                                                                                              
  else        output exl ;                                                                                                              
run ;  

Another way:

data inc exl ;                                                                                                                          
  do until (z_inc) ;                                                                                                                    
    set sashelp.class (where=(sex eq "M")) end = z_inc ;                                                                                
    output inc ;                                                                                                                        
  end ;                                                                                                                                 
  do until (z_exc) ;                                                                                                                    
    set sashelp.class (where=(sex ne "M")) end = z_exc ;                                                                                
    output exl ;                                                                                                                        
  end ;                                                                                                                                 
run ;    

 I wouldn't recommend either because in both cases you end up reading all input records into the buffer, which defies the purpose of using WHERE vs IF in the first place.

 

Kind regards

Paul D.

Kurt_Bremser
Super User

@David_Billa Mind that all solutions relying on WHERE involve multiple reads of the input dataset and need considerably more code than the simple if, so they are inefficient both in use of computing resources and programmer's time.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 16 replies
  • 3025 views
  • 4 likes
  • 5 in conversation