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

Hi, I have a question about how to remove duplicates and also replace data by specfic condition.

I have built a table below, I want for all the variables a to h, if on the same ID, the variable has a 'yes' then the variable will show 'yes' and if the variable only has 'no' then the varable will show 'no'.

 

IDabcdefgh
1nononononoyesnono
1noyesnononononono
1yesnonoyesnononono
2nonoyesnononoyesno
2yesnonononoyesnono

 

The result should look like:

IDabcdefgh
1yesyesnoyesnoyesnono
2yesnoyesnonoyesyesno
1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

You can use next tested code:

data have;
input ID (a b c d e f g h)(:$3.);
datalines;
1 no  no  no  no  no  yes no  no
1 no  yes no  no  no  no  no  no
1 yes no  no  yes no  no  no  no
2 no  no  yes no  no  no  yes no
2 yes no  no  no  no  yes no  no
;
run;

proc sql;
  select max(a) as a,
         max(b) as b,
         max(c) as c,
         max(d) as d,
         max(e) as e,
         max(f) as f,
         max(g) as g,
         max(h) as h
  from have 
  group by ID;
quit;

View solution in original post

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20
data have;
input ID (a b c d e f g h)(:$3.);
datalines;
1 no  no  no  no  no  yes no  no
1 no  yes no  no  no  no  no  no
1 yes no  no  yes no  no  no  no
2 no  no  yes no  no  no  yes no
2 yes no  no  no  no  yes no  no
;

data want;
   do until (last.ID);
      set have;
      by ID;
      array r $ a -- h;
      array rr {8} _temporary_;
      
      do over r;
         if r = "yes" then rr[_i_] = 1;
      end;
   end;
   
   do over r;
      r = ifc(rr[_i_], "yes", "no");
   end;

   call missing (of rr[*]);
run;

 

Result:

 

ID a   b   c   d   e   f   g   h
1  yes yes no  yes no  yes no  no
2  yes no  yes no  no  yes yes no 

 

Shmuel
Garnet | Level 18

You can use next tested code:

data have;
input ID (a b c d e f g h)(:$3.);
datalines;
1 no  no  no  no  no  yes no  no
1 no  yes no  no  no  no  no  no
1 yes no  no  yes no  no  no  no
2 no  no  yes no  no  no  yes no
2 yes no  no  no  no  yes no  no
;
run;

proc sql;
  select max(a) as a,
         max(b) as b,
         max(c) as c,
         max(d) as d,
         max(e) as e,
         max(f) as f,
         max(g) as g,
         max(h) as h
  from have 
  group by ID;
quit;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 819 views
  • 2 likes
  • 3 in conversation