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
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;
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;
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.
Google search:
separate "first name" +"middle name" and "last name" site:communities.sas.com
Koen
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.