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

## numeric extract last n digits

``````data ph;
input phone\$12.;
datalines;
32-015-10101
32-015-11201
32-015-11501
32-015-11502
32-015-11503
32-015-11504
;
run;

data last_3;
set ph;
x=substr(phone,length(phone)-1);/*last two characters*/
y=substr(phone,length(phone)-2);/*last three characters*/
proc print;
run;

data ph1;
input phone 15. ;
datalines;
32-015-10101
32-015-11201
32-015-11501
32-015-11502
32-015-11503
32-015-11504
;
run;
``````

How to read numeric datelines and how to get last 2 digits

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: numeric extract last n digits

You cannot read strings with hyphens in it into a number.

If you have number with integer values then the last two digits is just the remainder when dividing by 100. Which 10**2.

So to get the list N digits from an integer use:

``````last2num=mod(number,10**2);
last5num=mod(number,10**5);``````

If you have a string you showed how to get the last N characters.

``````last2char=substrn(string,length(string)-(2-1));
last5char=substrn(string.length(string)-(5-1));``````

If you want to convert those strings into a number using the INPUT() function.

``````last2num=input(substrn(string,length(string)-(2-1)),32.);
last5num=input(substrn(string.length(string)-(5-1)),32.);``````

If you did make a numeric variable if what the leading zeros to show then use Z format.

``format last2num z2. last5num z5.;``
3 REPLIES 3
Diamond | Level 26

## Re: numeric extract last n digits

Give example of "numeric dateline"

--
Paige Miller
Super User

## Re: numeric extract last n digits

You cannot read strings with hyphens in it into a number.

If you have number with integer values then the last two digits is just the remainder when dividing by 100. Which 10**2.

So to get the list N digits from an integer use:

``````last2num=mod(number,10**2);
last5num=mod(number,10**5);``````

If you have a string you showed how to get the last N characters.

``````last2char=substrn(string,length(string)-(2-1));
last5char=substrn(string.length(string)-(5-1));``````

If you want to convert those strings into a number using the INPUT() function.

``````last2num=input(substrn(string,length(string)-(2-1)),32.);
last5num=input(substrn(string.length(string)-(5-1)),32.);``````

If you did make a numeric variable if what the leading zeros to show then use Z format.

``format last2num z2. last5num z5.;``
Rhodochrosite | Level 12

## Re: numeric extract last n digits

Substr only works on char data. You can use it and then convert it to numeric with input function. Read this:

Discussion stats
• 3 replies
• 5058 views
• 0 likes
• 4 in conversation