Hi all
I would like to know if this code is correct for my question.
I want to know the number of herds in each month for each county by using this code.
regards
Ibrahim
Proc freq data = have;
tables month*county*herd / out = want;
run; quit;
proc sql; create table want as select county, month, year, count(distinct herd) as n_herd from have group by county, month, year; quit;
@Barkamih wrote:
herd County month YEAR 59994724 Lim 3 2004 59994724 Lim 7 2004 59994724 Lim 4 2004 59994724 Lim 8 2004 59994724 Lim 9 2004 59994724 Lim 5 2004 59994724 Lim 6 2004 59994724 Lim 7 2005 59994724 Lim 10 2005 59994724 Lim 9 2005 59994724 Lim 12 2005 59994724 Lim 6 2005 59994724 Lim 8 2005 59994724 Lim 11 2005 59994724 Lim 8 2004 59994724 Lim 7 2004 59994724 Lim 6 2004 59994724 Lim 2 2004 59994724 Lim 5 2004 59994724 Lim 4 2004 59994724 Lim 2 2005 59994724 Lim 3 2005 59994724 Lim 5 2005 59994724 Lim 10 2005 59994724 Lim 9 2005 59994724 Lim 8 2005 59994724 Lim 7 2005 59994724 Lim 6 2005 60005584 Co 4 2004 60005584 Co 5 2004 60005584 Co 6 2004 60005584 Co 10 2004 60005584 Co 8 2004 60005584 Co 9 2004 60006084 Co 10 2004 60006084 Co 7 2004 60006084 Co 11 2004 60006084 Co 4 2004 60006084 Co 9 2004
So long as the by groups are in order (I would guess):
proc freq data=have;
tables county*month*herd / out=want;
run;
Note you don't need the quit;
No, not quite. It tells you number of times each herd appeared in each month/county. You would have to continue with:
proc freq data=want;
tables month*county / out=truly_want;
run;
That summarizes to the counts that you are looking for: number of herds for each month/county.
It depends on your data structure and data. If you have repeated measurements the answer is different than if you have a single list of entries.
Sure, that is my issue. For example, the county is repeated many times by year, as well as herds are repeated by year for each county. So in this case what is the code looks like?
regards
Post sample data please, fake data is fine.
@Barkamih wrote:
Sure, that is my issue. For example, the county is repeated many times by year, as well as herds are repeated by year for each county. So in this case what is the code looks like?
regards
herd County month YEAR
59994724 Lim 3 2004
59994724 Lim 7 2004
59994724 Lim 4 2004
59994724 Lim 8 2004
59994724 Lim 9 2004
59994724 Lim 5 2004
59994724 Lim 6 2004
59994724 Lim 7 2005
59994724 Lim 10 2005
59994724 Lim 9 2005
59994724 Lim 12 2005
59994724 Lim 6 2005
59994724 Lim 8 2005
59994724 Lim 11 2005
59994724 Lim 8 2004
59994724 Lim 7 2004
59994724 Lim 6 2004
59994724 Lim 2 2004
59994724 Lim 5 2004
59994724 Lim 4 2004
59994724 Lim 2 2005
59994724 Lim 3 2005
59994724 Lim 5 2005
59994724 Lim 10 2005
59994724 Lim 9 2005
59994724 Lim 8 2005
59994724 Lim 7 2005
59994724 Lim 6 2005
60005584 Co 4 2004
60005584 Co 5 2004
60005584 Co 6 2004
60005584 Co 10 2004
60005584 Co 8 2004
60005584 Co 9 2004
60006084 Co 10 2004
60006084 Co 7 2004
60006084 Co 11 2004
60006084 Co 4 2004
60006084 Co 9 2004
proc sql; create table want as select county, month, year, count(distinct herd) as n_herd from have group by county, month, year; quit;
@Barkamih wrote:
herd County month YEAR 59994724 Lim 3 2004 59994724 Lim 7 2004 59994724 Lim 4 2004 59994724 Lim 8 2004 59994724 Lim 9 2004 59994724 Lim 5 2004 59994724 Lim 6 2004 59994724 Lim 7 2005 59994724 Lim 10 2005 59994724 Lim 9 2005 59994724 Lim 12 2005 59994724 Lim 6 2005 59994724 Lim 8 2005 59994724 Lim 11 2005 59994724 Lim 8 2004 59994724 Lim 7 2004 59994724 Lim 6 2004 59994724 Lim 2 2004 59994724 Lim 5 2004 59994724 Lim 4 2004 59994724 Lim 2 2005 59994724 Lim 3 2005 59994724 Lim 5 2005 59994724 Lim 10 2005 59994724 Lim 9 2005 59994724 Lim 8 2005 59994724 Lim 7 2005 59994724 Lim 6 2005 60005584 Co 4 2004 60005584 Co 5 2004 60005584 Co 6 2004 60005584 Co 10 2004 60005584 Co 8 2004 60005584 Co 9 2004 60006084 Co 10 2004 60006084 Co 7 2004 60006084 Co 11 2004 60006084 Co 4 2004 60006084 Co 9 2004
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.