BookmarkSubscribeRSS Feed
deleted_user
Not applicable
I have phone number data in which the three “parts” of the phone number were originally stored as separate numeric variables so that any leading zeroes were dropped (e.g., “014” became 14 and “004” became 4. So a phone number of 004-017-0123 is now shown as 4-17-123.)

How can I re-seperate the numbers and re-add the dropped zeros?

Thanks.
5 REPLIES 5
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
In a DATA step, use the SCAN function to split the data-string into three, and also use the PUT(,Z3.) for example. And use CATX to rebuild the number from the components.

Scott Barry
SBBWorks, Inc.
deleted_user
Not applicable
Thanks Scott, almost there. When I use CATX to reform the phone numbers after splitting and adding the zeros, CATX drops off the zeros I added. Any ideas? My current code is below. Thanks.

Phone_Number=Translate(Phone_Number,'---','._/');

format Phone1 Phone2 z3. Phone3 z4.;
Phone1=scan(Phone_Number,1);
Phone2=scan(Phone_Number,2);
Phone3=scan(Phone_Number,3);

PhoneCorrect=catx('-',Phone1,Phone2,Phone3);
NickR
Quartz | Level 8
Phone1=input(scan(Phone_Number,1),best.);
Phone2=input(scan(Phone_Number,2),best.);
Phone3=input(scan(Phone_Number,3),best.);
PhoneCorrect=catx('-',put(Phone1,z3.),put(Phone2,z3.),put(Phone3,z4.));
Peter_C
Rhodochrosite | Level 12
change the parameters of catx() so they are strings not numerics - you were getting automatic numeric-to-character conversion

PhoneCorrect=catx('-', put(Phone1,z3.), put(Phone2,z3.), put(Phone3,z4.));
deleted_user
Not applicable
Thanks everyone. Things are looking great now.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1137 views
  • 0 likes
  • 4 in conversation