BookmarkSubscribeRSS Feed
Barkamih
Pyrite | Level 9

 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 

 

6 REPLIES 6
PaigeMiller
Diamond | Level 26

Well, you could make the effort to provide a portion of the data so we can be see what you see.

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

--
Paige Miller
Barkamih
Pyrite | Level 9

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 

novinosrin
Tourmaline | Level 20
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;
PaigeMiller
Diamond | Level 26

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;
--
Paige Miller
Barkamih
Pyrite | Level 9
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 

MadhuKorni
Quartz | Level 8

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;

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
  • 6 replies
  • 1176 views
  • 0 likes
  • 4 in conversation