BookmarkSubscribeRSS Feed
jgreenberg321
Fluorite | Level 6

I am working with a large dataset that contains data for many hospitals (marked by "ahaid") across several years (2010-2015). Year is marked by "pryear." Each row indicates a unique surgical visit. I am trying to calculate the number of surgical visits per hospital for each year. My goal is to output a new dataset that contains "ahaid," "pryear," and count variables for each year (count_10, count_11, count_12...). I want the count variables to represent the total number of surgical visits for that value of ahaid for the specified year. I believe I can combine do loops and "by" statements to accomplish this, but am not sure of exactly the right sequence. Can anyone help?

 

Thank you very much!

6 REPLIES 6
PaigeMiller
Diamond | Level 26

Use PROC FREQ instead of do loops.

 

proc freq data=have;
    tables ahaid*pryear;
run;
 
--
Paige Miller
jgreenberg321
Fluorite | Level 6
Thank you.

However I need to output the frequency values as new variables (one value per ahaid per pryear). Can I do that with the proc freq approach?

Thank you again!
PaigeMiller
Diamond | Level 26

@jgreenberg321 wrote:

However I need to output the frequency values as new variables (one value per ahaid per pryear). Can I do that with the proc freq approach?


Yes. Use the PROC FREQ options named LIST and OUT=

 

proc freq data=have;
    tables ahaid*pryear/list out=want noprint;
run;

 

--
Paige Miller
jgreenberg321
Fluorite | Level 6

Thank you! This seems to work well, though I expect the other solution proposed below would also work.

 

To organize my results by number of cases per year, I just used a transpose statement on the output dataset, though other solutions could have been as efficient or better. Thank you all for your help!

 

proc freq data=have;

table ahaid*pryear/list  out=want_prelim (drop=percent)  noprint;

run;

proc transpose data=want_prelim out=want_final (drop=_NAME_ _LABEL_);

by ahaid;

id pryear;

run;

CarlosSpranger
Obsidian | Level 7

Hello @jgreenberg321 ,

 

I was playing with your challenge and I have the following idea in attach

sas_snippet_cs_to_jgreenberg321.sas

Regards,

 

CS

CSB

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
  • 6 replies
  • 5173 views
  • 1 like
  • 3 in conversation