BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sas_lak
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

6 REPLIES 6
Scott_Mitchell
Quartz | Level 8

How about this?

DATA WANT;

SET HAVE;

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

RUN;

Tom
Super User Tom
Super User

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 

 

barchan
Calcite | Level 5

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;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

SanjayAhir
Obsidian | Level 7

Hi,

Try this

data have;

  input number $;

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

cards;

1234567890

123456

987654321

8888888

999999999

4242424

;

run;

slchen
Lapis Lazuli | Level 10

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

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 6 replies
  • 128009 views
  • 17 likes
  • 7 in conversation