Reputation: 763
Following is an example of the data-set I have
data have;
input institution$ GPA;
cards;
A 3.2
AB 3.4
BC 4.0
DF 3.2
A 4.0
A 3.0
A 3.5
A 3.7
A 3.8
F 3.8
D 3.2
D 3.1
D 3.7
;
run;
Essentially I want to create a macro that selects a random sample from this data set (the actual dataset is much larger). I also want to simulate this sampling procedure, so that for e.g I can do 50 sampling iterations and for each iteration, I get a output data set. I tried creating the following macro to do this
%macro beta (maxj=,datain= ,numofsamples= , dataout=,);
%do j=1 %to &maxj;
proc surveyselect data=&datain
method=srs n=&numofsamples out=&dataout_&maxj;
run;
%end;
%mend;
%beta (maxj=4 ,datain=have ,numofsamples=5,dataout=pool);
Essentially when I run this, want to get outputs data sets so that they are named pool_1,pool_2,pool_3 etc.
However, when I run this macro it doesn't seem to do what I intended. Instead it shows that there is something wrong with the &dataout_&maxj; run;
line
Upvotes: 2
Views: 347
Reputation: 6378
When you code:
&dataout_&maxj
SAS will see a macro reference to macro variable named DATAOUT_ and a reference to macro variable MAXJ. You don't have a macro variable named DATAOUT_ so that reference will not resolve. You have a macro variable DATAOUT with no underscore. And also looks like you want macro variable J as the suffix, not MAXJ. So your macro should work if you change it to:
out=&dataout&j
If you want the underscore in the name of the output dataset, you can use a dot to end the macro variable reference:
out=&dataout._&j
That said, with PROC SURVEYSELECT you should not need to code this looping yourself. You can use the NREP option to pull multiple samples. This should be much more efficient than calling PROC SURVEYSELECT N times. So your macro could be like:
%macro beta (datain=, n=, reps=1, dataout=);
proc surveyselect data=&datain
method=srs n=&n reps=&reps out=&dataout;
run;
%mend;
*Pull one sample of 5 records ;
%beta(datain=have,n=5,dataout=want)
*Pull three samples of 5 records ;
%beta(datain=have,n=5,reps=3,dataout=want)
Upvotes: 2