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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

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