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

I have a dataset like below:

Subj   C1    C2

S01    Y      N

S01    N      N

S02    Y      N

S02    N      Y

 

I need to combine the rows with distinct Subjs as below:

 

Subj   C1    C2

S01    Y      N

S02    Y      Y

 

How should I go about it, please?

 

Thank you for your help in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Actually, the SAS implementation of SQL permits using the MAX function on character fields:

 

proc sql;

create table want as select Subj, max(C1) as C1, max(C2) as C2 , count(*) as nrows

from have

group by subj;

quit;

View solution in original post

4 REPLIES 4
LinusH
Tourmaline | Level 20

If I understand right, Y has precedence over N?

If those were 1 and 0 respectively (and you could have Y and N as a SAS format instead for display purposes), you could use the max() SQL aggregate function.

Data never sleeps
Astounding
PROC Star

Actually, the SAS implementation of SQL permits using the MAX function on character fields:

 

proc sql;

create table want as select Subj, max(C1) as C1, max(C2) as C2 , count(*) as nrows

from have

group by subj;

quit;

zz
Calcite | Level 5 zz
Calcite | Level 5

There is one additional detail: I need to count the number of rows for each subject, while combining rows.  

 

Thank you for your below implementation!  It is exactly what I'd need.  

data_null__
Jade | Level 19
data C;
   input (Subj   C1    C2)($);
   cards;
S01    Y      N
S01    N      N
S02    Y      N
S02    N      Y
;;;;
   run;
proc print;
   run;
proc summary data=c nway;
   class subj;
   output out=temp 
      idgroup(max(c1) out(c1)=)
      idgroup(max(c2) out(c2)=)
      ;
   run;
proc print;
   run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 944 views
  • 3 likes
  • 4 in conversation