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;

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
  • 7 replies
  • 1433 views
  • 2 likes
  • 3 in conversation