Hello!
I have a variable called CellPhoneNo (cell phone numbers) that I want to look like this: 555-222-3333. This is what most look like:
5552223333
but some people included a 1 so now I also have:
15552223333
My goal is to add a format to change this to
555-222-3333.
How can I do this if some numbers start with a 1? I was thinking of eliminating the "1" and then applying the format. But is there a way to just add a dash in starting from the right, 4 digits in, and then keep going after 3, and then after 1 place in case there is a "1" at the beginning?
Thanks!
Best,
Gina
Here we are again. Post test data in the form of a datastep.
Personally I use formats very little, so I would just do:
phone_no=catx('-',substr(var,lengthn(var)-9,3),substr(var,lengthn(var)-6,3),substr(var,lengthn(var)-3));
Not seeing the point of keeping something which doesn't have to be numeric as numeric. (I.e. area codes in brackets)
Is CellPhoneNo a numeric or character variable?
Here we are again. Post test data in the form of a datastep.
Personally I use formats very little, so I would just do:
phone_no=catx('-',substr(var,lengthn(var)-9,3),substr(var,lengthn(var)-6,3),substr(var,lengthn(var)-3));
Not seeing the point of keeping something which doesn't have to be numeric as numeric. (I.e. area codes in brackets)
No, not really. Formats can be useful multi level formats in some procedures and formats on graphs. However I am really against any proprietary file format, compiled macros, functions, formats, etc. If I could use CSV like datasets I would drop datasets also. The move from 32bit to 64bit has really highlighted how restrictive and future falible such formats are.
while I won't argue SAS's case for the lack of migration support into 64bit from 32, I can only compare the scenario (incompatability of format catalogs between 32 and 64bit) to almost all other applications, where equally there is the equivalent of a transport file in .csv or .xml format -
here we should (easily) keep "permanent" formats converted to .csv (via cntlout ds) - providing no version dependency.
I would not recommend keeping macros in compiled catalogs - perhaps we should be saying the same about compiled formats.
Assuming it is numeric variable.
proc format;
picture fmt
other='000-000-0000';
run;
data have;
input x;
p=int(log10(x));
if p=10 then x=mod(x,10**p) ;
format x fmt.;
drop p;
cards;
5552223333
15552223333
;
run;
Hi @Ksharp
as the format is defined to present just those 10 digits, it is any removing leading digits when wider than 10, so I don't think it needs selective processing
peterC
Hi Gina @ginak
as I couldn't find it in current doc, here (thank you @Cynthia_sas ) is a link to an old posting about creating and using a user-defined format for phone numbers communities.sas.com/t5/SAS-Enterprise-Guide/format-phone-number/m-p/6001
hope it works for you
peterC
Interesting case of seeing how PICTURE formats work.
By using '0' instead of '9' for the digit locations you can allow SAS to basically trim on the left.
So this format definition will work for numberic values that are either with or without the leading 1 .
picture phonefmt other='0-000-000-0000';
So a little test.
1755 data have;
1756 input x;
1757 put x phonefmt. x comma15.;
1758 cards;
201-555-1234 2,015,551,234
1-212-555-9876 12,125,559,876
It doesn't work so well if you want to use () around the area code. Since then without the extra digit in front you do not get the opening paren.
201)555-1234 2,015,551,234
1(212)555-9876 12,125,559,876
If you want to add the extra one.
if . < phone < 10**10 then phone=phone+10**10;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.