BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
JT99
Obsidian | Level 7
Hello! I Need to add country code(0063)to a list of phone numbers. My data looks like this:
Phone
4345121
464158245
29767111
998625676
63465126026
639208907675

My problem is that the numbers are a mix of cellphone and landline with some numbers already have the country code(63).
The conditions are these:
1.If phone length <= 7 then newphone is 00632phone
2.If phone length=8 and frst digit =2 then newphone is 0063phone
3.If phone length=9 and first two digits are in(46,32,82 etc,) then newphone is 0063phone.
4.if phone length =11 and first two digits = 63 then newphone is 00phone
5. If phone length is 9 and first digit is 9 then newphone is 0063phone
6. If phone length is 12 and first 3 digits are 639 then newphone is 00phone.

Newphone should look like this:
006324345121
0063464158245
006329767111
0063998625676
0063465126026
00639208907675

Any help will be much appreciated.
1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

like this 🙂

 

data phonenumbers;
input phone $;
format phone $12.;
datalines;
4345121
464158245
29767111
998625676
63465126026
639208907675
;

data want(drop = code);
   set phonenumbers;
   if length(Phone) <= 7 then code = '00632';
   else if length(Phone) = 8 then code = '0063';
   else if length(Phone) = 9 and substr(Phone, 1, 2) in ('46', '32', '82') then code = '0063';
   else if length(Phone) = 11 and substr(Phone, 1, 2) = '63' then code = '00';
   else if length(Phone) = 9 and substr(Phone, 1, 1) = '9' then code = '0063';
   else if length(Phone) = 12 and substr(Phone, 1, 3) = '639' then code = '00';

   newphone = cats(code, phone);
   format newphone $12.;
run;

View solution in original post

4 REPLIES 4
Reeza
Super User

Your rules are relatively clear, you can turn them into if/then statements. 

 

Use length to get the length of the phone number and Catt to add prefix. 

This assumes you have a character variable, but use PUT if you need to convert it. 

 

If length(phone) <= 7 then new_phone = catt('00632', phone);

else if length(phone)=8 and substr(phone, 1, 1) = '2' then new_phone = catt('0063', phone);

else ... etc

JT99
Obsidian | Level 7
Thank you so much!
PeterClemmensen
Tourmaline | Level 20

like this 🙂

 

data phonenumbers;
input phone $;
format phone $12.;
datalines;
4345121
464158245
29767111
998625676
63465126026
639208907675
;

data want(drop = code);
   set phonenumbers;
   if length(Phone) <= 7 then code = '00632';
   else if length(Phone) = 8 then code = '0063';
   else if length(Phone) = 9 and substr(Phone, 1, 2) in ('46', '32', '82') then code = '0063';
   else if length(Phone) = 11 and substr(Phone, 1, 2) = '63' then code = '00';
   else if length(Phone) = 9 and substr(Phone, 1, 1) = '9' then code = '0063';
   else if length(Phone) = 12 and substr(Phone, 1, 3) = '639' then code = '00';

   newphone = cats(code, phone);
   format newphone $12.;
run;

JT99
Obsidian | Level 7
It worked! Thank you both so much!

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
  • 4 replies
  • 2071 views
  • 2 likes
  • 3 in conversation