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
- /
- SAS Procedures
- /
- if then statement

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

08-12-2016 12:01 AM

dataset:

subj q1 q2 q3 q4 q5 q_total newvar

1 1 3.1 2.1 1 1 8.2

2 . . . . . .

3 1 1.2 1 0 1.1 4.3

4 0 1 1 .6 1 4.6

i would like to create a new variable (newvar), where if q1 <=1 and if q2 is <= 1 and if q3 is <=1 and if qtotal is <= 8, then newvar =1 , otherwise newvar =0, but if q1 through qtotal is ., then newvar is .

(for my data set, if one value is missing in a row that means all values are missing for that row...just an fyi---look at subj 2)

so newvar would look like:

newvar

0

.

0

1

Accepted Solutions

Solution

08-12-2016
12:59 AM

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

Posted in reply to starz4ever2007

08-12-2016 12:42 AM

You should be using if/else-if rather than multiple if statements. It's faster since once one is true it exists the set of conditions.

Also, SAS treats missing as the lowest value so you want to check for missing first.

There is no comma before the THEN

Note that you use <= for q1/q2 but < for q3

Note that you have cases that aren't accountd for so you may want to have an else to capture the others. I always like to, since it catches errors in my logic. In this case, I've coded it to 99. If you didn't account for this here, they would be coded as ., missing, and would not be differentiable from your first case.

```
data new;
set old;
if q1=. then newvar=.;
else if q1<=1 and q2<=1 and q3<1 and qtotal<=8 then newvar = 1;
elseif q1>1 and q2>1 and q3 >1 and qtotal>8 then newvar=0;
else newvar=99;
run;
```

All Replies

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

Posted in reply to starz4ever2007

08-12-2016 12:28 AM

You've had several examples of IF/THEN statements now posted on here and answered.

What have you tried so far?

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

Posted in reply to Reeza

08-12-2016 12:37 AM

data new;

set old;

if q1<=1 and q2<=1 and q3<1 and qtotal<=8, then newvar = 1;

if q1>1 and q2>1 and q3 >1 and qtotal>8, then newvar=0;

if q1 =. , then newvar=.;

run;

Solution

08-12-2016
12:59 AM

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

Posted in reply to starz4ever2007

08-12-2016 12:42 AM

You should be using if/else-if rather than multiple if statements. It's faster since once one is true it exists the set of conditions.

Also, SAS treats missing as the lowest value so you want to check for missing first.

There is no comma before the THEN

Note that you use <= for q1/q2 but < for q3

Note that you have cases that aren't accountd for so you may want to have an else to capture the others. I always like to, since it catches errors in my logic. In this case, I've coded it to 99. If you didn't account for this here, they would be coded as ., missing, and would not be differentiable from your first case.

```
data new;
set old;
if q1=. then newvar=.;
else if q1<=1 and q2<=1 and q3<1 and qtotal<=8 then newvar = 1;
elseif q1>1 and q2>1 and q3 >1 and qtotal>8 then newvar=0;
else newvar=99;
run;
```

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

Posted in reply to Reeza

08-12-2016 01:03 AM

and then i can make 99 to . if i wanted to with a simple if/then at a later point if I want to, right?

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

Posted in reply to starz4ever2007

08-12-2016 01:17 AM

Yes, or change the code to either remove that line or set to missing as well instead of 99.

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

Posted in reply to Reeza

08-12-2016 12:39 PM

so for some of my values that are supose to be 1, they are being replaced with 99

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

Posted in reply to starz4ever2007

08-12-2016 12:45 PM

i mean they are suppose to be 0, but they are reading as 99 (all q1 to q3 all are greater than one and total is greater than 8, so they should be 0)

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

Posted in reply to starz4ever2007

08-12-2016 03:20 PM

Please post your exact code and the data lines that are not meeting the logic.

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

Posted in reply to Reeza

08-12-2016 05:18 PM

**code used**

**data** new;

set old;

if q1=**.** then q1_resp=**.**;

else if q1<=**1** and q2<=**1** and q3<=**1** and qtotal<=**8** then q1_resp = **1**;

else if q1>**1** and q2>**1** and q3>**1** and qtotal>**8** then q1_resp=**0**;

else q1_resp=**99**;

**run**;

data looks like :

subj q1 q2 q3 qtotal q1resp

1 0 1 0.14285 1.14285 1

2 0.4285 13 0 13.4285 99 so this one should be q1resp=0

3 . . . . .

4 11 4 3.5 18.5 0

So I think I am seeing my problem (correct me if I am wrong). The reason subj 2 is not 0 is because q1 and q3 is less than 1, while my code reads that q1 AND Q2 and q3 should be greater than 1 to make it to 0. So while this part of the code (else if q1<=**1** and q2<=**1** and q3<=**1** and qtotal<=**8** then q1_resp = **1** is actually what I want to happen, this code (q1>**1** and q2>**1** and q3>**1** and qtotal>**8** then q1_resp=**0**;) can be q1, q2, and q3 be indepenent of qtotal. What I mean is that q1, q2, q3 can be less than or equal to 1 with qtotal is >8 to make it q1_resp=0, and in the same fashion q1, q2, q3 can be greater than one with qtotal<8.

So basically I can delete this line then: else if q1>**1** and q2>**1** and q3>**1** and qtotal>**8** then q1_resp=**0**;

and Missing will remain as ., the code i want will be 1, and everything else witll be 99 and i can make an if then statemetn to change 99 to 0.

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

Posted in reply to starz4ever2007

08-12-2016 05:36 PM

As long as you get what you want. Since I don't know I can't actually say if it's correct or not

Instead of AND's you may want OR, note the parentheses.

if (q1<1 or q2<1 or q3<1) AND q8<=1

The good thing with this type of programming is you can test it out pretty easily and tweak it until you get what you need. Just make sure to test all cases.

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

Posted in reply to Reeza

08-12-2016 10:31 PM

thank you for all your help! i appreciate it