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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

 

 

 

 

View solution in original post

12 REPLIES 12
novinosrin
Tourmaline | Level 20
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;
Reeza
Super User
Do you really need an array here?
novinosrin
Tourmaline | Level 20

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

PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
Reeza
Super User

@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.

novinosrin
Tourmaline | Level 20

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. 🙂

aowais
Obsidian | Level 7
Thanks, but this results in all values for public_care = 0...
novinosrin
Tourmaline | Level 20

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
ballardw
Super User

@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.

Astounding
PROC Star

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.

 

 

 

 

aowais
Obsidian | Level 7

This worked! Thank you! You were right about the values being the issue. The values included 1, 2, 555 and some missing.

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 12 replies
  • 1305 views
  • 8 likes
  • 6 in conversation