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
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;
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;
Thaaaaaaaaaaaaaaaaaaaaaaaaaaaanks million
It's done
working perfectly
best wishes
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.
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!
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.
Ready to level-up your skills? Choose your own adventure.