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 ?
What part is confusing you? What result are you expecting?
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.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.