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
newvar = q1 <=1 and q2 <= 1 and q3 <=1 and qtotal <= 8;
hmm im not getting newvar to equal 1 with that code
@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.
is adding q1 ne . and q2 ne . and q3 ne . to PG's statement the best option?
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.
thank you very much!
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: