BookmarkSubscribeRSS Feed
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;
5 REPLIES 5
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;
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

 

 

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
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
  • 5 replies
  • 191 views
  • 1 like
  • 5 in conversation