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 a big dataset (cows data) and I would like to know how many herds in each county in each month.

 

my data looks like this;

 

 

data example;
  
datelines;
county    herd  month 
London    11    Jan
London    11    Jan
London    11    Jan
London    11    Jan
London    11    Jan
London    12    Fed
London    11    Fed
London    12    Fed
London    11    Fed
London    12    Fed
London    12    Fed
London    11    Mar
London    12    Mar
London    13    Mar
London    11    Mar
London    12    Mar
London    14    Mar

 

And what I need to count the frequency of herd in each month by county and the results should be like this;

county     month     number of herds 

London    Jan       1 Herd

London    Feb       2 herds

London     Mar      4 herds 

 

So, I need a code to count frequencies of herds for each month in each county.

 

What My code should look like?

 

best regards 

 

Ibrahim 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

data example;
input county $   herd  month $;
datAlines;
London    11    Jan
London    11    Jan
London    11    Jan
London    11    Jan
London    11    Jan
London    12    Fed
London    11    Fed
London    12    Fed
London    11    Fed
London    12    Fed
London    12    Fed
London    11    Mar
London    12    Mar
London    13    Mar
London    11    Mar
London    12    Mar
London    14    Mar
;

proc sql;
create table want as
select county,month, count (distinct herd) as num_of_herds
from example
group by county, month;
quit;

/*Or*/

proc sql;
create table want as
select county,month, catx(' ',count (distinct herd),'herds') as num_of_herds
from example
group by county, month;
quit;

 

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20

data example;
input county $   herd  month $;
datAlines;
London    11    Jan
London    11    Jan
London    11    Jan
London    11    Jan
London    11    Jan
London    12    Fed
London    11    Fed
London    12    Fed
London    11    Fed
London    12    Fed
London    12    Fed
London    11    Mar
London    12    Mar
London    13    Mar
London    11    Mar
London    12    Mar
London    14    Mar
;

proc sql;
create table want as
select county,month, count (distinct herd) as num_of_herds
from example
group by county, month;
quit;

/*Or*/

proc sql;
create table want as
select county,month, catx(' ',count (distinct herd),'herds') as num_of_herds
from example
group by county, month;
quit;

 

Barkamih
Pyrite | Level 9

 Thaaaaaaaaaaaaaaaaaaaaaaaaaaaanks million 

 

It's done 

 

working perfectly 

 

best wishes 

ballardw
Super User

Another approach is two proc freq calls:

proc freq data=example noprint;
tables county*herd*month/out=work.firstcount;
run;
proc freq data=work.firstcount;
tables county*month/list;
run;

However the sort order is based on text so Jan does not appear before Feb.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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