DATA Step, Macro, Functions and more

Last 5 characters from string (irrespective of the length)

Accepted Solution Solved
Reply
Contributor
Posts: 46
Accepted Solution

Last 5 characters from string (irrespective of the length)

[ Edited ]

Hi All,

 

Please help me with Syntax

 

/* How to take last 5 characters from a string */

Ex:-

Number in Text

 

Number

 

1234567890
123456
987654321
8888888
999999999
4242424

 

Output should be like

Number

 

67890
23456
54321
88888
99999
42424

 

Appriciate in advance


Accepted Solutions
Solution
‎02-28-2017 08:15 AM
Super User
Super User
Posts: 6,502

Re: Last 5 characters from string (irrespective of the length)

[ Edited ]

Depends if NUMBER is a number or a character string.

Try these methods.  What do you want to do when the number is already less than 5 digits?

 

 data have ;
 /* Read as both a number and a char */
  input @1 number @1 char $10. ;
  /* calc 5 digit remainder with mod() */
  number2 = mod(number,100000);
  /* for char version, get last 5 characters */
  char2 = substr(right(char),6);
  /* handle case when char < 5 digits */
  char3 = substr(char,max(1,length(char)-4));
  put number number2 char2 char3 ;
cards;
1234567890
123456
987654321
8888888
999999999
4242424
1234
;;;;

 

Output:

Obs        number    char          number2    char2    char3

 1     1234567890    1234567890     67890     67890    67890
 2         123456    123456         23456     23456    23456
 3      987654321    987654321      54321     54321    54321
 4        8888888    8888888        88888     88888    88888
 5      999999999    999999999      99999     99999    99999
 6        4242424    4242424        42424     42424    42424
 7           1234    1234            1234      1234    1234 

 

View solution in original post


All Replies
Super Contributor
Posts: 297

Re: Last 5 characters from string(irrespective of the length)

How about this?

DATA WANT;

SET HAVE;

NUMBER1 = SUBSTR(TRIM(NUMBER),LENGTH(TRIM(NUMBER))-4);

RUN;

Solution
‎02-28-2017 08:15 AM
Super User
Super User
Posts: 6,502

Re: Last 5 characters from string (irrespective of the length)

[ Edited ]

Depends if NUMBER is a number or a character string.

Try these methods.  What do you want to do when the number is already less than 5 digits?

 

 data have ;
 /* Read as both a number and a char */
  input @1 number @1 char $10. ;
  /* calc 5 digit remainder with mod() */
  number2 = mod(number,100000);
  /* for char version, get last 5 characters */
  char2 = substr(right(char),6);
  /* handle case when char < 5 digits */
  char3 = substr(char,max(1,length(char)-4));
  put number number2 char2 char3 ;
cards;
1234567890
123456
987654321
8888888
999999999
4242424
1234
;;;;

 

Output:

Obs        number    char          number2    char2    char3

 1     1234567890    1234567890     67890     67890    67890
 2         123456    123456         23456     23456    23456
 3      987654321    987654321      54321     54321    54321
 4        8888888    8888888        88888     88888    88888
 5      999999999    999999999      99999     99999    99999
 6        4242424    4242424        42424     42424    42424
 7           1234    1234            1234      1234    1234 

 

Occasional Contributor
Posts: 7

Re: Last 5 characters from string(irrespective of the length)

Here is another proposal:

data have;

input char $char20.;

cards;

1234567890

   123456

987654321

8888888

999999999

4242424

run;

data want;

set have;

length char_new $5;

char_new=left(reverse(char));

char_new=reverse(char_new);

run;

proc print data=want; run;

Super User
Super User
Posts: 7,407

Re: Last 5 characters from string(irrespective of the length)

Mmm, nice, but it does have leading blanks if your string isn't > 5 in the first place.  Also, if you have a long program (and maybe if length isn't even setup there), its not easy to see what is happening as the code just reverses twice.  Substr or trimming make the statement very explicit.

Occasional Contributor
Posts: 9

Re: Last 5 characters from string(irrespective of the length)

Hi,

Try this

data have;

  input number $;

  var = substr(number,length(number)-4);

cards;

1234567890

123456

987654321

8888888

999999999

4242424

;

run;

Super Contributor
Posts: 275

Re: Last 5 characters from string(irrespective of the length)

string=prxchange('s/.*(\d{5})/$1/',-1,strip(number));

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 17853 views
  • 6 likes
  • 7 in conversation