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 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; 
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
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

 

View solution in original post

8 REPLIES 8
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

Astounding
PROC Star

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.

Reeza
Super User

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. 

 

 

Barkamih
Pyrite | Level 9

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 

Reeza
Super User

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 


 

 

Barkamih
Pyrite | Level 9
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
Reeza
Super User
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

 

Barkamih
Pyrite | Level 9
thanks very much my friend
I appreciate it
best regards

Ibrahim

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

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
  • 8 replies
  • 984 views
  • 0 likes
  • 4 in conversation