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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 6 replies
  • 1320 views
  • 1 like
  • 5 in conversation