BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
NewUsrStat
Pyrite | Level 9

Hi all, 

suppose to have the following dataset:

data DB;
  input ID :$20. City :$20. School :$20. Parents :$20.;
cards;
0001 0   0   0
0001 1   1   1
0004 1   0   0
0004 0   0   1
0004 0   0   0
0005 0   1   1
0006 1   0   0
0006 1   1   1
0006 1   1   0
....
;

I'm trying to merge all the variables to get the following:

 

data DB1;
  input ID :$20. Index :$20.;
cards;
0001 0
0001 1
0004 1
0004 1
0004 0
0005 1
0006 1
0006 1
0006 1
....
;

The rule is: if there is at least one "1" in "City", "School", "Parents" then Index = 1 otherwise 0. I tried to use coalesce but it takes a lot of time because the variables to be collapsed are 10 but the rows are 30.000. Is there an alternative to do the job?

Thank you in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

Something like below should work.

data want;
  set have;
  index = whichc('1',city,school,parents) > 0;
run;

Bit weird that variables only containing zero and one are of type character with a length of $20

View solution in original post

2 REPLIES 2
AhmedAl_Attar
Ammonite | Level 13

why not use this 

data DB(KEEP=ID index);
  input ID :$20. City :$20. School :$20. Parents :$20.;
  
  index = max(city,school,parents);
cards;
0001 0   0   0
0001 1   1   1
0004 1   0   0
0004 0   0   1
0004 0   0   0
0005 0   1   1
0006 1   0   0
0006 1   1   1
0006 1   1   0
....
;
run;
Patrick
Opal | Level 21

Something like below should work.

data want;
  set have;
  index = whichc('1',city,school,parents) > 0;
run;

Bit weird that variables only containing zero and one are of type character with a length of $20

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 2 replies
  • 650 views
  • 1 like
  • 3 in conversation