Hi all,
I have a phone number variable that has '-' in the middle. I would like to take numbers only.
This is what I want.
Phone num_phone (what I want)
111-222-3333 1112223333
111-222-4444 1112224444
111-2225555 1112225555
Thank you in advance.
data have;
input Phone :$20.;
datalines;
111-222-3333
111-222-4444
111-2225555
;
data want;
set have;
NewPhone = compress(phone, '-');
run;
data have;
input Phone :$20.;
datalines;
111-222-3333
111-222-4444
111-2225555
;
data want;
set have;
NewPhone = compress(phone, '-');
run;
Hi @cphd
Here are two methods to achieve this:
data want;
set have;
NewPhone = prxchange('s/-//',-1,phone);
run;
data want;
set have;
NewPhone = compress(phone, '-');
run;
Best,
Just in case your data contains other special characters such as parentheses, I would use:
NewPhone = compress(phone,,'kd');
As a third parameter, KD = Keep Digits
@cphd wrote:
Hi all,
I have a phone number variable that has '-' in the middle. I would like to take numbers only.
This is what I want.
Phone num_phone (what I want)
111-222-3333 1112223333
111-222-4444 1112224444
111-2225555 1112225555
Thank you in advance.
By any chance to you have international phone numbers in you data?
I worked with a process that did this to "clean" phone numbers and then found that certain international phone numbers with different number construction would fit in the US 10 digit dial string but were now considered to be in US because the first two digits were the country code and the remaining eight were the number within the country.
So if you do not know if you have international numbers you might check to see if you have any dialing strings with a dash in the third position.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: