## Formatting Phone Numbers with separated area codes

Solved
Occasional Contributor
Posts: 5

# Formatting Phone Numbers with separated area codes

I am having trouble trying to convert phone numbers into the following format:

(xxx) xxx-xxxx

``````datalines;
9136122 Code=999 Date=6/30/2001
;``````

The outputted format I want is:

Phone = (999) 913-6122

Date = June 30, 2001

I am not sure how to deal with converting 'Code=999' as the area code.

Thanks.

Accepted Solutions
Solution
‎10-03-2017 11:58 AM
PROC Star
Posts: 549

## Re: Formatting Phone Numbers with separated area codes

something like this

``````data want;
input phone \$ 1-17 date1 \$ 18-33 ;
phone = cat('(',substr(trim(phone),14),') ', substr(phone,1,3), '-',substr(phone,1,4));
date= input(substr(date1,6),mmddyy10.);
format date worddate20.;
drop date1;
datalines;
9136122 Code=999 Date=6/30/2001
;``````

All Replies
Super User
Posts: 24,027

## Re: Formatting Phone Numbers with separated area codes

[ Edited ]

Please post sample data that accurately reflects your data. I can't tell if you have three variables or one variable.

This paper walks through the different ways to format your phone number.

http://www2.sas.com/proceedings/sugi31/243-31.pdf

Posts: 1,400

## Re: Formatting Phone Numbers with separated area codes

You have a choice of

1. making the phone number a numeric value with 10 digits  (10000000*acode+pnum), and assigning it a format to produce your desired layout, or
2. creating a character variable with the parentheses, dash, and internal blank.  You can do the latter using a couple of CATS functions nested inside a CATX function.  For example the area code part could be   CATS('(',acode,')').
Solution
‎10-03-2017 11:58 AM
PROC Star
Posts: 549

## Re: Formatting Phone Numbers with separated area codes

something like this

``````data want;
input phone \$ 1-17 date1 \$ 18-33 ;
phone = cat('(',substr(trim(phone),14),') ', substr(phone,1,3), '-',substr(phone,1,4));
date= input(substr(date1,6),mmddyy10.);
format date worddate20.;
drop date1;
datalines;
9136122 Code=999 Date=6/30/2001
;``````
Super User
Posts: 2,516

## Re: Formatting Phone Numbers with separated area codes

[ Edited ]

Your program outputs what you request. What's the issue?

phone date
(999) 913-9136 June 30, 2001

Oh wait, not quite...

Like this?

``````  PHONE = cat('(',scan(PHONE,2,'='), ') ', substr(PHONE,1,3), '-',SUBSTR(PHONE,4,4));
``````

PHONE DATE
(999 ) 913-6122 June 30, 2001

Super User
Posts: 6,939

## Re: Formatting Phone Numbers with separated area codes

SAS lets you read this type of data directly.  Here's the documentation:

http://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.2&docsetId=lestmtsref&docsetTarget=p1...

☑ This topic is solved.