BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Barkamih
Pyrite | Level 9

 

Hi all,

I have created this code in order to keep at least 5 cow_id for each herd per year. 

this code working perfectly across all years, 

 

what I need,  How to specify my requirement per year not across all years?  

 

data have has these three variables   cow_id,  herd and year. 

 

Regards 

 

proc sql;
create table herdcount as
select * from have
where herd in 
(select distinct herd from have group by herd having count(distinct cow_id) >= 5);
run;quit;
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

@Barkamih   Are you after this?

 

data have;
input cow_id       herd     year ;
cards;
1	1	2000
2	1	2000
3	1	2000
4	1	2000
5	1	2000
6	1	2000
7	1	2000
8	1	2000
9	2	2000
10	2	2000
11	2	2000
12	2	2000
1	1	2001
2	1	2001
3	1	2001
4	1	2001
;

proc sql;
create table want as
select *
from have
group by herd, year
having count(distinct cow_id)>=5;
quit;

Please post your expected output sample too 

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20

do you mean you need

 

group by herd, Year   ?

 

(select distinct herd from have group by herd,year having count(distinct cow_id) >= 5);

 

Barkamih
Pyrite | Level 9

yes, 

so that I just forgot to put year in the proper order. 

let see !

Barkamih
Pyrite | Level 9

I changed the code like this but it does not work, 

and this my data looks like this, 

  

proc sql;
create table herdcount as
select * from have
where year herd in 
(select distinct herd from have group by year herd having count(distinct cow_id) >= 5);
run;quit;

 cow_id       herd     year 

112000
212000
312000
412000
512000
612000
712000
812000
922000
1022000
1122000
1222000
112001
212001
312001
412001
novinosrin
Tourmaline | Level 20

@Barkamih   Are you after this?

 

data have;
input cow_id       herd     year ;
cards;
1	1	2000
2	1	2000
3	1	2000
4	1	2000
5	1	2000
6	1	2000
7	1	2000
8	1	2000
9	2	2000
10	2	2000
11	2	2000
12	2	2000
1	1	2001
2	1	2001
3	1	2001
4	1	2001
;

proc sql;
create table want as
select *
from have
group by herd, year
having count(distinct cow_id)>=5;
quit;

Please post your expected output sample too 

Tom
Super User Tom
Super User

Do you want the HERD*YEAR combinations with at least 5 cows?

The HERDs that have at least 5 cows in ANY year?

The HERDs that have at least 5 cows in ALL years for that HERD?

Here is an answer for that last question (although it does not put any constraints on the numbers of years of data for that HERD).

proc sql;
create table big_herds as
select * from have
where herd in 
  (select herd from 
    (select herd,year,count(distinct cow_id) as ncows 
    from have
    group by herd,year
    )
    group by herd
    having 5<=min(ncows)
  )
;
quit;

 

Barkamih
Pyrite | Level 9

 

yes, this is my question? 

The HERDs that have at least 5 cows in ANY year?

for example,  keep 5 cows or more for any herd and delete 4 cows or less per year

 

I submitted your edits but still something wrong !!! 

 

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1042 views
  • 1 like
  • 3 in conversation