|
The SAS System always prints numbers between 0 and 1 with leading zeros. How can I eliminate the leading zeros? Use the PICTURE statement in PROC FORMAT. The PICTURE statement enables you to create a format that acts as a template for printing the values of numeric variables. To illustrate, we'll use a PICTURE statement to create a format. First, let's use the following statements to create and print the data set for our example:data sample; Output 3 Printing the Numbers with the Default Format The following PICTURE statement creates a user-defined format that PROC PRINT uses to print the numbers without leading zeros:
proc format;
picture nozeros
low - -1 = '00.00' (prefix='-' )
-1 <-< 0 = '99' (prefix='-.' mult=100)
0 -< 1 = '99' (prefix='.' mult=100)
1 - high = '00.00';
proc print data=sample noobs;
format amount nozeros.;
Output 4 Printing the Numbers with the NOZEROS. Format To understand why we wrote the NOZEROS. format this way, you need to understand how the PICTURE statement works. Figure 1 shows how a PICTURE statement format formats a variable value. The table that follows the figure provides an explanation for each step. The circled numbers in the figure correspond to the circled numbers in the table. In the following table, the Rule column gives the rules that enable you to predict formatted values. The Our Example column contains comments on how the corresponding rule relates to our example. Figure 1 Formatting One Value in Each Range Step Rule Our Example Determine into which range the value falls and use that picture. Notice in the second range that the operator < appears on both sides of the hyphen and excludes Š1 and 0 from the range. Take the absolute value of the numeric variable value. Treat all data values as positive values; ignore the sign. Because we ignore the negative sign, we need a separate range and picture for the negative numbers in order to prefix the minus sign.
Multiply the number by the MULT= value. If you do not specify the MULT= option, the
PICTURE statement uses the default. The default is 10n, where n is the number
of digit selectors to the right of the decimal in the picture.* (Digit selectors are numeric
characters 0 through 9 that Specifying a MULT= value is necessary for numbers between 0 and 1 and numbers between 0 and -1 because no decimal appears in the pictures for those ranges. Since MULT= defaults to 1, truncation of the significant digits results without a MULT= value specified. (Truncation is explained in the next step.) For the two ranges that do not have MULT= values specified, the MULT= value defaults to 100 since the corresponding picture has two digit selectors to the right of the decimal. After the MULT= value is applied, all significant digits are moved to the left of the decimal. Truncate the number after the decimal. If the ROUND option is in effect, the format rounds the number after the decimal to the next highest integer if the number after the decimal is greater than or equal to .5. Because we used MULT= values that ensured that all of our significant digits were moved to the left of the decimal, we don't have to worry about losing any significant digits. The zeros are truncated.
Turn the number into a character string. If the number is shorter than the picture, the length of the character string is equal to the number of digit selectors in the picture. Pad the character string with leading zeros. (The results are equivalent to using the Zw. format. Zw. is explained in Chapter 14 in SAS Language: Reference, Version 6, First Edition.) The numbers 205, 5, and 660 become the character strings 0205, 05, and 0660, respectively. Because each picture is longer than the numbers, the format adds a leading zero to each value. The format does not add leading zeros to the number 55 because the corresponding picture only has two digit selectors. Apply the character string to the picture. The format only maps the rightmost n characters in the character string, where n is the number of digit selectors in the picture. Thus, it is important to make sure that the picture has enough digit selectors to accommodate the characters in the string. After the format takes the rightmost n characters, it then maps those characters to the picture from left to right. Choosing a zero or nonzero digit selector is important if the character string contains leading zeros. If one of the leading zeros in the character string maps to a nonzero digit selector, it and all subsequent leading zeros become part of the formatted value. If all of the leading zeros map to zero digit selectors, none of the leading zeros become part of the formatted value; the format replaces the leading zeros in the character string with blanks.**
The leading zero is dropped from each of the character strings 0205 and 0660 because the
leading zero maps
Prefix any characters specified in the PREFIX= option.
You need the PREFIX= option because when a picture contains any digit selectors, the
picture must begin with We use the PREFIX= option to reclaim the decimal point and the negative sign, as shown with the formatted values Š.05 and .55.
Table 1 Answer provided by Philip Shelton and Jason Sharpe. Philip is a writer in the base product documentation department at SAS Institute. Jason is a technical support analyst at SAS Institute, specializing in base SAS procedures and the macro facility. |