DATA Step, Macro, Functions and more

Joining string's of different length

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

Joining string's of different length

Hi Guys,

 

I find that I am in need of some advice again…

I have to join two tables, t1 has an 11 digit number, t2 is a matching number, but the string is only between 6 and 11 digits.  If starting from the last number in the string (the furthest right number) the numbers will match. i.e.  Both number are formatted as a character ($12.)

 

T1

T2

                                                     00000456789

                                                               456789

                                                     00003456789

                                                             3456789

                                                     00023456789

                                                           23456789

 

How do I add 0’s to the string in T2 to make it an 11 digit number?  Or is there a better way to join them?

 

I’m very new to all of this, so if you could explain it as if speaking to a child, that would be great.

 

Thanks in advance for your time,

 

paul


Accepted Solutions
Solution
‎09-01-2016 10:52 AM
Super User
Posts: 17,828

Re: Joining string's of different length

Make them the same, either strip leading 0 or add leading zero. 

Add leading 0

 

put( var, z11.) 

 

Remove leading 0

 

input(var, best12.) 

 

You can either create a new variable and merge using the new variable or do conversion on join condition if using SQL. 

Eg:

T1= put(...)

 

View solution in original post


All Replies
Solution
‎09-01-2016 10:52 AM
Super User
Posts: 17,828

Re: Joining string's of different length

Make them the same, either strip leading 0 or add leading zero. 

Add leading 0

 

put( var, z11.) 

 

Remove leading 0

 

input(var, best12.) 

 

You can either create a new variable and merge using the new variable or do conversion on join condition if using SQL. 

Eg:

T1= put(...)

 

Super User
Posts: 17,828

Re: Joining string's of different length

Sorry, missed portion about T2 already being character. 

You can convert to numeric to implement solution or use repeat to generate number of 0 based on length and concatenate VM,yes. 

 

Untested

 

Repeat(0, 11-length(t2)) ||  t2

Contributor
Posts: 24

Re: Joining string's of different length

Hi Reeza, thanks for your input. I worked it out from what you said above. Initially the shorter number was Numeric, I had changed it in a previous step. So I went pack there and padded the 0's to the front and it worked great. thanks for your time, paul
☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 246 views
  • 0 likes
  • 2 in conversation