- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
I'm sure this topic has been done to death, but I have a relatively simple problem.
I have about a hundred phone numbers in the format 333--2964555 I want to put it into the format (555) 333-2964. I can add in the parentheses, spaces and dashes, but I can't figure out how to move the last 3 numbers to the front i.e so that 3332964555 becomes 5553332964 after my compress function.
I've just attached my code for 3 of the numbers;
data phone_a;
input ph $1-16;
datalines;
333--2964555
376--4232444
445--6777555
;
data b;
set phone_a;
base=compress(ph,"--.");
data c;
set b;
Phone_number=input(base,12.);
proc format;
picture phone_b (default=16)
low-high='999) 999-9999'
(prefix='(');
run;
proc print data=c;
format Phone_number phone_b.;
run;
Thank you very much
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Perl Regular expression will be very handy in this situation:
data phone_a;
input ph $1-16;
ph_new=prxchange('s/(\d{3})(--)(\d{3})/(\1)\3-/o',1,ph);
datalines;
333--2964555
376--4232444
445--6777555
;
ion
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Perl Regular expression will be very handy in this situation:
data phone_a;
input ph $1-16;
ph_new=prxchange('s/(\d{3})(--)(\d{3})/(\1)\3-/o',1,ph);
datalines;
333--2964555
376--4232444
445--6777555
;
ion
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data phone_a; input ph $1-16; datalines; 333--2964555 376--4232444 445--6777555 ; run; data want; set phone_a; want=cats('(',substr(ph,length(ph)-2),')',substr(ph,1,length(ph)-3)); run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
One side issue to note here ... when the CATS function creates a new variable, SAS assigns the new variable a length of $200.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Here is Ron Cody's tip for standardizing phone numbers in SAS.
https://blogs.sas.com/content/sastraining/2017/05/26/standardizing-phone-numbers-using-sas/
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@Rick_SAS wrote:
Here is Ron Cody's tip for standardizing phone numbers in SAS.
https://blogs.sas.com/content/sastraining/2017/05/26/standardizing-phone-numbers-using-sas/
One caution about standardizing phone numbers: Find out if the data may have internation numbers before you start.
We had a process similar to Ron Cody's for standardizing phone numbers and had issues with lots of responses when called of "I have no idea what you are talking about." The data turned out to have international numbers (South America) and some of the numbers when standardized created valid US phone numbers from international code+phone number. Which obviously were not the correct contacts.