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.
@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.
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;
@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.
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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.