03-25-2016 11:17 AM
Can someone explain what the use of the Put function? I thought it converts numeric data values to character data values.
But I read in the below link that it only re-formats. Since the format must be of the same type as the source, either character or numeric. How does it convert a numeric to a character?
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:
|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.|
03-25-2016 11:23 AM
Further down in the function explanation you can find:
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 PUT 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 change the type of a variable in a data set from numeric to character.
03-25-2016 11:26 AM
The Put function has two different behaviors depending on use.
A simple Put statement writes data to the default output destination (log or destination specified in a File statement).
The other is to create a character value, there will usually be an = involved here.
Newvar = put(somevariablename, UsingThisFormat.);
For a fun example try this code:
data junk; x = 25; newx = put(x,date9.); run;
If you run Proc Contents or use the SAS Explorer to examine the Junk data set you will see that NewX is a charater variable.
if a format isn't specified SAS will make a guess. The result is character.
You can use put in comparison:
If ( put(somevariablename, UsingThisFormat.) ='Some formatted value' ) then do; <some code>; end;
It can create a different character value from a character depending on the nature of the specific format used.
If you want t
03-25-2016 11:50 AM
The PUT() function applies a format to a value. Since formats are used for displaying values in output they always return character strings. This is similar to using the PUT statement to write out data. You can apply a numeric format to a numeric value or apply a character format to a character value, but either way the result is a character string.
The INPUT() function can by used to convert a character string to either a numeric or character value based on the informat that is used. This is similar to reading data using the INPUT statement.
03-25-2016 01:42 PM
Here's an example of the PUT function to perform these conversions:
1) numeric variable to character variable
2) character variable to character variable
Of course the most frequent use of the PUT function is to convert a numeric value to a character string to create a new character variable.
Here's the code:
data showstart; x=1; y = today(); z = 'KTF'; output; x=2; y=today() - 1; z='E'; output; x=3; y=today() -30; z='MP'; output; run; proc print data=showstart; title '1) Beginning data, no formats, before using PUT function'; run; proc format; value xfmt 1='1. First, on' 2='2. Yesterday, on' 3='3. Before that, on'; value $zfmt 'KTF' = 'Kermit the Frog' 'E' = 'Elmo' 'MP' = 'Miss Piggy'; run; data newput; length newx $50 newy $50 newz $50 sentence $90; set showstart; ** numeric to character; newx = put(x,xfmt.); ** numeric to character using SAS date format; newy = put(y, worddate.); ** character to character; newz = put(z, $zfmt.); sentence = catx(' ',newx, newy, "Jane's favorite stuffed animal was", newz); run; proc print data=newput; title '2) After making new variables with PUT and using the new variables to make another variable'; var x y z newx newy newz sentence; run;
Here's the output: