SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mayasak
Quartz | Level 8

Hi,

I have data sets that have mixed formats of phone numbers such as:

+1 (915) 111-1111

+ (202) 509-3114

+1 (915) 1234567

+ (202) 5093114

(777) 123-4567

3334445678

123.222.1334

How can I standardize the numbers into the following format:

717-333-4545

Thank you

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

Another task for one of my favourite functions: prxchange:

data have;
   input phoneNumber $20.;
   datalines;
+1 (915) 111-1111
+ (202) 509-3114
+1 (915) 1234567
+ (202) 5093114
(777) 123-4567
3334445678
123.222.1334
;

data want;
   set have;
   
   phoneNumber = prxchange('s/.*(\d{3})\D*(\d{3})\D*(\d{4})/$1-$2-$3/', -1, phoneNumber);
run;

.* matches anything

(\d{3}) = three digits, the brackets create a capture-group

\D* = zero or more non-digits

$1 = the first capture-group

View solution in original post

5 REPLIES 5
pdhokriya
Pyrite | Level 9
Hey, check with this

data test;
col1 = "+1 (915) 111-1111";
output;
col1 = "+ (202) 509-3114";
output;
col1 = "+1 (915) 1234567";
output;
col1 = "+ (202) 5093114";
output;
col1 = "(777) 123-4567";
output;
col1 = "3334445678";
output;
col1 = "123.222.1334";
output;
run;

data want;
set test;
col2 = tranwrd (col1,"+1","");
col3 = compress(col2, '(+. -)', 'a');
col4 = catx("-",substr(col3,1,3), substr(col3,4,3), substr(col3,7,4));
run;
andreas_lds
Jade | Level 19

Another task for one of my favourite functions: prxchange:

data have;
   input phoneNumber $20.;
   datalines;
+1 (915) 111-1111
+ (202) 509-3114
+1 (915) 1234567
+ (202) 5093114
(777) 123-4567
3334445678
123.222.1334
;

data want;
   set have;
   
   phoneNumber = prxchange('s/.*(\d{3})\D*(\d{3})\D*(\d{4})/$1-$2-$3/', -1, phoneNumber);
run;

.* matches anything

(\d{3}) = three digits, the brackets create a capture-group

\D* = zero or more non-digits

$1 = the first capture-group

mayasak
Quartz | Level 8

This actually worked thank you so much

ballardw
Super User

Do you have any international numbers? These may show up as 2 digits in front of other digits and not in a typical 3 digit area code , 3 digit exchange and 4 digit block.

 

Learned this the hard way with code similar to @andreas_lds used on some international numbers in the middle of data that was mostly US that moved them from Central America to Colorado because the local phone number was 2 digits shorter than than the US and the international code provided just enough to replace them and make something that looked like a US phone number.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 3330 views
  • 7 likes
  • 5 in conversation