Fluorite | Level 6

## sas 9.4 data step

% lets samplesize = 40;

%lets seed = 12345;

data want;

set have nobs=tot;

retain;

select = min(&samplesize, tot);

do I = Tot to 1 by -1;

set have;

if ranuni(&seed + (&I - 1)) < select /I then do;

select + - 1;

output;

end;

end;

run;

I am running this code to randomly select 40 customers from data have and getting following in my log:

Note: Limit set by errors=option reached. further errors of this type will not be printed. mathematical operations could not be performed at the following places. the results of the operators have been set to missing values.

this is not selecting 40 claims rather giving me everything whatever I have in 'have' data set.

Thanks,

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: sas 9.4 data step

Since you appear to be doing a form of simple random sample I might suggest:

```proc surveyselect data=have out=want
sampsize=&sampsize.   selectall
seed=&seed. stats   outall
;
run;```

The STATS option will show the selection probability and the sampleweight that you might want for any analysis using this data.

The SELECTALL says to take all of the observations if the actual size of the data set is smaller than the requested sample size.

The OUTALL option will have the output data set contain all records with a variable selected which =1 when selected and 0 otherwise.

8 REPLIES 8
Super User

## Re: sas 9.4 data step

From where do you get macro variable &i used in this statement:

``if ranuni(&seed + (&I - 1)) < select /I then do;``

😉

I guess it should be the data step variable i instead?

Fluorite | Level 6

## Re: sas 9.4 data step

Seed value I am getting from lets statement and &I value I am getting from a data step;

it is a macro

% lets samplesize = 40;

%lets seed = 12345;

%lets number = 1;

%macro want;

do I = 1 %to &number;

data want;

set have nobs=tot;

retain;

select = min(&samplesize, tot);

do I = Tot to 1 by -1;

set have;

if ranuni(&seed + (&I - 1)) < select /I then do;

select + - 1;

output;

end;

end;

run;

%mend want;

%want;

SAS Super FREQ

## Re: sas 9.4 data step

Hi:
I am surprised that your code works. The correct statement is %LET (no 's' on the end).
Cynthia
Fluorite | Level 6

## Re: sas 9.4 data step

Cynthia.

Sorry for that. That's a typo. I am not copying and pasting from my another computer. Sorry about that. There is no 's' in the let statement.

Super User

## Re: sas 9.4 data step

Since you appear to be doing a form of simple random sample I might suggest:

```proc surveyselect data=have out=want
sampsize=&sampsize.   selectall
seed=&seed. stats   outall
;
run;```

The STATS option will show the selection probability and the sampleweight that you might want for any analysis using this data.

The SELECTALL says to take all of the observations if the actual size of the data set is smaller than the requested sample size.

The OUTALL option will have the output data set contain all records with a variable selected which =1 when selected and 0 otherwise.

Fluorite | Level 6

## Re: sas 9.4 data step

Thanks. My seed value was too long for mathematical evaluation. I reduced it to 6 digits and that resolved the issue. Thank you so much. I appreciate all the prompt responses.

Super User

## Re: sas 9.4 data step

@Pooja2 wrote:

Seed value I am getting from lets statement and &I value I am getting from a data step;

it is a macro

% lets samplesize = 40;

%lets seed = 12345;

%lets number = 1;

%macro want;

do I = 1 %to &number;

data want;

set have nobs=tot;

retain;

select = min(&samplesize, tot);

do I = Tot to 1 by -1;

set have;

if ranuni(&seed + (&I - 1)) < select /I then do;

select + - 1;

output;

end;

end;

run;

%mend want;

%want;

This code is so full of syntax errors that it won't do anything. Get a simple data step to run correctly before you try anything harder; in particular, refrain from macro coding at all until you have a solid grasp of non-macro SAS coding.

I suggest you work through the free online Programming 1 course to learn the basics.

Super User

## Re: sas 9.4 data step

PS take a look if proc surveyselect would be a better solution.

Discussion stats
• 8 replies
• 799 views
• 2 likes
• 4 in conversation