Hi,
I am trying to create a binary variable based on the responses in 8 other variables. I'm currently using the following code, which is working, but I was wondering if there is a better way to do this, such as using arrays?
data two; set one;
if co7_1 = 1 or co7_2 = 1 or co7_3 = 1 or co7_4 = 1 or co7_5 = 1 or
co7_6 = 1 or co7_7 = 1 or co7_8= 1 then public_care = 1;
else public_care = 0;
run;
Without knowing the range of values these variables take on, there is no way to know whether the codes are equivalent. It's possible that the max value for some of the variables is 3 instead of 1. That generates a very different outcome.
You could probably switch to this instead:
public_care = (whichn(1, of col7_1-col7_8) > 0);
It's untested, but looks about right.
data two; set one;
/*if co7_1 = 1 or co7_2 = 1 or co7_3 = 1 or co7_4 = 1 or co7_5 = 1 or*/
/*co7_6 = 1 or co7_7 = 1 or co7_8= 1 then public_care = 1;*/
/*else public_care = 0;*/
array t(*) co7_1--co7_8;
public_care= max(of t(*))=1 ;
run;
No but @PaigeMiller demonstrated many times to avoid var: lists as that could lead to some ambiguity which made sense and henceforth I am in agreement to that best practice
Let me see if I can find that thread
@Reeza wrote:
Do you really need an array here?
Hello, @novinosrin
I think @Reeza was referring to the fact that you can use
public_care= max(of co7_1-co7_8)=1 ;
and no arrays are needed.
ah ok
@novinosrin I understand your comment, but if the array declaration is the same as in the OF there's no need for the array declaration. Unless you're using it more than once, then I'll create it because I'm a lazy typist.
got it. thick brains didn't get that insight at first until quantmiller clarified. I am even more convinced our search algorithm is paigerank algorithm. 🙂
Hi @aowais here is a demo
data one;
array t(*) co7_1-co7_8 (7*0 1);
run;
data two;
set one;
array t(*) co7_1--co7_8;
public_care= max(of t(*))=1 ;
run;
co7_1 | co7_2 | co7_3 | co7_4 | co7_5 | co7_6 | co7_7 | co7_8 | public_care |
---|---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |
@aowais wrote:
Thanks, but this results in all values for public_care = 0...
Please show the exact code you ran from the log. It is very easy to make a typo that may do that but without your code we can't tell where.
And some example input values for the variables that generate 0.
Without knowing the range of values these variables take on, there is no way to know whether the codes are equivalent. It's possible that the max value for some of the variables is 3 instead of 1. That generates a very different outcome.
You could probably switch to this instead:
public_care = (whichn(1, of col7_1-col7_8) > 0);
It's untested, but looks about right.
This worked! Thank you! You were right about the values being the issue. The values included 1, 2, 555 and some missing.
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!SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.