Write and run SAS programs in your web browser

How to get a cluster sample with replacement

Reply
Occasional Contributor
Posts: 10

How to get a cluster sample with replacement

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

Super User
Posts: 5,713

Re: How to get a cluster sample with replacement

Posted in reply to OldSASGuy100

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.

 

 

Ask a Question
Discussion stats
  • 1 reply
  • 111 views
  • 0 likes
  • 2 in conversation