BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
akj
Fluorite | Level 6 akj
Fluorite | Level 6

I have a large file of inpatient stays that I need to split into smaller files (approx. 20,000 observations) for operational purposes.  A patient (patient_id) may have one or more observations in the data set, these observations need to be kept together in the new smaller datasets, i.e., I can't split the large dataset into the first 20000 obs and then the second 20000 and so on.

 

How can I make sure to keep the observations for a particular patient (patient_ID) together?

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Sort by patient_id.

Run this data step:

data intermediate;
set have end=eof;
by patient_id;
retain counter 0 ds_counter 1;
counter + 1;
if first.patient_id and counter ge 20000
then do;
  ds_counter + 1;
  counter = 1;
end;
if end then call symputx('num_ds',ds_counter);
drop counter;
run;

You now have the maximum number of datasets in the macro variable (for use in a %do loop), and an indicator in every observation where it should go.

View solution in original post

1 REPLY 1
Kurt_Bremser
Super User

Sort by patient_id.

Run this data step:

data intermediate;
set have end=eof;
by patient_id;
retain counter 0 ds_counter 1;
counter + 1;
if first.patient_id and counter ge 20000
then do;
  ds_counter + 1;
  counter = 1;
end;
if end then call symputx('num_ds',ds_counter);
drop counter;
run;

You now have the maximum number of datasets in the macro variable (for use in a %do loop), and an indicator in every observation where it should go.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1 reply
  • 1218 views
  • 0 likes
  • 2 in conversation