Friday, June 6, 2014

Output statements using IF ELSE WHEN Clause in SAS - Creating multiple outputs in SAS

Sometimes we need to create multiple outputs in SAS depending on the IF ELSE conditions.The below JCL shows how to achieve this.
//SAS       EXEC SAS                    
//RYAN1     DD DSN=TEST.INPUT.SAS

//SYSOUT    DD SYSOUT=*                     
//SYSIN     DD *                

OPTION NOCENTER;                            
DATA DATA1;                                 
  INFILE RYAN1 MISSOVER;                    
  INPUT @6    POLNO     $CHAR10.            
        @10   CODE      $CHAR04.            
        ;                                   
  DATA EX1 EX2;                             
  SET DATA1;                                
  IF CODE='3992' THEN OUTPUT EX1;        
  IF CODE='T100' THEN OUTPUT EX2;       
  PROC PRINT DATA=EX1;                      
    TITLE 'EX1';                            
  PROC PRINT DATA=EX2;                      
    TITLE 'EX2'; 

Points to note: Both the output datasets, which we want to create should be mentioned in the DATA step. Here EX1 and EX2 are the ones.
Now, we can directly route these outputs to the output datasets as well.

Just a try with SELECT  WHEN clause in SAS  to get the same result.
OPTION NOCENTER;                 
DATA DATA1;                      
  INFILE RYAN1 MISSOVER;          
  INPUT @6    POLNO     $CHAR10. 
        @10   CODE   $CHAR04. 
        ;                        
  DATA EX1 EX2 EX3;              
  SET DATA1;                     
   SELECT (CODE);             
       WHEN  ('3992')  OUTPUT EX1;
       WHEN  ('1002')  OUTPUT EX2;
       OTHERWISE OUTPUT EX3;     
   END;                          
  PROC PRINT DATA=EX1;           
    TITLE 'EX1';                 
  PROC PRINT DATA=EX2;           
    TITLE 'EX2';                 

No comments:

Post a Comment