Assign a random number to each row, then sort by the random number.
@Watts wrote:
proc surveyselect data=have out=shuffled; rate=1 outorder=random;
See doc here: OUTORDER=RANDOM
There is an error here, there should be no semi-colon after OUT=SHUFFLED
This solution fits my needs better than writing my own code, as I prefer to use SAS algorithms whenever possible (even in very simple cases), rather than writing my own, as I know SAS has taken the time to get it right and debugged it and tested it, and my own algorithms sometimes don't get it right.
@PaigeMiller Thanks for correcting my typo.
I agree with the PROC SURVEYSELECT suggestion. Setting your SAMPRATE to 100 ensures you get all the data back in your "sample" but the order will be randomized. Even if you had groups of data such as a training set, test set, and validation set, you could use stratified sampling in SURVEYSELECT to keep this a pretty efficient approach.
It's simple with RAND group of functions like @PaigeMiller suggested
/*Using SASHELP.CLASS*/
data want ;
do _n_ = 1 to n ;
p = ceil (rand ("uniform") * n);
set sashelp.class point=p nobs=n ;
output ;
end ;
stop ;
run ;
@novinosrin wrote:
It's simple with RAND group of functions like @PaigeMiller suggested
/*Using SASHELP.CLASS*/ data want ; do _n_ = 1 to n ; p = ceil (rand ("uniform") * n); set sashelp.class point=p nobs=n ; output ; end ; stop ; run ;
But data set WANT now has duplicates of some records, and other records are not present.
Sir @PaigeMiller Sorry, I overlooked the possibility of recurrence of the same random number. Good catch. But now, this tweak works, albeit boring
data want ;
array t [19] _temporary_ (1:19) ;
do until(_n=n) ;
_n_ = ceil(rand ("uniform")*n);
if t(_n_)=. then continue;
p = t(_n_);
set sashelp.class point=p nobs=n ;
_n+1;
output ;
t(_n_)=.;
end ;
stop ;
drop _:;
run ;
May I ask why you go to such a complicated logic rather than the much simpler (and easier to understand) logic I originally stated; or the SQL solution above from @PeterClemmensen?
data want;
set have;
z=rand('uniform');
run;
proc sort data=want;
by z;
run;
"why you go to such a complicated logic rather"- my dumb brain fails a lot of time. 😞
@PaigeMiller wrote:
May I ask why you go to such a complicated logic rather than the much simpler (and easier to understand) logic I originally stated; or the SQL solution above from @PeterClemmensen?
data want; set have; z=rand('uniform'); run; proc sort data=want; by z; run;
Alternatively
proc sql;
create table want as
select * from sashelp.class
order by rand('uniform');
quit;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.