turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- If-then-else

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-18-2016 12:59 AM

**data** a;

input name $8. level **1.**;

cards;

Alfred 1

Alice 2

Barbara 2

Carol 3

Brut 4

Kelly .

Juan 1

;

**run**;

**data** exp;

set a;

if level =**.** then exp = 'Unknown';

else if level = **1** then exp = 'Low';

else if level= **2** or **3** then exp ='Medium';

else exp = 'high';

**run**;

The output dor dataset exp is as below:

name | level | exp |

Alfred | . | Unknown |

Alice | 2 | Medium |

Barbara | 2 | Medium |

Carol | 3 | Medium |

Brut | 4 | Medium |

Kelly | . | Unknown |

Juan | 1 | Low |

Not sure why for level 4 exp is 'Medium'?

Accepted Solutions

Solution

01-18-2016
09:41 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-18-2016 02:01 AM - edited 01-18-2016 02:02 AM

`else if level= 2 or 3 then exp ='Medium';`

is translated as

`if (level = 2) or (3)`

Now, in SAS the boolean value of "false" is represented by the number zero; any other numerical value means "true". "3" is therefore always true, and makes the whole condition always true, meaning that the branch for "high" will never be entered.

Edit: Heh, Josh beat me to it.

---------------------------------------------------------------------------------------------

Maxims of Maximally Efficient SAS Programmers

Maxims of Maximally Efficient SAS Programmers

All Replies

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-18-2016 01:26 AM

You would need to use the 'in' operator or list out each condition of the 'or' operator for the 'Medium' assignment.

```
else if level in(2 3) then exp ='Medium';
/* or */
else if level = 2 or level = 3 then exp = 'Medium';
```

I would also recommend you take a look at PROC FORMAT. It is useful for assignment logic like this.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to JoshB

01-18-2016 01:32 AM

Thanks for the solution, but I am intrested in understanding why exp for level 4 is 'Medium' when datastep written in that way.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-18-2016 01:40 AM

Not 100% sure, but my hunch is that the expression is being evaluated like so..

if (level = 2) or ( 3 )

For your observation in question, level = 2 is evaulated as false, but (3) on it's own is always true. It would be like saying if 3 ^= .

The if statement returns a value of true, thus the assignment of 'Medium' takes place.

Solution

01-18-2016
09:41 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-18-2016 02:01 AM - edited 01-18-2016 02:02 AM

`else if level= 2 or 3 then exp ='Medium';`

is translated as

`if (level = 2) or (3)`

Now, in SAS the boolean value of "false" is represented by the number zero; any other numerical value means "true". "3" is therefore always true, and makes the whole condition always true, meaning that the branch for "high" will never be entered.

Edit: Heh, Josh beat me to it.

---------------------------------------------------------------------------------------------

Maxims of Maximally Efficient SAS Programmers

Maxims of Maximally Efficient SAS Programmers