Edit

SAS: PROC SQL (Case When)

2016/12/18
  This article describes sample programs of 'CASE- WHEN' clause.  This clause initiates 'CASE' and ends 'END.'  Between two, case analysis will be conducted to use 'WHEN-THEN.'  This is similar to 'If - Then' statement in data-step.  In a case analysis in 'WHEN,'  you can use 5 patterns, '=, <, >,...,'  'Between and,' 'Contains,' 'Is null...,' and '=*.'  You can see the following green with bold. 


PROGRAM 
--------------------------------------
*************************************
CREATING NEW VARIABLES: #1
-> CASE WHEN END...
-------------------------------------
CASE-WHEN...
->  =,<,>,NOT,NE,AND,OR,IN...
-> BETWEEN AND
-> CONTAINS, ?
-> IS NULL, IS MISSING
-> =*
*************************************;
PROC SQL ;
 CREATE TABLE EFFICACY1
 AS SELECT *,
   CASE
     WHEN .< RES<0 THEN 'NEG '
     WHEN 0<=RES<5 THEN 'LOW '/*CANNOT USE '0=<RES<5'*/
     WHEN 5< RES   THEN 'HIGH'
     ELSE               'MISS'
   END AS RES_CAT        
 FROM EFFICACY ;
QUIT ;
PROC PRINT; RUN ;


*************************************
CREATING NEW VARIABLES: #2
-> CASE WHEN END...+ ALPHA
-> CHANGE THE DEFINITION BY VISIT (BASELINE OR POST-BASELINE)
*************************************;
PROC SQL ;
 CREATE TABLE D3
 AS SELECT *,
   CASE
     WHEN VISIT1 IN (1) &  .<RES<0   THEN 'NEG '
     WHEN VISIT1 IN (1) &  0<RES<=10 THEN 'LOW '
     WHEN VISIT1 IN (1) & 10<RES     THEN 'HIGH'

     WHEN VISIT1 IN (2) &  .<RES<0   THEN 'NEG '
     WHEN VISIT1 IN (2) &  0<RES<=5  THEN 'LOW '
     WHEN VISIT1 IN (2) &  5<RES     THEN 'HIGH'
    
     ELSE              'MISSING'
   END AS RES_CAT        
 FROM EFFICACY ;
QUIT ;
PROC PRINT; RUN ;
--------------------------------------  



OUTPUT (NOT FULL DATA)


Search This Blog