BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
pavank
Quartz | Level 8
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 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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