HHi ,
i have a coloumn product
whuch has vslues
1 YEAR TERM
10 YEAR TERM
15 YEAR TERM
UNKNOWN
hiw to extract the last letter M in 1,10,15 YEAR TERM and how to extract N from unknown
can anyone help
There might be a more direct route, but the following will work:
data have;
informat product $12.;
input product &;
newfield=substr(reverse(strip(product)),1,1);
cards;
1 YEAR TERM
10 YEAR TERM
15 YEAR TERM
UNKNOWN
;
Or this way:
data have;
informat product $12.;
input product &;
newfield=substrn(product,lengthn(product),1);
cards;
1 YEAR TERM
10 YEAR TERM
15 YEAR TERM
UNKNOWN
;
run;
hi ,
thank you so much it worked
Hi,
I'm sure there's multiple ways to achieve this. Here is one you can try.
substr( product , length( product) , 1)
For documentation of the used functions please see,
SUBSTR() - SAS(R) 9.4 Functions and CALL Routines: Reference, Third Edition
LENGTH() - SAS(R) 9.4 Functions and CALL Routines: Reference, Third Edition
Timo
data have;
informat product $20.;
input product &;
newfield=prxchange('s/.*(\w)/$1/',-1,product);
cards;
1 YEAR TERM
10 YEAR TERM
15 YEAR TERM
UNKNOWN
;
run;
If you examine your results using PROC CONTENTS, you will see that the length of your new variable is not necessarily what you would expect. If you want it to be defined as 1 character long, add a LENGTH statement first:
length newfield $ 1;
Having done that, there are other possibilities for extraction such as:
newfield = left(reverse(product));
Good luck.
Another option to get the length right would use the CHAR function.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.