BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BrahmanandaRao
Lapis Lazuli | Level 10
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
Tom
Super User Tom
Super User

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

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Give example of "numeric dateline"

--
Paige Miller
Tom
Super User Tom
Super User

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.;
tarheel13
Rhodochrosite | Level 12

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

https://www.listendata.com/2017/03/extract-last-4-characters-digits-in-sas.html 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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