BookmarkSubscribeRSS Feed
OldSASGuy100
Fluorite | Level 6

I have a dataset with a natural hierarchy; observations within subjects. I wish to draw samples from this dataset using sampling at the subject level with replacement. My first So I decided to roll my own. I got all the bits working and tried to stitch everything together in a macro (some bits were data steps, some procedures) when I realized this was a dead end without being able to nest both datasets and procedures within a control loop in the highest level datastep (or macro). The procedures needed to execute and then return control to the macro. This led me to discover proc  FCMP which appeared to do exactly what I needed but seemed like a difficult approach. The following is a pseudocode description of what I was trying to do:

 

     start

         N = the number of subjects in the longitudinal dataset

         iterate over N

             use surveyselect to randomly draw a subject ID (with replacement)

             create a small dataset consisting of only the one subject and all the observations on said subject

             paste that dataset onto a growing dataset of observations (not all unique in subject id)

             end;

     end;

    use this dataset in another program (repeat many times)

 

I'm starting to think there must be a better way. Any help much appreciated using SAS 9.3

1 REPLY 1
Astounding
PROC Star

From the VeryOldSASGuy101 Course notes, here is an approach that pre-dates SURVEYSELECT.

 

First, create a SAS data set with ID only, and one observation per ID.  Then design your samples.  For example, if you want 100 samples, each with 50 subjects (with replacement):

 

proc sort data=longitudinal_data (keep=ID) out=unique_IDs nodupkey;

   by id;

run;

data sample_IDs;

do iteration = 1 to 100;

   do subject = 1 to 50;

      obsno = ceil(ranuni(12345) * _nobs_);

      set unique_IDs point=obsno obs=_nobs_;

      output;

   end;

end;

stop;

run;

 

proc freq data=sample_IDs;

tables iteration * ID / noprint out=sample_map (keep=iteration ID count);

run;

 

This doesn't give you the sampled data.  Rather, it gives you a list of which subjects to select, and how many times to select each, for each of 100 iterations ... all randomly sampled.

 

At this point, you might be ready for a macro that selects each sample and runs it through the hoops that you describe as a series of DATA and PROC steps.  Here's the idea without a macro, to select iteration #5:

 

proc sort data=longitudinal_data;

   by ID;

run;

data sample5;

   merge longitudinal_data sample_map (where=(iteration=5) in=selected);

   by ID;

   if selected;

   do _n_=1 to count;

      output;

   end;

run;

 

Depending on the nature of your intended processing, there might be ways to use a BY statement instead of extracting each sample separately.  But we would need to know more of the downstream processing logic to consider that.

 

Write back if you have questions, or need help converting the logic to a macro.

 

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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