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

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