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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

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