DATA Step, Macro, Functions and more

variable counting

Reply
Contributor
Posts: 67

variable counting

 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 

 

Respected Advisor
Posts: 2,988

Re: variable counting

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
Contributor
Posts: 67

Re: variable counting

[ Edited ]
Posted in reply to PaigeMiller

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 

PROC Star
Posts: 1,772

Re: variable counting

[ Edited ]
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;
Respected Advisor
Posts: 2,988

Re: variable counting

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
Contributor
Posts: 67

Re: variable counting

Posted in reply to PaigeMiller
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 

Frequent Contributor
Posts: 77

Re: variable counting

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;

Ask a Question
Discussion stats
  • 6 replies
  • 90 views
  • 0 likes
  • 4 in conversation