Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to extract last 4 characters of the string in SAS

Tags:

sas

improved formatting,I am a bit stuck where I am not able to extract the last 4 characters of the string., when I write :-

indikan=substr(Indikation,length(Indikation)-3,4);

It is giving invalid argument.
how to do this?

like image 669
user3658367 Avatar asked Jan 09 '15 11:01

user3658367


2 Answers

This code works:

data temp;
indikation = "Idontknow";
run;

data temp;
set temp;
indikan = substrn(indikation,max(1,length(indikation)-3),4);
run;

Can you provide more context on the variable? If indikation is length 3 or smaller than I could see this erroring or if it was numeric it may cause issues because it right justifies the numbers (http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000245907.htm).

like image 194
JJFord3 Avatar answered Sep 23 '22 03:09

JJFord3


If it's likely to be under four characters in some cases, I would recommend adding max:

indikan = substrn(indikation,max(1,length(indikation)-3),4);

I've also added substrn as Rob suggests given it better handles a not-long-enough string.

like image 30
Joe Avatar answered Sep 20 '22 03:09

Joe