☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Obsidian | Level 7

## Extract First ,Middle , Last name from mail id ?

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: Extract First ,Middle , Last name from mail id ?

``````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;``````
3 REPLIES 3
Super User

## Re: Extract First ,Middle , Last name from mail id ?

``````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;``````
Super User

## Re: Extract First ,Middle , Last name from mail id ?

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;``````
Diamond | Level 26

## Re: Extract First ,Middle , Last name from mail id ?

@animesh123 wrote:

how to extract First ,Middle , Last name from mail id ?

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;

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.

--
Paige Miller
Discussion stats
• 3 replies
• 364 views
• 3 likes
• 4 in conversation