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 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 5830 views
  • 1 like
  • 3 in conversation