## Phone number

Solved
Occasional Contributor
Posts: 8

# Phone number

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

Accepted Solutions
Solution
‎09-21-2017 06:22 AM
Posts: 3,167

## Re: Phone number

[ Edited ]

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

All Replies
Solution
‎09-21-2017 06:22 AM
Posts: 3,167

## Re: Phone number

[ Edited ]

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

Super User
Posts: 10,691

## Re: Phone number

```
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;

```
Super User
Posts: 6,637

## Re: Phone number

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

SAS Super FREQ
Posts: 4,174

## Re: Phone number

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/

Super User
Posts: 13,338

## Re: Phone number

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.

☑ This topic is solved.