Quartz | Level 8

## extract last 10 digits

Hi Experts

How to extract last 10 digits without substr ,scan ,reverse ,regex functions

``````data  dsn;
input phone \$ 50.;
cards;
S001 110064 9873628374
S002 110065 8447752736
S003 110023 9811537264
;
run;``````
1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: extract last 10 digits

It looks like  an interview question ?

``````data  dsn;
input phone \$ 50.;
length dummy \$ 12;
dummy=phone;
want=strip(tranwrd(phone,dummy,' '));
drop dummy;
cards;
S001 110064 9873628374
S002 110065 8447752736
S003 110023 9811537264
;
run;``````
14 REPLIES 14
Diamond | Level 26

## Re: extract last 10 digits

I don't accept your restrictions, and so it is easy to do. No reason to make it harder.

In this case, use the SCAN function.

``last_10=scan(phone,3,' ');``

--
Paige Miller
PROC Star

## Re: extract last 10 digits

This is not difficult for SAS:

``````data  dsn;
length phone \$ 50;
input phone phone phone;
cards;
S001 110064 9873628374
S002 110065 8447752736
S003 110023 9811537264
;``````

But why would you want to prohibit the most useful tools.  To illustrate, here's a task for you.  Try to eat a bowl of cereal and milk without using a spoon.

Super User

## Re: extract last 10 digits

@Astounding wrote:

This is not difficult for SAS:

``````data  dsn;
length phone \$ 50;
input phone phone phone;
cards;
S001 110064 9873628374
S002 110065 8447752736
S003 110023 9811537264
;``````

But why would you want to prohibit the most useful tools.  To illustrate, here's a task for you.  Try to eat a bowl of cereal and milk without using a spoon or bowl.

Expanded for a closer approximation to OP restrictions. (just to be a smart alec 😉 )

Super User

## Re: extract last 10 digits

It looks like  an interview question ?

``````data  dsn;
input phone \$ 50.;
length dummy \$ 12;
dummy=phone;
want=strip(tranwrd(phone,dummy,' '));
drop dummy;
cards;
S001 110064 9873628374
S002 110065 8447752736
S003 110023 9811537264
;
run;``````
Diamond | Level 26

## Re: extract last 10 digits

Yes, probably an interview question or a homework assignment. If an interview question, this makes me angry, I would want the interviewer to find out if the candidate knows to use the SCAN function in this situation. Some interviewers want to see if the candidate can "think on their feet" when faced with unexpected restrictions, in which case this is a particularly poor way to find that out.

I have told this story before, but I'll tell it again. Years ago, I took over SAS and statistical support for a department, the previous person having left the company. I began trying to understand the code left behind by the previous person. I found many places where there were 10-15 lines of data step code which I didn't understand. I worked through the code, line by line, and came to the conclusion that these 10-15 lines of data step code were simply adding a column of zeros and ones. Shocked, I thought that can't be right, and did it again, and came to the same conclusion. This decision to add zeros and ones with 10-15 lines of data step code not only cost my company time (for the original programmer to develop this code compared to using PROC SUMMARY) but also cost the company time for me to understand it and replace the code with PROC SUMMARY. Perhaps, some interviewer was happy that the previous person at that job could write such convoluted code; I would not be happy if an interviewer at my company used the question in the original post; and I would not be happy if an interviewer at my company wanted a candidate to not use obvious tools and come up with a convoluted way to perform a task.

--
Paige Miller
PROC Star

## Re: extract last 10 digits

If this is an interview question, then I doubt it would be worth working for a company asking such dumb questions.

Super User

## Re: extract last 10 digits

I think that "previous person" has too poor programming skill to perform the same task or just is a sas rookie don't konw how to enhance the code by "PROC SUMMARY".
I also meet a person who don't know how to calculate the years between two date by INTCK() or YRDIF(), just divide its range by 365.25

Anyway, any seasoned sas programmer is coming from a rookie.
Essentially, I think that reason is sas is a very little used programming language.People  would barely like to learn it (unlike Python,Java,C#) due to hard to find a job. The company is too difficult to find a good sas programmer .

Quartz | Level 8

## Re: extract last 10 digits

Hi @Ksharp

Another approch

``````data e;
set dsn;
r=kupdate(phone,1,find(strip(phone)," ",-length(phone)),trim(' '));
run;``````
Super User

## Re: extract last 10 digits

It is the first time I heard of KUPDATE() function.
I quickly check its documentation in support.sas.com , it is more like a left-side SUBSTR() function.
https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0uev77ebdwy90n1rsd7hwjd2qc3.h...
Super User

## Re: extract last 10 digits

another stupid way

```data  dsn;
input phone \$ 50.;
l=length(phone);
length newstring \$ 10;
do i=(l-10) to l;
c=char(phone,i);
newstring=cats(newstring,c);
end;
drop l c;
cards;
S001 110064 9873628374
S002 110065 8447752736
S003 110023 9811537264
;
run;```

```data  dsn;
input @13 phone \$ 10.;

cards;
S001 110064 9873628374
S002 110065 8447752736
S003 110023 9811537264
;
run;```

Personally I would ask "how exhaustive is that example input"? If the lengths of the "last" part of that record change the approach might change such as what would be expected if reading

5004 1101 7234567

Super User

## Re: extract last 10 digits

ballardw,
CHAR(i) is just another version of SUBSTR(i,1)
Super User

## Re: extract last 10 digits

@Ksharp wrote:
ballardw,
CHAR(i) is just another version of SUBSTR(i,1)

And the Char documentation says it returns same as SUBPAD(string, position, 1). But default length from Subpad would be 200.

Length of returned variables will differ if not assigned beforehand. SUBSTR will create a variable the length of the base string variable, Char creates variable with length 1. So not exactly the same.

But the silly restrictions say SUBSTR specifically, not CHAR.  Any such artificial restriction I would say opens up any alternative. Didn't even go the SUBSTRN (also not explicitly restricted) route.

If this is an interview question and I come up with more functions than the interviewer knows to restrict the result should be what?

Super User

## Re: extract last 10 digits

Maybe interviewer ask this question for just showing himself brilliant programming skill ?
Quartz | Level 8