#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!
1. Reverse(substr(string,1,1))
2. Compress(string, , 'kd');
Check the parameter order for compress, but use the modifiers to keep only digits
1. Reverse(substr(string,1,1))
2. Compress(string, , 'kd');
Check the parameter order for compress, but use the modifiers to keep only digits
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!
Reverse the string and then take the substring.
Reversing a string of 1 is 1.
<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).
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.
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);
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.