How can I use IF-THEN-DO statements in SAS with examples? 2

How can I use IF-THEN-DO statements in SAS with examples?

IF-THEN-DO statements in SAS are a conditional programming tool used to execute a specific set of actions when a certain condition is met. This allows for more efficient and precise data processing and analysis. The syntax for an IF-THEN-DO statement in SAS is as follows:

IF THEN DO;

END;

The can be any logical or arithmetic expression, and the can range from simple assignments to complex data manipulation.

For example, if we want to create a new variable “age_group” based on the age of a person, we can use the following IF-THEN-DO statement in SAS:

IF age = 18 AND age
END;

Overall, IF-THEN-DO statements in SAS provide a powerful and flexible way to control the flow of data processing and analysis. They can be used in various scenarios to make the code more efficient and accurate.

Use IF-THEN-DO in SAS (With Examples)


You can use an IF-THEN-DO statement in SAS to do a block of statements if some condition is true.

This statement uses the following basic syntax:

if var1 = "value" thendo;
    new_var2 = 10;
    new_var3 = 5;
end;

Note: An IF-THEN statement is used when you only want to do one statement. An IF-THEN-DO statement is used when you want to do several statements.

The following example shows how to use an IF-THEN-DO statement in practice.

Example: IF-THEN-DO in SAS

Suppose we have the following dataset in SAS that shows the total sales made by two stores during consecutive days:

/*create dataset*/
data original_data;
    input store $ sales;
    datalines;
A 14
A 19
A 22
A 20
A 16
A 26
B 40
B 43
B 29
B 30
B 35
B 33
;
run;

/*view dataset*/
proc printdata=original_data;

We can use the following IF-THEN-DO statement to create two new variables that take on certain values if the store is equal to “A” in the original dataset:

/*create new dataset*/
data new_data;
set original_data;
if store = "A" then do;
    region="East";
    country="Canada";
end;
run;

/*view new dataset*/
proc printdata=new_data;

IF-THEN-DO statement in SAS

Here’s how this chunk of code worked:

If the store was equal to “A” then a new variable called region was created with a value of “East” and a new variable called country was created with a value of “Canada.”

Note that we can use multiple IF-THEN-DO statements as well:

/*create new dataset*/
data new_data;
set original_data;

if store = "A" then do;
    region="East";
    country="Canada";
end;

    if store = "B" then do;
    region="West";
    country="USA";
    end; 
run;

/*view new dataset*/
proc printdata=new_data;

  • If the store was equal to “A” then a new variable called region was created with a value of “East” and a new variable called country was created with a value of “Canada.”
  • If the store was equal to “B” then the value for region was “West” and the value for country was “USA.”

Additional Resources

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

Cite this article

stats writer (2024). How can I use IF-THEN-DO statements in SAS with examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-if-then-do-statements-in-sas-with-examples/

stats writer. "How can I use IF-THEN-DO statements in SAS with examples?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-if-then-do-statements-in-sas-with-examples/.

stats writer. "How can I use IF-THEN-DO statements in SAS with examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-if-then-do-statements-in-sas-with-examples/.

stats writer (2024) 'How can I use IF-THEN-DO statements in SAS with examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-if-then-do-statements-in-sas-with-examples/.

[1] stats writer, "How can I use IF-THEN-DO statements in SAS with examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.

stats writer. How can I use IF-THEN-DO statements in SAS with examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top