how to extract First ,Middle , Last name from mail id ?
DATA emails; INPUT email $50.;
DATALINES;
;
RUN;
DATA emails;
INPUT email $50.;
DATALINES;
john.doe@example.com
jane.smith@company.org
robert.johnson@email.net
sarah.lee-wong@domain.co.uk
mary.jane.parker@email.com
tom.m.hanks@movies.com
a.einstein@physics.edu
;
RUN;
data want;
set emails;
temp=scan(email,1,'@');
if countw(temp,,'ka')=3 then do;
first=scan(temp,1,,'ka');
middle=scan(temp,2,,'ka');
last=scan(temp,3,,'ka');
end;
else do;
first=scan(temp,1,,'ka');
last=scan(temp,-1,,'ka');
end;
drop temp;
run;
DATA emails;
INPUT email $50.;
DATALINES;
john.doe@example.com
jane.smith@company.org
robert.johnson@email.net
sarah.lee-wong@domain.co.uk
mary.jane.parker@email.com
tom.m.hanks@movies.com
a.einstein@physics.edu
;
RUN;
data want;
set emails;
temp=scan(email,1,'@');
if countw(temp,,'ka')=3 then do;
first=scan(temp,1,,'ka');
middle=scan(temp,2,,'ka');
last=scan(temp,3,,'ka');
end;
else do;
first=scan(temp,1,,'ka');
last=scan(temp,-1,,'ka');
end;
drop temp;
run;
In case you have more than one middle name:
data want;
set emails;
length first_name middle_names last_name $50;
whole_name = scan(email,1,"@");
first_name = scan(whole_name,1,".");
last_name = scan(whole_name,-1,".");
num_middle = countw(whole_name,".");
do i = 2 to num_middle - 1;
middle_names = catx(" ",middle_names,scan(whole_name,i,"."));
end;
drop whole_name i num_middle;
run;
@animesh123 wrote:
how to extract First ,Middle , Last name from mail id ?
DATA emails; INPUT email $50.;
DATALINES;
;
RUN;
Why do you assume that e-mails have a distinct separator character between the names? I see lots of email names that don't have clear separators, and maybe the first name is just the initial, like my email pmiller1@nameofmycompany.com. Or first name and last name not separated by anything, like fredflintstone@prehistoric.com, or emails that have lastname followed by firstname flintstonewilma@prehistoric.com.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.