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 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 5058 views
  • 0 likes
  • 4 in conversation