BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
_maldini_
Barite | Level 11

#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 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

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

 

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

View solution in original post

7 REPLIES 7
Reeza
Super User

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

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

 

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

_maldini_
Barite | Level 11

@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!

Reeza
Super User

Reverse the string and then take the substring.

Reversing a string of 1 is 1.

_maldini_
Barite | Level 11

@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).


rev sub.jpg
Tom
Super User Tom
Super User

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.

 

Tom
Super User Tom
Super User

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);
Ksharp
Super User
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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 51170 views
  • 5 likes
  • 4 in conversation