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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
kiranv_
Rhodochrosite | Level 12

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
;

View solution in original post

5 REPLIES 5
Reeza
Super User

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

mkeintz
PROC Star

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,')').
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
kiranv_
Rhodochrosite | Level 12

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
;
ChrisNZ
Tourmaline | Level 20

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

 

 

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
  • 4416 views
  • 0 likes
  • 6 in conversation