Suppose you have an some empirical data in a SAS data set, say 3 numbers: {1, 3, 9}
I'd like to draw numbers from that data until the sum is greater than some number (say 15) and save those numbers.
Is there an easy (and efficient) way to do that in SAS? 9.4 Would I be better off switching to Rcpp?
Thanks!
Hello @dschmidt,
Try this:
data have;
do x=1, 3, 9;
output;
end;
run;
%let t=15; /* threshold */
data want;
call streaminit(27182818);
do _n_=1 to 999999 until(s>&t); /* "999999" to prevent infinite loop */
p=rand('integer',n);
set have point=p nobs=n;
s+x;
output;
end;
stop;
drop s; /* optional */
run;
If the 'integer' distribution is not yet available in your maintenance release, use
p=ceil(n*rand('uniform'));
Edit: The subject line ("while total is less than") is not exactly equivalent to the requirement described as "until the sum is greater than," which I used in the UNTIL condition of the DO loop.
A 'simple' approach is to randomly sort the data and take the first N records until the sum is over the N.
%let N_Limit = 40;
data _random;
set sashelp.class;
*add random variable;
order = rand('integer', 1, 100);
run;
proc sort data=_random;
by order;
run;
data want;
set _random;
retain total;
if total<&N_Limit then output;
*increment total after to allow the total to go over;
total+age;
run;
@dschmidt wrote:
Suppose you have an some empirical data in a SAS data set, say 3 numbers: {1, 3, 9}
I'd like to draw numbers from that data until the sum is greater than some number (say 15) and save those numbers.
Is there an easy (and efficient) way to do that in SAS? 9.4 Would I be better off switching to Rcpp?
Thanks!
This is a convenient approach for the situation of sampling without replacement.
My suggestion was rather aimed at sampling with replacement -- in view of @dschmidt's example of obtaining a sum >15 from the 3-element set {1, 3, 9}.
Thanks! What OR procedure would you refer to? I had that thought, but didn't see any obvious candidates.
If it helps, this is something that I'd like to repeat many times for many lists (with replacement)
E.g.
For iteration 1:num_iterations
For lists 1:num_lists
Draw a random value from the list if count < threshold
Count = count + random draw from the list
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.