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       1    2.1  4.1   1    3      11.2       

3       1    1.2  1      0    1.1     4.3

4       0      1    0      1    0       2

 

i would like to creat 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.

 

so newvar would look like:

   newvar

        0

        0

        0

        1

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

newvar = q1 <=1 and q2 <= 1 and q3 <=1 and qtotal <= 8;

PG

View solution in original post

6 REPLIES 6
PGStats
Opal | Level 21

newvar = q1 <=1 and q2 <= 1 and q3 <=1 and qtotal <= 8;

PG
starz4ever2007
Quartz | Level 8

hmm im not getting newvar to equal 1 with that code

ballardw
Super User

@PGStats is being very terse.

 

His solution works for what you are doing because SAS treats true results as 1 and false as 0. So Newvar is the result of all of those comparisons, true or false.

 

You may want to consider if "otherwise newvar=0" includes cases of missing values. Missing are less than any value. So in this specific interest if all of the q variables are missing then newvar would be 1.

starz4ever2007
Quartz | Level 8

is adding q1 ne . and q2 ne . and q3 ne . to PG's statement the best option?

PGStats
Opal | Level 21

Your logical expression yields true (=1) when q1, q2, q3, or q_total are missing because missing is considered by SAS as inferior to any non-missing value. You can change the expression so that missing values will yield false, as demonstrated:

 

data test;
input subj  q1   q2   q3   q4   q5    q_total;
newvarTrue = q1 <=1 and q2 <= 1 and q3 <=1 and q_total <= 8;
newvarFalse = -q1 >= -11 and -q2 >= -1 and -q3 >= -1 and -q_total >= -8;
datalines;
1       1    3.1  2.1   1    1        8.2
2       1    2.1  4.1   1    3      11.2       
3       1    1.2  1      0    1.1     4.3
4       0      1    0      1    0       2
5       .      1    0      1    0       2
;

proc print; run;

Add a comment in your code to explain the trick.

PG
starz4ever2007
Quartz | Level 8

thank you very much!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1467 views
  • 1 like
  • 3 in conversation