BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
KJazem
Obsidian | Level 7

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?  

1 ACCEPTED SOLUTION

Accepted Solutions
sbxkoenk
SAS Super FREQ

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

 

View solution in original post

4 REPLIES 4
sbxkoenk
SAS Super FREQ

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

KJazem
Obsidian | Level 7
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.
sbxkoenk
SAS Super FREQ

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

 
Tom
Super User Tom
Super User

@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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1323 views
  • 2 likes
  • 3 in conversation