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

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