BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Pooja2
Fluorite | Level 6

% 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
ballardw
Super User

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.

View solution in original post

8 REPLIES 8
Kurt_Bremser
Super User

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?

Pooja2
Fluorite | Level 6

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;

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

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. 

ballardw
Super User

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.

Pooja2
Fluorite | Level 6

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.

Kurt_Bremser
Super User

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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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