BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
pavank
Quartz | Level 8

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
Ksharp
Super User

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;

View solution in original post

14 REPLIES 14
PaigeMiller
Diamond | Level 26

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
Astounding
PROC Star

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.

ballardw
Super User

@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 😉 )

Ksharp
Super User

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;
PaigeMiller
Diamond | Level 26

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
SASKiwi
PROC Star

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

Ksharp
Super User

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 .

pavank
Quartz | Level 8

Hi @Ksharp 

Another approch

data e;
set dsn;
r=kupdate(phone,1,find(strip(phone)," ",-length(phone)),trim(' '));
run;
Ksharp
Super User
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...
ballardw
Super User

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;

or reading the data to just read those 10 characters

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

 

 

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

@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?

 

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

Hi @ballardw 

Thank you for your solution

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 14 replies
  • 1138 views
  • 9 likes
  • 6 in conversation