/***************************************************************************************************************** SAS file name: random_sampling_without.sas File location: __________________________________________________________________________________________________________________ Purpose: To demonstrate how to do random sampling in SAS using the Data Step and PROC SURVEYSELECT. Author: Peter Clemmensen Creation Date: 06/01/2020 This program supports the blog post "Random Sampling in SAS Without Replacement" on SASnrd.com *****************************************************************************************************************/ /* Example Data */ data have; input id x; datalines; 1 1 1 2 1 3 2 4 2 5 2 6 2 7 3 8 3 9 3 10 3 11 3 12 ; /* A Data Step Method */ data want(keep = id x); retain k 5; if _n_ = 1 then call streaminit (123); set have nobs = n; if rand ("uniform") < k/n then do; output; k = k-1; end; n = n-1; run; /* Another Data Step Method */ data want (keep = id x); array s {15} _temporary_ (1:15); call streaminit (123); h = n; do _n_ = 1 to 5; i = rand ("integer", h); p = s [i]; set have point=p nobs=n; output; s [i] = s [h]; h = h-1; end; stop; run; /* The same code as above, which prints the content of s for each sample */ data want (keep = id x); array s {15} _temporary_ (1:15); h = n; array_s = catq('d', '|', of s [*]); put "BEFORE SAMPLING: " / array_s = //; do _n_ = 1 to 5; i = rand ("integer", h); p = s [i]; set have point=p nobs=n; output; s [i] = s [h]; array_s = catq('d', '|', of s[*]); put h = "Replaces " i = "in s." / array_s = /; h = h-1; end; stop; run; /* PROC SURVEYSELECT */ proc surveyselect data=have out=want noprint method=srs sampsize=5; run;