PUT Function

Returns a value using a specified format.
Category: Special

Syntax

PUT(source, format.)

Required Arguments

source
identifies the constant, variable, or expression whose value you want to reformat. The source argument can be character or numeric.
format.
contains the SAS format that you want applied to the value that is specified in the source. This argument must be the name of a format with a period and optional width and decimal specifications, not a character constant, variable, or expression. By default, if the source is numeric, the resulting string is right aligned, and if the source is character, the result is left aligned. To override the default alignment, you can add an alignment specification to a format:
-L left aligns the value.
-C centers the value.
-R right aligns the value.
Restriction:The format. must be of the same type as the source, either character or numeric. That is, if the source is character, the format name must begin with a dollar sign, but if the source is numeric, the format name must not begin with a dollar sign.

Details

If the PUT function returns a value to a variable that has not yet been assigned a length, by default the variable length is determined by the width of the format.
Use the PUT function to convert a numeric value to a character value. The PUT function has no effect on which formats are used in PUT statements or which formats are assigned to variables in data sets. You cannot use the PUT function to directly change the type of variable in a data set from numeric to character. However, you can create a new character variable as the result of the PUT function. Then, if needed, use the DROP statement to drop the original numeric variable, followed by the RENAME statement to rename the new variable back to the original variable name.

Comparisons

The PUT statement and the PUT function are similar. The PUT function returns a value using a specified format. You must use an assignment statement to store the value in a variable. The PUT statement writes a value to an external destination (either the SAS log or a destination you specify).

Examples

Example 1: Converting Numeric Values to Character Value

In this example, the first statement converts the values of cc, a numeric variable, into the four-character hexadecimal format, and the second statement writes the same value that the PUT function returns.
cchex=put(cc,hex4.);
put cc hex4.;
If you need to keep the original variable name of cc, but as a character variable, then use the DROP and RENAME statements following the PUT function.
cchex=put(cc,hex4.);
drop cc;
rename cchex=cc;
The new cchex variable is created as a character variable from the numeric value of the cc variable. The DROP statement prevents the numeric cc variable from being written to the data set, and the RENAME statement renames the new character cchex variable back to the name of cc.

Example 2: Using PUT and INPUT Functions

In this example, the PUT function returns a numeric value as a character string. The value 122591 is assigned to the CHARDATE variable. The INPUT function returns the value of the character string as a SAS date value using a SAS date informat. The value 11681 is stored in the SASDATE variable.
numdate=122591;
chardate=put(numdate,z6.);
sasdate=input(chardate,mmddyy6.);

See Also

Statements:
PUT Statement in SAS Statements: Reference