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