DATA Step, Macro, Functions and more

Insert character at a specific position in a string

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

Insert character at a specific position in a string

Hello,

 

Let's say I have a string variable with the value "ABCD". How do I insert a 'K' between first and second characters so that the output is: AKBCD?

 

I tried the following but it's not working:

substr(string,1,0) = 'K';

 

Thank you in advance.  


Accepted Solutions
Solution
‎11-15-2017 07:13 PM
Super User
Posts: 6,933

Re: Insert character at a specific position in a string

[ Edited ]

First step:  make sure your variable has a length of $5 or longer.  Otherwise you need to assign the new string to a new variable.

 

data want;

set have;

var = cat( substr(var, 1, 1), 'K', substr(var, 2) );

run;

View solution in original post


All Replies
Solution
‎11-15-2017 07:13 PM
Super User
Posts: 6,933

Re: Insert character at a specific position in a string

[ Edited ]

First step:  make sure your variable has a length of $5 or longer.  Otherwise you need to assign the new string to a new variable.

 

data want;

set have;

var = cat( substr(var, 1, 1), 'K', substr(var, 2) );

run;

☑ This topic is solved.

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

Discussion stats
  • 1 reply
  • 2201 views
  • 2 likes
  • 2 in conversation