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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.