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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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