Put Statement & Put Function

Reply
Frequent Contributor
Posts: 75

Put Statement & Put Function

What are both for? What is the difference between the 2?

Thanks

Super User
Super User
Posts: 6,497

Re: Put Statement & Put Function

PUT statement is for writing information to a file (or the SAS log or output destination).  It has a robust syntax that allows writing data in many ways.

PUT() function is for applying a format to a value to convert it into a character string. You can use it in expressions or an assignment statement.

Super Contributor
Posts: 644

Re: Put Statement & Put Function

To expand a little on Tom's explanation the PUT statement is used to write data from a SAS datastep into an object that is not a SAS dataset: usually an external file, or the SAS log or output areas.  It has the ability to write text strings and formatted values of datastep variables; with the ability to tab to a position or skip to a new line or new page.

The PUT() function is used to populate a variable within a SAS dataset or view with a value derived from an existing variable or expression so it needs much less functionality.  It is always used with a format specified as the second parameter, and always results in a character value whether the existing variable is numeric or character (though SAS may have to do a type conversion behind the scenes if you specify a numeric destination).

To derive a numeric value from an existing character value you need the INPUT() function with an appropriate informat.  To derive a numeric value from a numeric value you would need to nest a PUT() function inside an INPUT() function, or find a specific numeric function for the transformation you require.

There are also the PUTC() and PUTN() functions which are strictly for character and numeric expressions respectively, which you need to use with %SYSFUNC() in a macro expression. They can also be used in datastep code in the case where the format needs to be selected dynamically (eg other logic in the datastep determines which format to apply).

Richard temporarily back in Oz

Frequent Contributor
Posts: 114

Re: Put Statement & Put Function

Hi Richard,

Awesome explanation.... thanks for sharing knowledge.

Regards

Uma Shanker Saini

Occasional Contributor
Posts: 18

Re: Put Statement & Put Function

hi apple,

It looks like you require some sort of documentation for comprehensive understanding pf the basic concepts. I'd suggest you to google search for the free online book called step by step base sas prpgramming learning edition that has all that you need from the very basic to complex examples. Also, there is a beautiful book by author Ron Cody- SAS function by examples. I'd highly recommend referring the book.

hth,

Naveen

Frequent Contributor
Posts: 114

Re: Put Statement & Put Function

Hi,

If you are unable to get the free online book and some notes, Please feel free to write an email to me.

else TOM's explanation is very useful.

Regadrs
Uma Shanker Saini

Ask a Question
Discussion stats
  • 5 replies
  • 884 views
  • 3 likes
  • 5 in conversation