So here is my problem. I have a numeric variable that is either 0, 500, 600, or 660. Given that this is a categorical variable, I would like it to change it to a character but can't seem to get it to work. I've gotten close using the code below:
TRIM(PUT(SERIES,3.)) AS SERIES,
This didn't give me my desired result. It was a character, but now the 0's were "space"space"0 (seems like the trim option didn't do anything).
Your wisdom is much appreciated!
Thanks,
Paul
put(series, 3. -l) as series,
The PUT function has two other options, -l or -r to align the value to the left or right as desired. I've add in the -l above to illustrate. Let us know if that solves your issue. If you want 0 as 000 you can use z3. instead of 3. to get that.
@pchappus wrote:
So here is my problem. I have a numeric variable that is either 0, 500, 600, or 660. Given that this is a categorical variable, I would like it to change it to a character but can't seem to get it to work. I've gotten close using the code below:
TRIM(PUT(SERIES,3.)) AS SERIES,
This didn't give me my desired result. It was a character, but now the 0's were "space"space"0 (seems like the trim option didn't do anything).
Your wisdom is much appreciated!
Thanks,
Paul
put(series, 3. -l) as series,
The PUT function has two other options, -l or -r to align the value to the left or right as desired. I've add in the -l above to illustrate. Let us know if that solves your issue. If you want 0 as 000 you can use z3. instead of 3. to get that.
@pchappus wrote:
So here is my problem. I have a numeric variable that is either 0, 500, 600, or 660. Given that this is a categorical variable, I would like it to change it to a character but can't seem to get it to work. I've gotten close using the code below:
TRIM(PUT(SERIES,3.)) AS SERIES,
This didn't give me my desired result. It was a character, but now the 0's were "space"space"0 (seems like the trim option didn't do anything).
Your wisdom is much appreciated!
Thanks,
Paul
assign to a different variable and not the same variable
TRIM(PUT(SERIES,3.)) AS SERIES1,
@novinosrin wrote:
assign to a different variable and not the same variable
TRIM(PUT(SERIES,3.)) AS SERIES1,
That's perfectly valid in SQL, it's just in a data step that you cannot use the same type while changing the data type.
If a variable is numeric you cannot make it character.
For a very small number of values such as you show if you want to display the values with different appearance then a custom format would likely be a better way to go. That does not change the variable name or type and can be turned on or off as needed.
You do not show what you actually want to see for those values though. So it is hard to recommend specific code. So describe or show what you want to appear for each of the values.
Trim function removes trailing blanks, if you want leading blanks removed as well then use the STRIP function.
or even
newvar= put(series,3. -L); which left justifies the output of the Put function.
Thanks for the input guys. As for the custom formatting, not sure it is really needed. I was getting an error when trying to merge two tables because series was a character in one table and a number in another. I'm going to try merging them using the PUT( series, 3. -l) and see what happens.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.