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

Hello all, I have the following data format. Each subject could be rated in either a Y or R category.

Subject
RaterCategorization
11Y
12Y
13R
21R
22R
31Y
32Y
41Y
42
43R

I would like to aggregate the data to the following count format:

SubjectCat_YCat_R
121
202
320
411

I am not sure I the best way would be to do this via a datastep or some proc (e.g means, freq, etc). In total I have ratings for about 60 subjects so I would like to automate the process as much as possible. Any suggestions?

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

I, too, think that sql would be the easiest.  However, if you prefer a datastep and your data are already sorted by subject, then you could use:

data want (keep=subject cat_:);

  set have;

  by subject;

  if first.subject then do;

    cat_y=0;

    cat_r=0;

  end;

  if categorization eq 'R' then cat_r+1;

  else if categorization eq 'Y' then cat_y+1;

  if last.subject then output;

run;

View solution in original post

3 REPLIES 3
Haikuo
Onyx | Level 15

proc sql;
create table want as
select subject, sum(Categorization='R') as cat_r,
sum(Categorization='Y') as cat_y
from have

group by subject;
quit;

art297
Opal | Level 21

I, too, think that sql would be the easiest.  However, if you prefer a datastep and your data are already sorted by subject, then you could use:

data want (keep=subject cat_:);

  set have;

  by subject;

  if first.subject then do;

    cat_y=0;

    cat_r=0;

  end;

  if categorization eq 'R' then cat_r+1;

  else if categorization eq 'Y' then cat_y+1;

  if last.subject then output;

run;

spirto
Quartz | Level 8

Thank you Art and Hai.kuo.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 3 replies
  • 1478 views
  • 3 likes
  • 3 in conversation