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:
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.