Conditionally Deleting First Digit in a Variable

Accepted Solution Solved
Reply
Contributor
Posts: 67
Accepted Solution

Conditionally Deleting First Digit in a Variable

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


Accepted Solutions
Solution
‎04-20-2017 10:27 PM
Super User
Super User
Posts: 7,430

Re: Conditionally Deleting First Digit in a 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)

View solution in original post


All Replies
PROC Star
Posts: 554

Re: Conditionally Deleting First Digit in a Variable

Is CellPhoneNo a numeric or character variable?

Solution
‎04-20-2017 10:27 PM
Super User
Super User
Posts: 7,430

Re: Conditionally Deleting First Digit in a 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)

Valued Guide
Posts: 2,175

Re: Conditionally Deleting First Digit in a Variable

RW9
surprising !
you miss so much value
Super User
Super User
Posts: 7,430

Re: Conditionally Deleting First Digit in a Variable

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.

Valued Guide
Posts: 2,175

Re: Conditionally Deleting First Digit in a Variable

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.

 

Valued Guide
Posts: 2,175

Re: Conditionally Deleting First Digit in a Variable

Formatting numeric in phone number style is/was a standard demo example for user-defined formats
Check the on-line doc
Super User
Posts: 9,691

Re: Conditionally Deleting First Digit in a Variable

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;
Valued Guide
Posts: 2,175

Re: Conditionally Deleting First Digit in a Variable

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

 

 

Valued Guide
Posts: 2,175

Re: Conditionally Deleting First Digit in a Variable

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

Super User
Super User
Posts: 6,502

Re: Conditionally Deleting First Digit in a Variable

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;

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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