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;
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
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;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.