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
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;
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;
This also worked really well, thanks so much.
your help is much appreciated!
Mike
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;
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.