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 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 2592 views
  • 7 likes
  • 5 in conversation