BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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;

View solution in original post

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

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

@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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 3 replies
  • 151 views
  • 3 likes
  • 4 in conversation