BookmarkSubscribeRSS Feed
Rajeshganta
Calcite | Level 5
I have a data like
Data
Set
Plan
View
As per my requirement the data has to get shuffled in a random way like any thing, may be like
Plan
Set
Data
View
12 REPLIES 12
PaigeMiller
Diamond | Level 26

Assign a random number to each row, then sort by the random number.

--
Paige Miller
Reeza
Super User
PROC SURVEYSELECT?
Watts
SAS Employee
proc surveyselect data=have out=shuffled
     rate=1 outorder=random;

 

See doc here: OUTORDER=RANDOM 

PaigeMiller
Diamond | Level 26

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

--
Paige Miller
Watts
SAS Employee

@PaigeMiller Thanks for correcting my typo.

Duggins
Obsidian | Level 7

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.

novinosrin
Tourmaline | Level 20

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 ;  
PaigeMiller
Diamond | Level 26

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

--
Paige Miller
novinosrin
Tourmaline | Level 20

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 ;
PaigeMiller
Diamond | Level 26

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;
--
Paige Miller
novinosrin
Tourmaline | Level 20

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

 

PeterClemmensen
Tourmaline | Level 20

Alternatively

 

proc sql;
   create table want as
   select * from sashelp.class
   order by rand('uniform');
quit;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 12 replies
  • 4128 views
  • 10 likes
  • 7 in conversation