BookmarkSubscribeRSS Feed
gurpreetkaur
Fluorite | Level 6

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.;
datalines;
gurpreetkaur
anujmadan
dhirajdhingra
mehakbhasin
sahibakhurana
;
run;

 

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

6 REPLIES 6
SASKiwi
PROC Star

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.

 

Jagadishkatam
Amethyst | Level 16
I agree with saskiwi on this. We need a pattern or some information which helps differentiate the surname and last name
Thanks,
Jag
DartRodrigo
Lapis Lazuli | Level 10

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.);
end;
run;

%mend;

%name(g,9,6 ,1,8);
%name(a,5,6 ,1,4);
%name(d,7,10,1,9);
%name(m,6,10,1,5);
%name(s,7,10,1,6);

 And then append.

 

Hope this helps

MaikH_Schutze
Quartz | Level 8

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.

gurpreetkaur
Fluorite | Level 6
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.
ballardw
Super User

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 2528 views
  • 2 likes
  • 6 in conversation