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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

11 REPLIES 11
Reeza
Super User

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

What have you tried so far?

starz4ever2007
Quartz | Level 8

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;

Reeza
Super User

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;
starz4ever2007
Quartz | Level 8

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?

Reeza
Super User

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

 

starz4ever2007
Quartz | Level 8

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

starz4ever2007
Quartz | Level 8

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)

Reeza
Super User

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

starz4ever2007
Quartz | Level 8

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.

                                                 

Reeza
Super User

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. 

starz4ever2007
Quartz | Level 8

thank you for all your help! i appreciate it 🙂

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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