/********************************************* THIS MACRO SELECTS n RANDOM SAMPLES FROM THE EMPLOYEE DATASET, AND MERGES (OR STACKS) THE RESULTING SAMPLE FILES TOGETHER. File = macro_random_sample_steps.sas **********************************************/ OPTIONS FORMCHAR="|----|+|---+=|-/\<>*"; libname mylib "C:\"; options mprint; /* STEP 1 - take 3 random samples (fixed n)*/ %macro sampler(); %do i=1 %to 3; proc surveyselect data = mylib.employee method = SRS sampsize = 25 out = employee_s&i; id _all_; run; %end; %mend; %sampler(); /* STEP 2 - Pass n as an argument to the macro */ %macro sampler(n); %do i=1 %to &n; proc surveyselect data = mylib.employee method = SRS sampsize = 25 out = employee_s&i; id _all_; run; %end; %mend; %sampler(4); /* STEP 3 - For each dataset, add variable to indicate the sample # */ %macro sampler(n); %do i=1 %to &n; proc surveyselect data = mylib.employee method = SRS sampsize = 25 out = employee_s&i; id _all_; run; data employee_s&i; set employee_s&i; sample=&i; run; %end; %mend; %sampler(5); /* STEP 4 - Merge all n datasets */ %macro sampler(n); %let filenames=; %do i=1 %to &n; proc surveyselect data = mylib.employee method = SRS sampsize = 25 out = employee_s&i; id _all_; run; %let filenames=&filenames employee_s&i; %put &filenames; data employee_s&i; set employee_s&i; sample=&i; run; %end; data merged; set &filenames; run; %mend; %sampler(6);