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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 3992 views
  • 9 likes
  • 5 in conversation