How to Use IF OR Logic in SAS


You can use the following basic syntax to use IF OR logic in SAS:

data new_data;
    set my_data;
    if team="Cavs" or points>20 then cavs_or_20 = 1;
    else cavs_or_20 = 0;
run;

This particular example creates a new dataset with a column called cavs_or_20 that takes on the following values:

  • 1 if the value in the team column is equal to “Cavs” or if the value in the points column is greater than 20.
  • 0 if neither condition is met.

The following example shows how to use this syntax in practice.

Example: How to Use IF OR Logic in SAS

Suppose we have the following dataset in SAS that contains information about various basketball players:

/*create dataset*/
data my_data;
    input team $ points;
    datalines;
Cavs 12
Cavs 24
Warriors 15
Cavs 26
Warriors 14
Celtics 36
Celtics 19
;
run;

/*view dataset*/
proc print data=my_data;

Suppose we would like to create a new dataset with a column that takes on the following values:

  • 1 if the value in the team column is equal to “Cavs” or if the value in the points column is greater than 20.
  • 0 if neither condition is met.

We can use the following syntax to do so:

/*create new dataset*/
data new_data;
    set my_data;
    if team="Cavs" or points>20 then cavs_or_20 = 1;
    else cavs_or_20 = 0;
run;

/*view new dataset*/
proc print data=new_data;

The new column called cavs_or_20 uses IF OR logic to determine if each row in the dataset should have a value of 0 or 1.

For example:

  • The team name in the first row is “Cavs” so the cavs_or_20 column takes on a value of 1.
  • The team name in the second row is “Cavs” so the cavs_or_20 column takes on a value of 1.
  • The team name in the first row is not “Cavs” and the points value is not greater than 20 so the cavs_or_20 column takes on a value of 0.

And so on.

Additional Resources

The following tutorials explain how to perform other common tasks in SAS:

How to Rename Variables in SAS
How to Create New Variables in SAS
How to Replace Characters in a String in SAS

Leave a Reply

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