INPUT vs PUT

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

INPUT vs PUT

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


Accepted Solutions
Solution
‎06-05-2018 04:27 PM
Super User
Posts: 23,705

Re: INPUT vs PUT

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


All Replies
Solution
‎06-05-2018 04:27 PM
Super User
Posts: 23,705

Re: INPUT vs PUT

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


 

PROC Star
Posts: 1,800

Re: INPUT vs PUT

assign to a different variable and not the same variable

 

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

Super User
Posts: 23,705

Re: INPUT vs PUT

Posted in reply to novinosrin

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

Super User
Posts: 13,528

Re: INPUT vs PUT

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.

 

Occasional Contributor
Posts: 9

Re: INPUT vs PUT

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.

Occasional Contributor fy1
Occasional Contributor
Posts: 8

Re: INPUT vs PUT

you can to use put(series, z3.) to change it to character.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

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