09-28-2017 05:58 PM
I have a string variable called part4 that looks something like:
I am trying to extract the first 24 characters starting from right hand side moving toward the left, how can I accomplish that?
09-28-2017 06:44 PM - edited 09-28-2017 07:01 PM
09-28-2017 07:02 PM - edited 09-28-2017 07:03 PM
extract= substr(x, length(x)-24+1);
I did 24 + 1 incase you need to change the 24 based on a variable value.
If you have 10 characters and want 4, 10-4=6, but character positions 6,7,8,9 and 10 = 5 characters, so add 1.
This will not work if you ask for more characters than exist though.
09-28-2017 09:22 PM
data want; set have; Latest24months = ifc(length(text)>24,substrn(text,length(text)-23), text); LATETS24MONTHS_2= ifc(length(text)<=24,TEXT, substrn(text,length(text)-23));
IF LATEST24MONTHS = LATEST24MONTHS_2 THEN TAG=1; run;
Choose between latest24months or latest24months_2. Tag is used to check if both will provide the same result.
Hope this helps.