BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
pchappus
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
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


 

View solution in original post

6 REPLIES 6
Reeza
Super User
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


 

novinosrin
Tourmaline | Level 20

assign to a different variable and not the same variable

 

        TRIM(PUT(SERIES,3.)) AS SERIES1,

Reeza
Super User

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

ballardw
Super User

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.

 

pchappus
Obsidian | Level 7

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.

fy1
Fluorite | Level 6 fy1
Fluorite | Level 6
you can to use put(series, z3.) to change it to character.

SAS Innovate 2025: Register Now

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1790 views
  • 1 like
  • 5 in conversation