DATA Step, Macro, Functions and more

Put Function

Frequent Contributor
Posts: 76

Put Function

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? 

Thank you



  PUT(source, format.)




identifies the constant, variable, or expression whose value you want to reformat. The source argument can be character or numeric.


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:


left aligns the value.


centers the value.


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.
Super Contributor
Posts: 319

Re: Put Function



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.

Super User
Posts: 13,563

Re: Put Function

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.);

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

Super User
Super User
Posts: 8,115

Re: Put Function

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.

Posts: 9,370

Re: Put Function

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;
y = today();
z = 'KTF';
y=today() - 1;
y=today() -30;
proc print data=showstart;
  title '1) Beginning data, no formats, before using PUT function';
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';
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);
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;


Here's the output:




Trusted Advisor
Posts: 1,403

Re: Put Function

You may find this SAS Sample note helpful too...


Kind Regards,


Ask a Question
Discussion stats
  • 5 replies
  • 6 in conversation