HomeContact
Randomly select character values for each observation
February 01, 2021
1 min

In the example dataset - SASHELP.HEART, you want to create a new variable - Activity_status and assign one of three values of (‘High’, ‘MEDIUM’, ‘LOW’), at random to each observation. You can achieve these using 2 methods.

Method 1: Using an array

data heart(drop=id _1-_3 keep=status sex bp_status chol_status activity_status);
set sashelp.heart(obs=10);
array _{3} $ ('Low', 'Medium', 'High');
id=rand('integer', 1, dim(_));
activity_status=_;
run;```
The above method uses an [array](https://www.9to5sas.com/arrays-in-sas/) to hold the three-character values and creates three character variables - _1,_2,_3. The next statement `id=rand('integer',1,dim(_)) ` creates a variable holding random numbers between 1 and 3 and then activity_status variable is used to assign the corresponding values.
![](../../assets/2021/02/img_601794d3292bb.png)
## Method 2: Using CHOOSEC function
```sas
data heart (keep=status sex bp_status chol_status activity_status);
set sashelp.heart;
activity_status=choosec(ceil(ranuni(0)*3), "High", "Medium", "Low");
run;```
The first argument of the CHOOSEC function is a numeric value that specifies which character value to choose in the list of arguments that follow.
One way to generate a random integer from 1 to n is to first use the [RANUNI](https://documentation.sas.com/?docsetId=lefunctionsref&docsetTarget=p1fkiqt9ygapyxn1pd1w8manlpub.htm&docsetVersion=9.4&locale=en) function to generate a random value between 0 and 1. Next, you multiply this value by n to generate a random number between 0 and n.
Finally, the [CEIL](https://www.9to5sas.com/sas-numeric-functions/) function rounds up positive numbers to the next highest integer.
For example, if the random function generates a value of .1, 3 times .1 = .3. This, rounded to the next highest integer, gives you a 1. An alternative expression for a random integer from 1 to n is `(int(ranuni(0)*3 + 1)`.
Once you have generated a random integer from 1 to 3, the CHOOSEC function will return the name corresponding with that selection. ![](../../assets/2021/02/img_60178c2a56674.png)
export const _frontmatter = {"title":"Randomly select character values for each observation","slug":"randomly-select-character-values-for-each-observation","date":"2021-02-01T11:18:42","modified":"2021-08-04T09:08:55","excerpt":"In the example dataset - SASHELP.HEART, you want to create a new variable - Activity_status and assign one of three values of ('High', 'MEDIUM', 'LOW'), at random to each observation. You can achieve these using 2 methods. Method 1: Using an array data heart(drop=id _1-_3 keep=status sex bp_status chol_status activity_status); set sashelp.heart(obs=10); array _{3} $ [...]","author":"Subhro","authorSlug":"subhroster","categories":["SAS PROGRAMS"],"tags":[],"wordpressId":8580,"wordpressLink":"https://www.9to5sas.com/randomly-select-character-values-for-each-observation/","featuredImage":8581,"type":"post"}

Share


© 2025 9to5sas
AboutContactPrivacyTerms