BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
pavank
Obsidian | Level 7
Data ds
infile datalines;
input name $40.;
datalines;
Virat Kohli
Surya Kumar Yadav
Rohit Sharma
Dhoni
Chetandra Pratap Singh Chauhan
;
run;

find firstname   middlename  lastname 

pavank_0-1714203596486.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
Data ds;
infile datalines;
input name $40.;
call scan(name,1,p1,l1,' ');
call scan(name,-1,p2,l2,' ');
first=scan(name,1,' ');
middle=substrn(name,p1+l1,p2-p1-l1);
last=scan(name,-1,' ');
drop p1 p2 l1 l2;
datalines;
Virat Kohli
Surya Kumar Yadav
Rohit Sharma
Dhoni
Chetandra Pratap Singh Chauhan
;
run;

View solution in original post

4 REPLIES 4
Ksharp
Super User
Data ds;
infile datalines;
input name $40.;
call scan(name,1,p1,l1,' ');
call scan(name,-1,p2,l2,' ');
first=scan(name,1,' ');
middle=substrn(name,p1+l1,p2-p1-l1);
last=scan(name,-1,' ');
drop p1 p2 l1 l2;
datalines;
Virat Kohli
Surya Kumar Yadav
Rohit Sharma
Dhoni
Chetandra Pratap Singh Chauhan
;
run;
PaigeMiller
Diamond | Level 26

Of course, this assumes there is a rule to find the middle name when there are four names or a rule to find the last name when there are three names. Here in the USA (and in many places), a possibility is that people have last name of "Van Gundy" and first name "Jeff". So the rules need to be flexible enough to handle this situation in general. @pavank how do you want to handle this situation?

 

It seems to me that the problem is better handled by not creating First Name and Middle Name and Last name columns from the Name column.

--
Paige Miller
sbxkoenk
SAS Super FREQ

Google search:

separate "first name" +"middle name" and "last name" site:communities.sas.com

Koen

Patrick
Opal | Level 21

If you've got the SAS Data Quality Server/Dataflux licensed then look into this module. Splitting names and addresses into their components is one of its out-of-the-box core functionalities and also deals with cases as mentioned by @PaigeMiller 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 319 views
  • 3 likes
  • 5 in conversation