Working with Character Variables |
Remember that SAS uses eight bytes of storage for every numeric value in the DATA step; by default, SAS also uses eight bytes of storage for each numeric value in an output data set. However, a character value can contain a minimum of one character; in that case, SAS uses one byte for the character variable, both in the program data vector and in the output data set. In addition, SAS treats the digits 0 through 9 in a character value like any other character. When you are not going to perform calculations on a variable, you can save storage space by treating a value that contains digits as a character value.
For example, some tours offer various prices, depending on the quality of the hotel room. The brochures rank the rooms as two stars, three stars, and so on. In this case the values 2, 3, and 4 are really the names of categories, and arithmetic operations are not expected to be performed on them. Therefore, the values can be read into a character variable. The following DATA step reads HotelRank as a character variable and assigns it a length of one byte:
data hotels; input Country $ 1-9 HotelRank $ 11 LandCost; datalines; Italy 2 498 Italy 4 698 Australia 2 915 Australia 3 1169 Australia 4 1399 ; proc print data=hotels; title 'Hotel Rankings'; run;
In the previous example, the INPUT statement assigns HotelRank a length of one byte because the INPUT statement reads one column to find the value (shown by the use of column input). If you are using list input, place a LENGTH statement before the INPUT statement to set the length to one byte.
If you read a number as a character value and then discover that you need to use it in a numeric expression, then you can do so without making changes in your program. SAS automatically produces a numeric value from the character value for use in the expression; it also issues a note in the log that the conversion occurred. (Of course, the conversion causes the DATA step to use slightly more computer resources.) The original variable remains unchanged.
The following output displays the results:
Saving Storage Space by Creating a Character Variable
Hotel Rankings 1 Hotel Land Obs Country Rank Cost 1 Italy 2 498 2 Italy 4 698 3 Australia 2 915 4 Australia 3 1169 5 Australia 4 1399
Note: Note that the width of the column is not the default width of eight.
Copyright © 2012 by SAS Institute Inc., Cary, NC, USA. All rights reserved.