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 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 3 replies
  • 331 views
  • 1 like
  • 3 in conversation