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.
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
;
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.
You have a choice of
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
;
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 lets you read this type of data directly. Here's the documentation:
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.
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.