turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- set variable equal to a randomly chosen value from...

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

06-07-2017 02:09 AM

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.

Accepted Solutions

Solution

06-07-2017
10:28 AM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to the_sheriff

06-07-2017 06:56 AM

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

All Replies

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to the_sheriff

06-07-2017 02:31 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to the_sheriff

06-07-2017 02:43 AM

Solution

06-07-2017
10:28 AM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to the_sheriff

06-07-2017 06:56 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to s_lassen

06-07-2017 10:32 AM

Always great to learn something new. This is exactly what i was looking for. Thank you.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to the_sheriff

06-07-2017 08:56 AM

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