Adding a space in the middle of a number (using a DI Studio expression or format)

Solved
Frequent Contributor
Posts: 89

Adding a space in the middle of a number (using a DI Studio expression or format)

If I have the number "15748645879", and want to add a space after the 6th number so that it becomes "157486 45879", how do I accomplish this in a DI Studio transformation expression (or format)?

Thanks.

Accepted Solutions
Solution
‎11-14-2012 05:15 AM
Super Contributor
Posts: 644

Re: Adding a space in the middle of a number (using a DI Studio expression or format)

Please clarify.  Is your starting value a numeric variable or a string?  If a numeric, are you wanting an output string in the format you describe?  (If so you will need a PICTURE format.)  But if your starting point is already a string and you need to insert a space you could achieve this with the SUBSTR() function.  Note that in the latter case the string length will have increased so your variable needs to be long enough to hold the extra space character.

Richard in Oz

All Replies
Solution
‎11-14-2012 05:15 AM
Super Contributor
Posts: 644

Re: Adding a space in the middle of a number (using a DI Studio expression or format)

Please clarify.  Is your starting value a numeric variable or a string?  If a numeric, are you wanting an output string in the format you describe?  (If so you will need a PICTURE format.)  But if your starting point is already a string and you need to insert a space you could achieve this with the SUBSTR() function.  Note that in the latter case the string length will have increased so your variable needs to be long enough to hold the extra space character.

Richard in Oz

Frequent Contributor
Posts: 89

Re: Adding a space in the middle of a number (using a DI Studio expression or format)

Thanks. The variable is already a character, and it has a decent length, but I can't make SUBSTR do what I'd like. Any advice on this?

EDIT: Nevermind, got it, thanks again.

Super Contributor
Posts: 644

Re: Adding a space in the middle of a number (using a DI Studio expression or format)

value = substr(value, 1, 6) || ' ' || substr(value, 7, 5) ;

should work if you want to put the result back into the same variable and you have the room.  If not, show me the log!!

Richard in Oz

data _null_ ;

length value \$12 ;

value = "15748645879" ;

value = substr(value, 1, 6) || ' ' || substr(value, 7, 5) ;

Put value= ;

run ;

value=157486 45879

Message was edited by: Richard in Oz - added code to demonstrate

🔒 This topic is solved and locked.