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

I have a dataset where the same sample ID is associated with two components (a and b), it also contains singular data (c). I'm trying to add a column that counts the number of occurrences of the ID so I can identify when I only have one of the two components. I'm trying to create the Count column in the table below so I could identify the unpaired component (in red text) using an if then statement (if Comp eq "a" and Count ne 2 then) and (if Comp eq "b" and Count ne 2 then).

 

CompIDCount
c52361
a32982
b32982
a41721
a87232
b87232
c76921

 

If it were in Excel, it could be done using the COUNTIF function and dragging it down (=COUNTIF(B:B,B2)) Unfortunately I don't even know where to begin to do this in SAS and haven't had any luck Googling it. I would appreciate any direction you could give me.

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

In my mind, counting questions are best handled by PROC FREQ.

 

proc freq data=have;
     tables id/noprint out=counts;
run;

data want;
    merge have counts(keep=id count);
    by id;
run;

 

You have to sort data set HAVE by ID in order for this to work.

--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

In my mind, counting questions are best handled by PROC FREQ.

 

proc freq data=have;
     tables id/noprint out=counts;
run;

data want;
    merge have counts(keep=id count);
    by id;
run;

 

You have to sort data set HAVE by ID in order for this to work.

--
Paige Miller
rstuart931
Calcite | Level 5

@PaigeMiller Thank you, that worked great!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2 replies
  • 424 views
  • 1 like
  • 2 in conversation