Hi all
I just wanted to count the number of cow_id for each herd that I have in my dataset.
Data named: Fatd and I have variable herd and coe_id, and then I want to delete all herds that have less than 5 cow_id.
My regards
Ibrahim
Well, you could make the effort to provide a portion of the data so we can be see what you see.
Herd cow_id
10 101
10 101
10 101
10 101
10 101
10 101
10 101
11 102
11 102
11 102
My data looks like this, so I would like to keep only herds that have 5 or more cow_id.
data name : Fatd
data have;
input Herd cow_id;
cards;
10 101
10 101
10 101
10 101
10 101
10 101
10 101
11 102
11 102
11 102
;
proc sql;
create table want as
select *
from have
group herd
having count(cow_id)>=5;
quit;
So even though cow_id repeats (it is 101 for every cow), you would like to count this as 7 cows in the herd. Ok, that's pretty simple, assumes the data is sorted by herd.
proc summary data=have;
var cow_id;
class herd;
output out=counts n=count;
run;
data want;
merge have counts;
by herd;
if count<5 then delete;
run;
10 101
10 101
10 101
10 101
10 101
10 101
10 101
10 102
10 102
10 102
10 102
10 103
10 104
10 104
10 104
10 105
11 106
11 106
11 106
11 106
11 106
11 106
11 106
11 106
11 106
11 107
11 107
11 108
11 108
11 108
11 108
11 108
11 108
11 108
Not really what I'm looking for, but I will explain much more to you, I knew it was my fault I didn't give enough information.
If you look at this data, Herd 10 has 5 cows ID (101, 102, 103, 104, 105) with different time of measurements. And herd 11 has 3 cows ID (106, 107, 108), so in this case, I just want to keep herd 10 and delete herd 11, it does not matter how much each cow had been repeated.
I just care about the number of cows in each herd not about the number of measurements. (Repeated cow_id is a different time of measurements).
I hope you get my idea this time
I'm so sorry about the last post.
Regards
Ibrahim
proc sql;
Create table Want as
select Herd, Cow_Id, count(distinct Cow_Id) as cnt from Have
group by Herd
having calculated cnt ge 5;
quit;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: