BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ginak
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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)

View solution in original post

10 REPLIES 10
PeterClemmensen
Tourmaline | Level 20

Is CellPhoneNo a numeric or character variable?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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)

Peter_C
Rhodochrosite | Level 12
RW9
surprising !
you miss so much value
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Peter_C
Rhodochrosite | Level 12

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.

 

Peter_C
Rhodochrosite | Level 12
Formatting numeric in phone number style is/was a standard demo example for user-defined formats
Check the on-line doc
Ksharp
Super User

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;
Peter_C
Rhodochrosite | Level 12

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

 

 

Peter_C
Rhodochrosite | Level 12

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

Tom
Super User Tom
Super User

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;

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 1062 views
  • 8 likes
  • 6 in conversation