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
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.;
Give example of "numeric dateline"
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.;
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
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.