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
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.
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.
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.
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.
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
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.
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).
data have;
input string;
new_string=prxchange('s/.*(\d{8})/$1/',-1,strip(string));
cards;
295999335
29599973101
295999333
2959997311
2959997312
295999735
2959997310
;
run;
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_
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;
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.
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.
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.
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.