Help using Base SAS procedures

if then statement

Accepted Solution Solved
Reply
Contributor
Posts: 45
Accepted Solution

if then statement

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
Super User
Posts: 17,819

Re: if then statement

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;

View solution in original post


All Replies
Super User
Posts: 17,819

Re: if then statement

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

What have you tried so far?

Contributor
Posts: 45

Re: if then statement

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
Super User
Posts: 17,819

Re: if then statement

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;
Contributor
Posts: 45

Re: if then statement

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?

Super User
Posts: 17,819

Re: if then statement

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

 

Contributor
Posts: 45

Re: if then statement

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

Contributor
Posts: 45

Re: if then statement

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)

Super User
Posts: 17,819

Re: if then statement

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

Contributor
Posts: 45

Re: if then statement

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 = 1Smiley Wink 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.

                                                 

Super User
Posts: 17,819

Re: if then statement

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

 

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. 

Contributor
Posts: 45

Re: if then statement

thank you for all your help! i appreciate it Smiley Happy

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 623 views
  • 0 likes
  • 2 in conversation