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

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
Obsidian | Level 7

Thank you Art and Hai.kuo.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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