DATA Step, Macro, Functions and more

Separating first name and last name

Occasional Contributor
Posts: 17

Separating first name and last name

Hi Experts,


I need to separate first name and last name which do not have any delimiter specified.


I have the following data set:


data sample;
input name : $20.;


I need a dataset from the above dataset with ouput as:


first name last name
gurpreet kaur
anuj madan
dhiraj dhingra
mehak bhasin
sahiba khurana


how should I get the above data set??


Thanks !!

Gurpreet Kaur

Super User
Posts: 3,106

Re: Separating first name and last name

If there is no rule or pattern you can describe that identifies where the word boundaries are then there is no way to program it in SAS or any other computer language.


The only way I suspect this would work is if your software had access to an appropriate database of surnames and first names and you were able to do name matches. This is definitely not available in Base SAS.


Trusted Advisor
Posts: 1,128

Re: Separating first name and last name

I agree with saskiwi on this. We need a pattern or some information which helps differentiate the surname and last name
Regular Contributor
Posts: 212

Re: Separating first name and last name

Hi mate,


For this small table you can use multiple IFs and substr function to separate what you need.

But if your table is very large and there is no patterns, then you need a new way to get this table


I agree with Kiwi.


But for this small table you can do this:


%macro name(name,pos,len,pos1,len1);

data want_&name(where=(last_name ne ""));
set sample;
if substr(name,1,1) = "&name." then do;
last_name = substr(name,&pos. ,&len.);
first_name = substr(name,&pos1.,&len1.);


%name(g,9,6 ,1,8);
%name(a,5,6 ,1,4);

 And then append.


Hope this helps

Posts: 45

Re: Separating first name and last name

Agree with everyone above. Even something as simple as capital letter for each first and last name would help. Anything to determine the start position of each name element (including middle initials if there are any).


So if you altered the data in any way to make them all lowercase, then maybe you have the information you need, otherwise, you will need to request a new file with the appropriate delimiters.

Occasional Contributor
Posts: 17

Re: Separating first name and last name

Thank you all for your replies. I was asked this question in one of my interviews. I answered the same but the interviewer was not convinced and then I thought there may be a way to do this.
Super User
Posts: 10,500

Re: Separating first name and last name

It is possible that what you presented was not exactly what the interviewer asked or the data example you provided had some difference.

A very minor change such as the name parts starting with a capital letter would make it possible.

Ask a Question
Discussion stats
  • 6 replies
  • 6 in conversation