BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
harcluna
Calcite | Level 5

Hello,

 

I'm sure this topic has been done to death, but I have a relatively simple problem.

 

I have  about a hundred phone numbers in the format 333--2964555 I want to put it into the format (555) 333-2964. I can add in the parentheses, spaces and dashes, but I can't figure out how to move the last 3 numbers to the front i.e so that 3332964555 becomes 5553332964 after my compress function.

 

I've just attached my code for 3 of the numbers;

 

data phone_a;
input ph $1-16;
datalines;
333--2964555

376--4232444

445--6777555
;
data b;
set phone_a;
base=compress(ph,"--.");
data c;
set b;
Phone_number=input(base,12.);
proc format;
picture phone_b (default=16)
low-high='999) 999-9999'
(prefix='(');
run;

proc print data=c;
format Phone_number phone_b.;
run;

 

Thank you very much

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

Perl Regular expression will be very handy in this situation:

 

data phone_a;
input ph $1-16;
ph_new=prxchange('s/(\d{3})(--)(\d{3})/(\1)\3-/o',1,ph);
datalines;
333--2964555 
376--4232444
445--6777555
;

ion 

View solution in original post

5 REPLIES 5
Haikuo
Onyx | Level 15

Perl Regular expression will be very handy in this situation:

 

data phone_a;
input ph $1-16;
ph_new=prxchange('s/(\d{3})(--)(\d{3})/(\1)\3-/o',1,ph);
datalines;
333--2964555 
376--4232444
445--6777555
;

ion 

Ksharp
Super User

data phone_a;
input ph $1-16;
datalines;
333--2964555
376--4232444
445--6777555
;
run;
data want;
 set phone_a;
 want=cats('(',substr(ph,length(ph)-2),')',substr(ph,1,length(ph)-3));
run;

Astounding
PROC Star

One side issue to note here ... when the CATS function creates a new variable, SAS assigns the new variable a length of $200.

Rick_SAS
SAS Super FREQ

Here is Ron Cody's tip for standardizing phone numbers in SAS.

https://blogs.sas.com/content/sastraining/2017/05/26/standardizing-phone-numbers-using-sas/

 

ballardw
Super User

@Rick_SAS wrote:

Here is Ron Cody's tip for standardizing phone numbers in SAS.

https://blogs.sas.com/content/sastraining/2017/05/26/standardizing-phone-numbers-using-sas/

 


One caution about standardizing phone numbers: Find out if the data may have internation numbers before you start.

 

We had a process similar to Ron Cody's for standardizing phone numbers and had issues with lots of responses when called of "I have no idea what you are talking about." The data turned out to have international numbers (South America) and some of the numbers when standardized created valid US phone numbers from international code+phone number. Which obviously were not the correct contacts.

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 3025 views
  • 3 likes
  • 6 in conversation