This one had been bugging me for a while now. There are a lot of analyses where it is useful to select multiple random groups. Usually, this would involve picking a bunch of numbers out of your head and trying them as the seed values (I like using phone numbers without the area codes – then I can call the person and tell them they rocked my randomization).
But today, as I struggled with pulling a multitude of sample sets, I decided to come up with a more elegant solution for generating random number seed values. Behold the random loop:
%macro loopdiloop; %do i = 1 %to 250; %let seedno = %sysfunc(round(%sysfunc(rand(UNIFORM))*1000000,1)); %put &seedno; proc surveyselect data=basepopulation noprint out = outputset method = sys seed = &seedno sampsize = tochoose; strata stratify; run; %put &seedno; %put &i; /*Processing things here*/ proc sql; insert into trials select &seedno as seedno, stuff as outputvar, things as othervar from outputset; quit; %end; %mend; %loopdiloop;
Here’s the Step-by-Step:
- Declare your MACRO and name it.
- Set your DO loop for as many iterations as you want. In this example, it is set to 250.
- Set your seed value to a random calculation. Note that, because RAND outputs a decimal, you need to multiply it by something large and round it to an integer.
- Use the SURVEYSELECT procedure to select samples. Here, I have chosen to select a stratified random sample using the STRATA command.
- Display the current seed value and iteration using PUT commands. This is totally optional, but I find it nice to see where I am.
- Do whatever processing you need to do to the sample.
- APPEND or INSERT a summary of information into a table for later viewing. The “Trials” table here contains the seed value (so it can be referenced later if you need to reprocess or check anything or store out the sample it selected or whatever) as well as the key metrics needed, here called stuff and things.
- END the looping iterations and the macro! (Exclamation mark because it’s that important)
- Call the macro using its name.
- In this example, there are a number of elements that were previously processed and referenced in the code. For instance:
- tochoose was a set of numbers sampled from a control population so that the strata could be applied appropriately.
- The STRATA were already calculated and a variable created in both the test and control data sets on which to stratify the selection.
- When using the LET statement, you have to use %SYSFUNC for a couple of the functions in order to get it to evaluate. If you do not, you will store out the string of what was meant to be a command, which is not helpful as a random value.