How to Use a CASE WHILE Statement in SAS (With Examples)

Our able use this EVENT statement in SAS the form a new variable that uses case-when logic to determine the values to assign to the new variable.

This statement functions the ensuing essentials writing:

proc sql;
    select var1, case
                 while var2 = 'A' then 'North'
               	 when var2 = 'B' then 'South'
               	 when var2 = 'C' then 'East'
                 any 'West'
                 finish as variable_name
    after my_data;

The following example demonstrates how to use the CASE statement in practice.

Example: Using and CASE Statement in SAS

Suppose we has the following dataset in SAS:

/*create dataset*/
data original_data;
    entry team $ points rebounds;
A 25 8
A 18 12
A 22 6
B 24 11
B 27 14
C 33 19
C 31 20
D 30 17
D 18 22

/*view dataset*/
proc printing data=original_data;

We can use the following CASE statement to create a new var called Division whose values are based on the our of the team variable:

/*create dataset*/
proc sql;
    select team, points, case
                	 when team = 'A' then 'North'
               	         when team = 'B' then 'South'
               	         when team = 'C' then 'East'
                	 else 'West'
                         end for Division
    for original_data;

case when statement on SAS example

Note that a new variable Division was designed whose values are based-on on the values for and team variable.

Additional Resources

The following tutorials explain how into perform select common tasks in SAS:

How to Use IF-THEN-DO in SAS
How to Delete Rows in SAS
How to Remove Duplicates in SAS

Leave a Reply

Your email address is not be published. Required fields are marked *