An easy way to generate a vector of letters

3

A little-known but useful feature of SAS/IML 12.3 (which was released with SAS 9.4) is the ability to generate a vector of lowercase or uppercase letters by using the colon operator (:).

Many SAS/IML programmers use the colon operator to generate a vector of sequential integers:

proc iml;
x = 1:5;     /* increasing sequence {1 2 3 4 5} */
y = 9:5;     /* decreasing sequence {9 8 7 6 5} */

The colon operator is also useful for generating variable names that start with a common prefix and are enumerated consecutively:

varNames = "var1":"var5";  /* {"var1" "var2" "var3" "var4" "var5"} */

The new feature of the colon operator is the ability to generate a sequence of consecutive English letters:

lower = "a":"z";    /* 26 lowercase letters */
upper = "A":"Z";    /* 26 uppercase letters */
s = "p":"k";        /* {"p" "o" "n" "m" "l" "k"} */
u = "U":"Z";        /* {U V W X Y Z} */

This feature is useful when you want to quickly name the levels of a categorical variable. For example, the following SAS/IML statements sample with replacement from four categories named A, B, C, and D:

call randseed(1234);
x = sample("A":"D", 10); /* 10 draw with replacement from {A B C D} */
print x;
t_letters

Can you think of a way to use this feature in your work? Leave a comment.

Share

About Author

Rick Wicklin

Distinguished Researcher in Computational Statistics

Rick Wicklin, PhD, is a distinguished researcher in computational statistics at SAS and is a principal developer of SAS/IML software. His areas of expertise include computational statistics, simulation, statistical graphics, and modern methods in statistical data analysis. Rick is author of the books Statistical Programming with SAS/IML Software and Simulating Data with SAS.

3 Comments

  1. Peter Lancashire on

    This would be useful for labelling means which are not significantly different in a multiple comparison test. SAS has procedures which do this. I use a sequence of letters to make a "fingerprint" of a permutation of numbers. I use the fingerprints to check for duplicated randomisations.

    Which collating sequence is used for this operator? For example, what happens in Cyrillic, as used to write Russian? Or Chinese?

  2. Pingback: Overview of new features in SAS/IML 12.3 - The DO Loop

Leave A Reply

Back to Top