BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
righcoastmike
Quartz | Level 8

Hi All, 

 

I have a data set that looks roughly like this: 

 

 

data hospital_by_pc;
   input ID Hospital Postcode;
   datalines;
1 11 M4T
2 11 M4T
3 11 M4R
3 11 M4N
4 12 D2F
5 12 D1J
6 12 D1J
7 14 M4T
8 14 D2F;

What I need to figure out is how many instances of each postal code is affiliated with each hospital. So the output would look roughly like: 

 

For hospital #11

2 from M4T

1 from M4R

1 from M4N

 

For Hospital #12 

1 from D2F

2 from D1J 

 

From Hospital #14

1 from M4T

1 from D2F

 

In essence I'm trying to figure out how many people from each postal code are going to each hospital. 

 

Any thoughts would be much appreciated! 

 

Thanks so much

 

Mike 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Another way if you want a human to read it and not use a resulting data set as input to something else would be one of the report or summarization procedures.

 

data work.hospital_by_pc;
   input ID Hospital  Postcode $;
   datalines;
1 11 M4T
2 11 M4T
3 11 M4R
3 11 M4N
4 12 D2F
5 12 D1J
6 12 D1J
7 14 M4T
8 14 D2F
;

proc tabulate data=work.hospital_by_pc;
   class hospital postcode;
   table hospital*postcode,
         n;
run;

proc freq data=work.hospital_by_pc;
   tables hospital*postcode/list nocum nopercent;
run;

Note changes to your data step so that postcode is actually read (your code had it numeric with character values) and the ; to end datalines has to be on a separate line from the end of the data.

 

Note that both of these approaches lend them selves to also generate a "which hospitals go with which code". Such as

proc tabulate data=work.hospital_by_pc;
   class hospital postcode;
   table hospital*postcode,
         n ;
   table postcode*hospital ,
         n ;
run;

 

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

There are numerous methods - means, summary, - but I tend to jump right at SQL for simple summary stats:

proc sql;
  create table want as
  select hospital,
         postcode,
         count(*) as want
  from   hospital_by_pc
  group by hospital,
           postcode;
quit;
righcoastmike
Quartz | Level 8

This also worked really well, thanks so much. 

 

your help is much appreciated! 

 

Mike 

 

 

ballardw
Super User

Another way if you want a human to read it and not use a resulting data set as input to something else would be one of the report or summarization procedures.

 

data work.hospital_by_pc;
   input ID Hospital  Postcode $;
   datalines;
1 11 M4T
2 11 M4T
3 11 M4R
3 11 M4N
4 12 D2F
5 12 D1J
6 12 D1J
7 14 M4T
8 14 D2F
;

proc tabulate data=work.hospital_by_pc;
   class hospital postcode;
   table hospital*postcode,
         n;
run;

proc freq data=work.hospital_by_pc;
   tables hospital*postcode/list nocum nopercent;
run;

Note changes to your data step so that postcode is actually read (your code had it numeric with character values) and the ; to end datalines has to be on a separate line from the end of the data.

 

Note that both of these approaches lend them selves to also generate a "which hospitals go with which code". Such as

proc tabulate data=work.hospital_by_pc;
   class hospital postcode;
   table hospital*postcode,
         n ;
   table postcode*hospital ,
         n ;
run;

 

righcoastmike
Quartz | Level 8

This is exactly what I was looking for. Thank you.

 

Also, lesson learned on the input code, thanks for pointing out the syntax problems. It really is like learning a new language. I'll get there eventually. 

 

Thanks again. 

 

Mike 

 

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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