Skip to content
9to5sas
  • Index
  • Glossary
Facebook Twitter Instagram Email RSS

  • Start Here
  • Base SAS
  • Advanced SASExpand
    • SAS Macros
    • PROC SQL
  • SAS/STATSExpand
    • SAS Analytics
    • Statistics
  • SAS Programs
9to5sas

9to5sas » SAS PROGRAMS » Randomly select character values for each observation

Randomly select character values for each observation

BySubhro Posted onFebruary 1, 2021August 4, 2021 Last Updated:August 4, 2021
0 Comments

In the example dataset – SASHELP.HEART, you want to create a new variable – Activity_status and assign one of three values of…

Randomly select character values for each observation

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=_[id];
run;

The above method uses an array 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.

Randomly select character values for each observation

Method 2: Using CHOOSEC function

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 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 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. Randomly select character values for each observation

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Check your inbox or spam folder to confirm your subscription.

Subhro

Subhro Kar is an Analyst with over five years of experience. As a programmer specializing in SAS (Statistical Analysis System), Subhro also offers tutorials and guides on how to approach the coding language. His website, 9to5sas, offers students and new programmers useful easy-to-grasp resources to help them understand the fundamentals of SAS. Through this website, he shares his passion for programming while giving back to up-and-coming programmers in the field. Subhro’s mission is to offer quality tips, tricks, and lessons that give SAS beginners the skills they need to succeed.

Facebook Twitter Linkedin

Post navigation

Previous Previous
Changing the Case of All Character Variables in a Data Set
NextContinue
SAS Data Access Functions

SAS Tips in your inbox

Subscribe to 9to5sas for timely SAS tips and news delivered each month.
Learn about the latest articles, and code samples to keep your SAS skills fresh!

Your subscription is successful!

Recent Posts

  • Concatenate strings in SAS: The CAT Functions Demystified
  • 5 Techniques for Quickly Removing Leading Zeros in SAS
  • Troubleshoot Your Proc SQL Code Like a Pro with These 7 Automatic Macro Variables
  • 7 PROC SQL Options You Should Use to Debug Queries
  • How To Use The SAS Proc SQL Order By Statement?
9to5sas
  • Privacy Policy
  • Disclaimer
  • About
  • Contact
Facebook Twitter Instagram RSS Email Pinterest

Copyright © 2023 9TO5SAS, All rights reserved.

Scroll to top
  • 9to5sas Blueprint
  • About
  • About
  • Acceptable use policy
  • calculator
  • Confirm email
  • Contact
  • Contact
  • Cookie Policy
  • DISCLAIMER
  • Getting Started with SAS
  • Glossary
  • Index
  • Post #13801
  • Privacy Policy
  • Privacy policy
  • SAS Programs
  • Styles
  • Subscription confirmed
  • Terms and conditions
  • Thank You