Help using Base SAS procedures

Pull out last 8 charcters of a string

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 145
Accepted Solution

Pull out last 8 charcters of a string

Hi,

I have a character variables with recoreds of different lengths like 8,9,10,11,12,13,13.... i want to write a code in such way that i want to pull out only last 8 charcters of that variable....only last 8 charaters irrespective of the varaible lenght.

thanks


Accepted Solutions
Solution
‎07-31-2014 11:30 AM
Respected Advisor
Posts: 3,777

Re: Pull out last 8 charcters of a string

The mistake in your program is FULLVAR should be character not numeric.  The conversion to character with best12. "right justified" is the source of the differences you see in the methods.  Fix that and you get this.7-31-2014 10-28-35 AM.png

View solution in original post


All Replies
Regular Contributor
Posts: 217

Re: Pull out last 8 charcters of a string

rakeshvvv,

You did not indicate if you wanted to keep trailing blanks.  If so, you do not need the trim, left or strip functions.  Use the reverse function, grab the first 8 characters of the reversed value using the substring function; then reverse the result of the substring.

Super User
Super User
Posts: 7,433

Re: Pull out last 8 charcters of a string

Whilst I completely agree with the answer to the question, I would want to check what type of data he is dealing with as just chopping off any 8 chars from the end seems a bit random.  Normally you would want to have some kind of pattern indicator, i.e. all records have id or XYX-yyyyyyyy and you want the last 8.  Otherwise you could get some strange results if items are not padded as expected, there are leading/inner blanks, special characters etc.

Regular Contributor
Posts: 217

Re: Pull out last 8 charcters of a string

Exactly RW9!  However, that's what experienced analysts know to do.  I was answering the question posed by an audience that appears to be less experienced.  My apologies to rakesh if I assumed a skill level less experienced than you are.

Frequent Contributor
Posts: 145

Re: Pull out last 8 charcters of a string

my data looks like this and I want to have only

295999335

29599973101

295999333

2959997311

2959997312

295999735

2959997310

output should be like this....should have last 8 charaters

95999335

99973101

95999333

59997311

59997312

95999735

59997310

Super User
Super User
Posts: 7,433

Re: Pull out last 8 charcters of a string

So, whats the difference between x95999xxx and xx95999?   Note that with your data you can strip out 295999 (e.g. tranwrd(id,"295999","")) in each instance and be left with a unique number of 3-5 digits.

Frequent Contributor
Posts: 145

Re: Pull out last 8 charcters of a string

I am dealing with a barcode number that includes the “Container number” that is one or two characters to the right of the number.  This number sometimes changes from the time to time.For improvement,we need to remove the “Container Number”  from the barcode number (In other words, use only the 8 characters to the left of the number).

Super Contributor
Posts: 275

Re: Pull out last 8 charcters of a string

data have;

input string;

new_string=prxchange('s/.*(\d{8})/$1/',-1,strip(string));

cards;

295999335

29599973101

295999333

2959997311

2959997312

295999735

2959997310

;

run;

New Contributor
Posts: 3

Re: Pull out last 8 charcters of a string

There are two options:

1. If the var is numeric, you can just use the MOD function:

     VAR8 = Mod(VAR,100000000) ;

2. If the var is character, use the SUBSTR function as advice by data_NULL_

Respected Advisor
Posts: 3,777

Re: Pull out last 8 charcters of a string

substrn(s,length(s)-7)
Regular Contributor
Posts: 217

Re: Pull out last 8 charcters of a string

rakeshvvv,

There are differences in functions and there are valid reasons for using one function over another.  Below are different solutions to the question you asked with the data you provided.

data want;
input fullvar one $20. ;
length eightlim1 eightlim2 eightlim3 two three $8;
      one  = reverse(strip(fullvar));
      two  = substr(one,1,8);
     three = reverse(two);
eightlim1  = reverse(substr(reverse(strip(fullvar)),1,8));
eightlim2  = substrn(fullvar,length(fullvar)-7);
eightlim3  = substr(fullvar,length(fullvar)-7);
datalines;
295999335
29599973101
295999333
2959997311
2959997312
295999735
2959997310

;
run;

proc print data=work.want;run;

Solution
‎07-31-2014 11:30 AM
Respected Advisor
Posts: 3,777

Re: Pull out last 8 charcters of a string

The mistake in your program is FULLVAR should be character not numeric.  The conversion to character with best12. "right justified" is the source of the differences you see in the methods.  Fix that and you get this.7-31-2014 10-28-35 AM.png

Regular Contributor
Posts: 217

Re: Pull out last 8 charcters of a string

Data_null,

Thank you for the correction to "input fullvar one $20." I made the mistake of thinking that the input statement acted like the format statement. "input fullvar $20 one $20." is what I should have coded.

Respected Advisor
Posts: 3,777

Re: Pull out last 8 charcters of a string

You could have written INPUT (fullvar one) ($20.); 

However I don't understand why you are reading "ONE" at all.  There is only 1 field in the data.

Regular Contributor
Posts: 217

Re: Pull out last 8 charcters of a string

In math courses, our instructors always said "show your work".  Showing my reasoning has help others. It has also allowed others to point out my mistakes. "One" is a variable that contains the value extracted from fullvar after blanks have been removed and the non-blank values have been reversed.

🔒 This topic is solved and locked.

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

Discussion stats
  • 14 replies
  • 616 views
  • 0 likes
  • 6 in conversation