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

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

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

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

14 REPLIES 14
jwillis
Quartz | Level 8

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

jwillis
Quartz | Level 8

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.

rakeshvvv
Quartz | Level 8

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

rakeshvvv
Quartz | Level 8

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

slchen
Lapis Lazuli | Level 10

data have;

input string;

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

cards;

295999335

29599973101

295999333

2959997311

2959997312

295999735

2959997310

;

run;

asasuser
Calcite | Level 5

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_

data_null__
Jade | Level 19
substrn(s,length(s)-7)
jwillis
Quartz | Level 8

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;

data_null__
Jade | Level 19

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

jwillis
Quartz | Level 8

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.

data_null__
Jade | Level 19

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.

jwillis
Quartz | Level 8

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.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 14 replies
  • 2144 views
  • 0 likes
  • 6 in conversation