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
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
How about this?
DATA WANT;
SET HAVE;
NUMBER1 = SUBSTR(TRIM(NUMBER),LENGTH(TRIM(NUMBER))-4);
RUN;
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
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;
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.
Hi,
Try this
data have;
input number $;
var = substr(number,length(number)-4);
cards;
1234567890
123456
987654321
8888888
999999999
4242424
;
run;
string=prxchange('s/.*(\d{5})/$1/',-1,strip(number));
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.