Quartz | Level 8

## Last 5 characters from string (irrespective of the length)

Hi All,

/* 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```

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

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

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
```

6 REPLIES 6
Quartz | Level 8

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

DATA WANT;

SET HAVE;

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

RUN;

Super User

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

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
```

Calcite | Level 5

## 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;

Diamond | Level 26

## 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.

Obsidian | Level 7

## 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;

Lapis Lazuli | Level 10

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

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

Discussion stats
• 6 replies
• 128147 views
• 17 likes
• 7 in conversation