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;
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;
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
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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: