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 this code working correctly to get 4 records of each COW_ID by using all my dataset that contained 10 years of records, but I need to get 4 records for each cow_id for each year. 

my data called have 

I have the variable YEAR in this data

 

So How  I can to specify my requirement by using this code?

 

best regards 

 

barkamih 

PROC SQL;
create table want as
select *
from have
group by COW_ID
having count(*) > 3  ;
RUN;
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
PROC SQL;
create table want as
select *
from have
group by COW_ID, year
having count(*) > 3  ;
RUN;
group by COW_ID, year

View solution in original post

7 REPLIES 7
novinosrin
Tourmaline | Level 20
PROC SQL;
create table want as
select *
from have
group by COW_ID, year
having count(*) > 3  ;
RUN;
group by COW_ID, year
PGStats
Opal | Level 21

group by year, COW_ID

PG
novinosrin
Tourmaline | Level 20

Good catch 

PGStats
Opal | Level 21

Not a catch at all. I hadn't seen your submission before I posted. Both will give the same result, but sorted differently.

 

And both will be wrong if OP wanted cow_id's with 4 records in every year... Smiley Happy

PG
novinosrin
Tourmaline | Level 20

Oh Sir, I think you comprehended -", but I need to get 4 records for each cow_id for each year. "  this correctly

Barkamih
Pyrite | Level 9

 both of you are correct, the first reply get accepted and the second one got like, I'm trying to be equal for both of you hahaha, I hope I did. 

 

I appreciate your response

 

thanks million my friends 

 

 

 

 

novinosrin
Tourmaline | Level 20

It's all good. Just a thought, I think it;s better to end proc sql with quit; than run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 2037 views
  • 2 likes
  • 3 in conversation