Extract the last character of a string / Extract only numeric values in an alphanumeric string

Solved
Regular Contributor
Posts: 202

Extract the last character of a string / Extract only numeric values in an alphanumeric string

[ Edited ]

#1. I have a variable that contains alphanumeric strings of differing lengths. The last digit in each string is a character. I need to extract just the last digit of each string into a new variable. I was trying to use the SUBST function, but I don't know how to determine the position when the length of the string may change from observation to observation.

#2. Then, I need to extract only the numeric values in this same variable (e.g. xyz1333520M, x1234F). Again, the alphanumeric strings are of differing lengths. How can I extract just the numeric values (They are always sequential).

Thanks!

Accepted Solutions
Solution
‎09-02-2016 01:03 PM
Super User
Posts: 23,700

Re: Extract the last character of a string / Extract only numeric values in an alphanumeric string

1. Reverse(substr(string,1,1))

2. Compress(string, , 'kd');

Check the parameter order for compress, but use the modifiers to keep only digits

All Replies
Solution
‎09-02-2016 01:03 PM
Super User
Posts: 23,700

Re: Extract the last character of a string / Extract only numeric values in an alphanumeric string

1. Reverse(substr(string,1,1))

2. Compress(string, , 'kd');

Check the parameter order for compress, but use the modifiers to keep only digits

Regular Contributor
Posts: 202

Re: Extract the last character of a string / Extract only numeric values in an alphanumeric string

@Reeza

The result for Reverse(substr(string,1,1)) is the same as the result for (substr(string,1,1)). It seems like it's not picking up the "reverse".

What am I doing wrong here?

Thanks!

Super User
Posts: 23,700

Re: Extract the last character of a string / Extract only numeric values in an alphanumeric string

Reverse the string and then take the substring.

Reversing a string of 1 is 1.

Regular Contributor
Posts: 202

Re: Extract the last character of a string / Extract only numeric values in an alphanumeric string

[ Edited ]

@Reeza

<Reverse the string and then take the substring. Reversing a string of 1 is 1.>

I understand the approach, but: 1) The strings are not 1 character in length (e.g. 13000M, x13260M); and 2) When the string is reversed, the syntax is still taking the character in the first position in the orginal, not reversed, string.

In other words, 13000M is new_var=1, both with, and without, the reverse function. Should new_var=M, given that the string was reversed? That's what I'm after...

When I use the functions separately the reverse function converts 13000M to M13000. When I then use the substring function, only a subset of the observations have the correct value. It's only the observations w/ the longest length (i.e. 7 characters). Any observation w/ less characters than that contains a missing value (See attached image).

Super User
Posts: 8,089

Re: Extract the last character of a string / Extract only numeric values in an alphanumeric string

If the string is only one character string long then reversing it does nothing.

But if you applied the functions in the other order then you could still have problems because of trailing spaces. If you store 'ABC' into variable of length 5 then it will have two trailing spaces. If you reverse it there will be two leading spaces instead.

You could do

``substr(reverse(trim(VAR)),1,1)``

But it does seem like way too much work.

Super User
Posts: 8,089

Re: Extract the last character of a string / Extract only numeric values in an alphanumeric string

The LENGTH() function will tell you the location of the last non-blank character.  (Note for empty strings it will return 1).

``````want = substr(have,length(have),1);
``````
Super User
Posts: 10,770

Re: Extract the last character of a string / Extract only numeric values in an alphanumeric string

```Then use LENGTHN()

data _null_;
x='xxx';n1=length(x);n2=lengthn(x);
put x= n1= n2=;

x=' ';n1=length(x);n2=lengthn(x);
put x= n1= n2=;

run;

```
☑ This topic is solved.

Discussion stats
• 7 replies
• 10693 views
• 2 likes
• 4 in conversation