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

For the attached sample dataset, I would like to:

 

1) generate a variable "random_value_norepeat" which equals to a randomly selected number from the variable "value" (any number from the "value" can only be selected for one time, or put it another way, sample without replacement), and

 

2) generate another variable "random_value_repeat" which also equals to a randomly selected number from the variable "value", but the number from the "value" can be selected more than once (sample with replacement).

 

Many thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

All the information you need is at 

http://blogs.sas.com/content/iml/2016/02/15/essential-sampling-methods.html

 

I believe you have to generate each variable separately.  It is not clear if you want all 50 observations or a subset. It is also not clear if you need the values in random order or in the original order.  The easiest way is to use the SAMPLE function in SAS/IML, but since you posted to the Base SAS Community, here is a PROC-based solution to get you started:

 

libname temp "C:\Temp";
data Have;
set temp.have;
call streaminit(1);
u = rand("uniform");
run;

/* without replacement (SRS) */
proc sort data=Have out=SRS(rename=(value=random_value_norepeat));
by u;
run;

/* with replacement (URS) */
proc surveyselect noprint data=temp.Have
     method=urs  
     seed=2
     samprate=1
     outhits
     out=URS(rename=(value=vandom_value_repeat));
run;

/* if you want URS in random order, then generate random uniform and sort */

View solution in original post

3 REPLIES 3
PGStats
Opal | Level 21

proc surveyselect does both. method=srs selects without replacement, method=urs does so with replacement.

PG
Rick_SAS
SAS Super FREQ

All the information you need is at 

http://blogs.sas.com/content/iml/2016/02/15/essential-sampling-methods.html

 

I believe you have to generate each variable separately.  It is not clear if you want all 50 observations or a subset. It is also not clear if you need the values in random order or in the original order.  The easiest way is to use the SAMPLE function in SAS/IML, but since you posted to the Base SAS Community, here is a PROC-based solution to get you started:

 

libname temp "C:\Temp";
data Have;
set temp.have;
call streaminit(1);
u = rand("uniform");
run;

/* without replacement (SRS) */
proc sort data=Have out=SRS(rename=(value=random_value_norepeat));
by u;
run;

/* with replacement (URS) */
proc surveyselect noprint data=temp.Have
     method=urs  
     seed=2
     samprate=1
     outhits
     out=URS(rename=(value=vandom_value_repeat));
run;

/* if you want URS in random order, then generate random uniform and sort */

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 3 replies
  • 8100 views
  • 3 likes
  • 4 in conversation