BookmarkSubscribeRSS Feed
jmariee
Calcite | Level 5

I want to create a new variable based on the number of entries. 

 

This is a sample of my dataset. 

 

HospitalName        City                  State            CountyName

Name 1                  Columbia          Tx                Shelby 

 

 

I would want a new variable to see how many hospitals are in each county specific to their state. 

 

How would I be able to do this? 

 

4 REPLIES 4
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

Proc freg data=have out=want;

by state county;

quit;

 

 

 

http://support.sas.com/documentation/cdl/en/procstat/63104/HTML/default/viewer.htm#freq_toc.htm

 

then you can merge the results from want back to have.

Cynthia_sas
SAS Super FREQ

Hi:
This sounds like a simple PROC FREQ type of output. Do you need a data set or do you need a report?
I am guessing that your data set does not have just one row. So if you have a Texas County with 3 hospitals, would you expect to see a 3 in the new variable for each of the hospitals?

It's not clear to me what you would want to see populated in your new variable. In the fake data, below, Shelby county has 3 hospitals, Anderson county has 4 hospitals, Bexar county has 2 and there there are different hospitals in Ohio -- it sounds to me like you want a new data set, with a count variable. Perhaps something like this:

counties.png

The report on the right side shows output from PROC FREQ with the LIST option. Otherwise, I'm not sure how your original data would get the new variable populated on every row.

 

Cynthia


jmariee
Calcite | Level 5

Yes! I want a new data set with a count of how many hospitals there are in each county. Also for it to be categorized by state since some states have the same county names. 

Cynthia_sas
SAS Super FREQ

If you want a new data set, then you'd just need to create an output data set from PROC FREQ. I used the LIST option, so if your variables were named STATE and COUNTY as shown in my example, then:
output_data.png

and WORK.FRLIST would be the output data set.
Cynthia

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 499 views
  • 0 likes
  • 3 in conversation