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;
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;
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,' ');
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.
@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
bowlof cereal and milk without using a spoon or bowl.
Expanded for a closer approximation to OP restrictions. (just to be a smart alec 😉 )
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;
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.
If this is an interview question, then I doubt it would be worth working for a company asking such dumb questions.
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 .
Hi @Ksharp
Another approch
data e;
set dsn;
r=kupdate(phone,1,find(strip(phone)," ",-length(phone)),trim(' '));
run;
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 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?
Hi @ballardw
Thank you for your solution
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!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: