- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data have;
input Phone :$20.;
datalines;
111-222-3333
111-222-4444
111-2225555
;
data want;
set have;
NewPhone = compress(phone, '-');
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data have;
input Phone :$20.;
datalines;
111-222-3333
111-222-4444
111-2225555
;
data want;
set have;
NewPhone = compress(phone, '-');
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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,
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.