BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Obsidian | Level 7

## How to properly use IF/ELSE statements in SAS

I have a dataset which looks like this.

``````data have;
infile datalines truncover;
input type \$ quarter phase counter percent avg std;
datalines;
XX-A 202001 1 30 20 20 1
XX-A 202002 1 50 2 20 1
XX-A 202003 1 60 2 20 2
XX-A 202004 1 50 10 20 2
XX-B 202001 2 30 4 30 3
XX-B 202002 2 60 4 30 3
XX-B 202003 2 70 1 30 3
XX-B 202004 2 20 3 30 3``````

I want to create two new columns named STATUS1 and STATUS05 based on the following if/case statements:

(for COLUMN STATUS1)
If count is < (avg + std * 1) STATUS1 = 'green'

if count > (avg + std * 1) and < (avg +std * 2) STATUS1 = 'yellow'

if count > (avg + std * 2) and (avg + std * 3) STATUS1 = 'orange'
if count > (avg + std * 3) STATUS1 = 'red'

(for COLUMN STATUS05)

If count is < (avg + std * 0.5)  STATUS05 = 'green'

if count > (avg + std * 0.5) and < (avg +std * 2) STATUS05 = 'yellow'

if count > (avg + std * 1) and (avg + std * 3) STATUS05 = 'orange'
if count > (avg + std * 1.5) STATUS05 = 'red'

1 ACCEPTED SOLUTION

Accepted Solutions
Diamond | Level 26

## Re: How to properly use IF/ELSE statements in SAS

Although I suspect you already know this, your statements in SAS need to end with a semicolon.

You also need a THEN in each statement, and there are a few other syntax errors you need to fix.

Here is correct code:

``````if count <= (avg + std * 1) then STATUS1 = 'green';
else if count > (avg + std * 1) and count <= (avg +std * 2) then STATUS1 = 'yellow';
else if count > (avg + std * 2) and count <= (avg + std * 3) then STATUS1 = 'orange';
else if count > (avg + std * 3) then STATUS1 = 'red';``````

You might also want to include the following BEFORE the above code, as it will prevent truncation of the values of STATUS1.

``length status1 \$ 6;``
--
Paige Miller
5 REPLIES 5
Diamond | Level 26

## Re: How to properly use IF/ELSE statements in SAS

Although I suspect you already know this, your statements in SAS need to end with a semicolon.

You also need a THEN in each statement, and there are a few other syntax errors you need to fix.

Here is correct code:

``````if count <= (avg + std * 1) then STATUS1 = 'green';
else if count > (avg + std * 1) and count <= (avg +std * 2) then STATUS1 = 'yellow';
else if count > (avg + std * 2) and count <= (avg + std * 3) then STATUS1 = 'orange';
else if count > (avg + std * 3) then STATUS1 = 'red';``````

You might also want to include the following BEFORE the above code, as it will prevent truncation of the values of STATUS1.

``length status1 \$ 6;``
--
Paige Miller
Obsidian | Level 7

## Re: How to properly use IF/ELSE statements in SAS

Oke and where is the STATUS05? I want STATUS1 and STATUS05 in one view
Diamond | Level 26

## Re: How to properly use IF/ELSE statements in SAS

@Andalusia wrote:
Oke and where is the STATUS05? I want STATUS1 and STATUS05 in one view

I didn't show code for STATUS05. I expected you would be able to see what changes were made to the STATUS1 code, and make the same changes to the STATUS05 code.

--
Paige Miller
Super User

## Re: How to properly use IF/ELSE statements in SAS

@Andalusia wrote:

I have a dataset which looks like this.

``````data have;
infile datalines truncover;
input type \$ quarter phase counter percent avg std;
datalines;
XX-A 202001 1 30 20 20 1
XX-A 202002 1 50 2 20 1
XX-A 202003 1 60 2 20 2
XX-A 202004 1 50 10 20 2
XX-B 202001 2 30 4 30 3
XX-B 202002 2 60 4 30 3
XX-B 202003 2 70 1 30 3
XX-B 202004 2 20 3 30 3``````

I want to create two new columns named STATUS1 and STATUS05 based on the following if/case statements:

(for COLUMN STATUS1)
If count is < (avg + std * 1) STATUS1 = 'green'

if count > (avg + std * 1) and < (avg +std * 2) STATUS1 = 'yellow'

if count > (avg + std * 2) and (avg + std * 3) STATUS1 = 'orange'
if count > (avg + std * 3) STATUS1 = 'red'

(for COLUMN STATUS05)

If count is < (avg + std * 0.5)  STATUS05 = 'green'

if count > (avg + std * 0.5) and < (avg +std * 2) STATUS05 = 'yellow'

if count > (avg + std * 1) and (avg + std * 3) STATUS05 = 'orange'
if count > (avg + std * 1.5) STATUS05 = 'red'

You were almost there but have several import issues. First all statements end in a semicolon.

Second you have to have the value in the right relationship for comparison. I am not aware of any programming language the "distributes" a variable as you try in the second and third statement. SAS will allow you to place the value between comparison operators:

Third you need a THEN after the comparison(s) to tell SAS what to do when the condition is true.

Fourth, the length of character values will be set with the first value used if you do not set it prior. So yours would have been 5 characters long and truncated values for "yellow" and "orange" to "yello" and "orang" because the first value for status "green" set length to 5.

And last you need another IF in the last comparison.

```length status1 \$ 6;
If count is < (avg + std * 1) THEN STATUS1 = 'green';
else if (avg + std * 1) < count  < (avg +std * 2) Then STATUS1 = 'yellow';
else if   (avg + std * 2)< count <(avg + std * 3) then STATUS1 = 'orange';
else if count > (avg + std * 3) Then STATUS1 = 'red' ;```
Obsidian | Level 7

## Re: How to properly use IF/ELSE statements in SAS

Where is STATUS05?
Discussion stats
• 5 replies
• 495 views
• 4 likes
• 3 in conversation