Desktop productivity for business analysts and programmers

Right function in excel how to convert to sas

Reply
Frequent Contributor
Posts: 120

Right function in excel how to convert to sas

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

Esteemed Advisor
Posts: 7,290

Re: Right function in excel how to convert to sas

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

;

Respected Advisor
Posts: 3,825

Re: Right function in excel how to convert to sas

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;

Frequent Contributor
Posts: 120

Re: Right function in excel how to convert to sas

hi ,

thank you so much it worked

SAS Employee
Posts: 1

Re: Right function in excel how to convert to sas

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

Super Contributor
Posts: 275

Re: Right function in excel how to convert to sas

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;

Respected Advisor
Posts: 4,966

Re: Right function in excel how to convert to sas

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.

Respected Advisor
Posts: 3,775

Re: Right function in excel how to convert to sas

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

lastchar = char(product,length(product));
 
Ask a Question
Discussion stats
  • 7 replies
  • 2044 views
  • 0 likes
  • 7 in conversation