BookmarkSubscribeRSS Feed
apple
Calcite | Level 5

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

 

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000199354.htm

 

Syntax

  PUT(source, format.)

 

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.
5 REPLIES 5
Loko
Barite | Level 11

Hello,

 

Further down in the function explanation you can find:

 

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

ballardw
Super User

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

Tom
Super User Tom
Super User

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.

Cynthia_sas
SAS Super FREQ

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:

PUT_function.png

 

cynthia

MichelleHomes
Meteorite | Level 14

You may find this SAS Sample note helpful too... http://support.sas.com/kb/24/590.html

 

Kind Regards,

Michelle

//Contact me to learn how Metacoda software can help keep your SAS platform secure - https://www.metacoda.com

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 20691 views
  • 4 likes
  • 6 in conversation