12-28-2017 08:46 AM
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:
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)
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
12-28-2017 09:59 AM
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;
do iteration = 1 to 100;
do subject = 1 to 50;
obsno = ceil(ranuni(12345) * _nobs_);
set unique_IDs point=obsno obs=_nobs_;
proc freq data=sample_IDs;
tables iteration * ID / noprint out=sample_map (keep=iteration ID count);
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;
merge longitudinal_data sample_map (where=(iteration=5) in=selected);
do _n_=1 to count;
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.