Solved
Contributor
Posts: 46

# trying to figure out is how many instances of each postal code is affiliated with a hospital

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

Accepted Solutions
Solution
‎04-13-2018 11:54 AM
Super User
Posts: 13,583

## Re: trying to figure out is how many instances of each postal code is affiliated with a hospital

[ Edited ]

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;
```

All Replies
Super User
Posts: 9,599

## Re: trying to figure out is how many instances of each postal code is affiliated with a hospital

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;```
Contributor
Posts: 46

## Re: trying to figure out is how many instances of each postal code is affiliated with a hospital

This also worked really well, thanks so much.

Mike

Solution
‎04-13-2018 11:54 AM
Super User
Posts: 13,583

## Re: trying to figure out is how many instances of each postal code is affiliated with a hospital

[ Edited ]

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;
```

Contributor
Posts: 46

## Re: trying to figure out is how many instances of each postal code is affiliated with a hospital

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

☑ This topic is solved.