BookmarkSubscribeRSS Feed
HeatherNewton
Quartz | Level 8
data one;                                                                                                                               
   cc='abc';                                                                                                                              
   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;                                                                                                                       
   put cc=;                                                                                                                               
run;

I read this from the doc trying to understand how to use put but get more confused, what is it doing ?

3 REPLIES 3
PaigeMiller
Diamond | Level 26

What part is confusing you? What result are you expecting?

--
Paige Miller
Patrick
Opal | Level 21

The PUT Statement is writing the variable value to the output destination. If you don't specify it specifically via a FILE statement then it's writing to the SAS Log.

PUT <variable name>=; will also write the variable name to the output destination - <variable name>= <value>;

PUT <variable name>= <SAS format>; will write the value formatted. In your sample code as HEX value.

 

The PUT Function gets always used in conjunction with a SAS Format. It returns the formatted value that then gets assigned to the variable on the left side of the equal sign.

<new variable>=put(<variable>,<SAS format>);

 

More about the PUT Statement and the PUT Function in the SAS Documentation.

Tom
Super User Tom
Super User

So you create a character variable named CC that is three bytes long.

Then you used the PUT() function to convert the first two bytes of that variable into the 4 byte hexadecimal representation of those two bytes and used it to create a new variable CCHEX which will be 4 bytes long.

Then you wrote the value of the first two bytes of CC to the log, again using the 4 character hex code.  The value written to the log will be prefixed with the name of the variable and an equal sign.

You then re-wrote the same value into the variable CCHEX by repeating the same code as before.

The next two statements just control how the dataset ONE is created.  The three byte variable CC with is not written.  But the four byte variable CCHEX is written but it will be called CC, not CCHEX.

You then write the value of CC to the log, this time you write the whole value and do not convert it to hexadecimal codes.  Again the value is prefixed with the name of the variable and an equal sign.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 337 views
  • 1 like
  • 4 in conversation