I have a macro that takes a random sample of a dataset at every run and outputs a table. For example, if the random sample size is 1000, and if the source table somehow has fewer than 1000 records, it takes all the records.
%let _sampsize = <code that chooses a sample size based on rules>
proc surveyselect data=CAD.SOURCE out=CAD.RAND_DEST
method=srs sampsize=&_sampsize. selectall;
run;
But if the _samplesize macro variable is for whatever reason, zero, the code breaks and says sampsize has to be a positive integer. What would be the best/most reliable way to proof this?
I would prefer to return an empty table if the sample size is zero. I guess I check against sample size first, but is there a more 'elegant' way?
This would work :
if &_sampsize. = 0 then do;
data CAD.RAND_DEST;
set CAD.SOURCE;
IF 0; /* subsetting IF , but never true */
run;
end;
Koen
Just enhance your macro logic such that the PROC SURVEYSELECT with a zero sample size will never be executed.
You know how to do this?
Koen
This would work :
if &_sampsize. = 0 then do;
data CAD.RAND_DEST;
set CAD.SOURCE;
IF 0; /* subsetting IF , but never true */
run;
end;
Koen
@KJazem wrote:
Yes, this is my first thought. But I would like to return an empty table instead.
if &_sampsize. = 0 then do;
data CAD.RAND_DEST;
set CAD.SOURCE;
run;
end;
Would this work? Each output table (random sample) will be appended to main table.
If the goal is to APPEND observations to a table and you want zero observations appended, then just skip the APPEND step.
If you want to generate a dataset with zero observations you could add a STOP statement.
data CAD.RAND_DEST;
stop;
set CAD.SOURCE;
run;
Or the OBS=0 dataset option.
data CAD.RAND_DEST;
set CAD.SOURCE (obs=0);
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.