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

Hello,

 

I am attempting the following for each observation in my dataset.

 

x=SelectRandomlyFrom("A","B","C")

 

That's it.  I can use ranuni or proc surveyselect to select one of the options but i can't figure out how to set x equal to the output.

 

it doesn't matter to me if my choices have to be in a dataset or simply a typed list.

 

Any help would be appreciated.

 

thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

@the_sheriff: The CHOOSEC is probably the easiest way in your example:

x=ChooseC(ceil(Rand('UNIFORM')*3),"A","B","C");

If your data is in an array, this is the format:

array values c1-c60;
x=ChooseC(ceil(Rand('UNIFORM')*dim(values)),of values(*));

I recommend using RAND() instead of RANUNI(), as it is a better pseudorandom algorithm.

 

View solution in original post

5 REPLIES 5
Yegen
Pyrite | Level 9

If you only want to select random As, Bs, and Cs, the following code will take care of it:

data work.want;
	do i=1 to 10;
	random_ABC = substr('ABC',ceil(ranuni(0)*3),1);
	output;
   	end;
run;
s_lassen
Meteorite | Level 14

@the_sheriff: The CHOOSEC is probably the easiest way in your example:

x=ChooseC(ceil(Rand('UNIFORM')*3),"A","B","C");

If your data is in an array, this is the format:

array values c1-c60;
x=ChooseC(ceil(Rand('UNIFORM')*dim(values)),of values(*));

I recommend using RAND() instead of RANUNI(), as it is a better pseudorandom algorithm.

 

the_sheriff
Calcite | Level 5
Always great to learn something new. This is exactly what i was looking for. Thank you.
Rick_SAS
SAS Super FREQ

You can generalize @s_lassen's solution to the case of unequal probabilities, as described in the blog post

"Simulate categorical data in SAS"

 

data Sample; 
keep i j x; 
array prob [3] (0.5 0.2 0.3);       /* unequal probabilities */
array values [3] $ ("A" "B" "C"); /* categories */
call streaminit(54321); 
do i = 1 to 100;
   j = rand("Table", of prob[*]);
   x = values[j];
   output; 
end; 
run;

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
  • 5 replies
  • 2574 views
  • 9 likes
  • 5 in conversation