Sample 51386: Create character variables that contain random alphabetic values
It is a common practice to use the RANUNI function to generate random numbers within a SAS® program. These random numbers are real numbers between 0 and 1. To generate random alphabetic values, the program requires a few additional lines of logic in addition to the RANUNI function.
To see the program, click the Full Code tab. To see the results, click the Output tab.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
This program illustrates how to create random alphabetic values with the RANUNI and BYTE functions. Each random number is between 65 and 90 because the BYTE function maps these decimal values to the capital letters A-Z in the ASCII collating sequence. The SUBSTR function places one random alphabetic character in positions 1, 2, and 3 of variable Final. Both DO loops and the LENGTH statement can be adjusted to create a longer width variable as well as additional observations.
data a(keep=final);
length final $3;
do i=1 to 10;
do j=1 to 3;
substr(final,j)=byte(int(65+26*ranuni(0)));
end;
output;
end;
run;
proc print;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
/* The values will vary each time this code is run because the seed value of 0 */
/* produces a unique set of random numbers between 65 and 90. */
Obs final
1 SHU
2 NKO
3 BNZ
4 BAV
5 RWY
6 UJR
7 YYQ
8 ALU
9 EWF
10 PLY
Type: | Sample |
Topic: | SAS Reference ==> DATA Step
|
Date Modified: | 2013-10-31 12:18:02 |
Date Created: | 2013-10-17 13:16:17 |
Operating System and Release Information
SAS System | Base SAS | Microsoft® Windows® for 64-Bit Itanium-based Systems | 9.1 TS1M0 | |
Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9.1 TS1M0 | |
Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9.1 TS1M0 | |
Microsoft Windows 2000 Advanced Server | 9.1 TS1M0 | |
Microsoft Windows 2000 Datacenter Server | 9.1 TS1M0 | |
Microsoft Windows 2000 Server | 9.1 TS1M0 | |
Microsoft Windows 2000 Professional | 9.1 TS1M0 | |
Microsoft Windows NT Workstation | 9.1 TS1M0 | |
Microsoft Windows Server 2003 Datacenter Edition | 9.1 TS1M0 | |
Microsoft Windows Server 2003 Enterprise Edition | 9.1 TS1M0 | |
Microsoft Windows Server 2003 Standard Edition | 9.1 TS1M0 | |
Microsoft Windows XP Professional | 9.1 TS1M0 | |
64-bit Enabled AIX | 9.1 TS1M0 | |
64-bit Enabled HP-UX | 9.1 TS1M0 | |
64-bit Enabled Solaris | 9.1 TS1M0 | |
HP-UX IPF | 9.1 TS1M0 | |
Linux | 9.1 TS1M0 | |
OpenVMS Alpha | 9.1 TS1M0 | |
Tru64 UNIX | 9.1 TS1M0 | |