## Add country code to phone numbers

Solved
Occasional Contributor
Posts: 15

# Add country code to phone numbers

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.

Accepted Solutions
Solution
‎11-17-2016 04:59 AM
PROC Star
Posts: 1,283

## Re: Add country code to phone numbers

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;

``````

All Replies
Super User
Posts: 23,689

## Re: Add country code to phone numbers

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

Occasional Contributor
Posts: 15

## Re: Add country code to phone numbers

Thank you so much!
Solution
‎11-17-2016 04:59 AM
PROC Star
Posts: 1,283

## Re: Add country code to phone numbers

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;

``````
Occasional Contributor
Posts: 15

## Re: Add country code to phone numbers

It worked! Thank you both so much!
☑ This topic is solved.

Discussion stats
• 4 replies
• 351 views
• 2 likes
• 3 in conversation