BookmarkSubscribeRSS Feed
chennupriya
Quartz | Level 8

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

7 REPLIES 7
art297
Opal | Level 21

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

;

Patrick
Opal | Level 21

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;

chennupriya
Quartz | Level 8

hi ,

thank you so much it worked

ssftik
SAS Employee

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

slchen
Lapis Lazuli | Level 10

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;

Astounding
PROC Star

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.

data_null__
Jade | Level 19

Another option to get the length right would use the CHAR function.

lastchar = char(product,length(product));
 

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!

SAS Enterprise Guide vs. SAS Studio

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.

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
  • 7 replies
  • 5663 views
  • 0 likes
  • 7 in conversation