Quartz | Level 8

Split the string into two parts.

Hello Everyone..

I my dataset i have a column called "Name" which contain First name and last name of the person. Unfortunately there is no delimiter between first name and last name.So i want to add a space between First name and last name.

The only Clue i have to identify the firstname and last name is ,Both names should start with Capital letter and rest of them  all is small letters.

Like

SanjeevKumar

YaswanthJuluri.

In above names First name is Sanjeev,Yaswanth and Last name is Kuridi and Juluri.

Hope you people understand my requirement.

Thanks&Regards.

Sanjeev.K

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Split the string into two parts.

That seems overly complicated.

data _null_;

input name \$40.;
p = anyupper(name,
2);
First = substrn(name,1,p-1);
Last = substrn(name,p);

put _all_;

cards;
SanjeevKumar
YaswanthJuluri
Juluri
;;;;
run;
12 REPLIES 12
Quartz | Level 8

Re: Split the string into two parts.

set yourdataset;

Firstname=substr(name,1,anyupper(name,-(length(name)+10))-1);

lastname=substr(name,anyupper(name,-(length(name)+10)),length(name)+5);

run;

There ya go

The any upper function, with a negative second value > the total length of string searches from right to left. So it searches for the first Upper string from the right side of the string. Therefore the first name is simply Substr(1- VAR-1)

Quartz | Level 8

Re: Split the string into two parts.

Hi

But looking bit complex.

Just now i tried in another way,

Here we go.....:-)

Data Name1;

set mydataset;

Firstname=substr(name,1,notlower(substr(name,2)));

Lastname=substr(name,length(firstname)+1);

run;

Regards.

Sanjeev.K

Re: Split the string into two parts.

That seems overly complicated.

data _null_;

input name \$40.;
p = anyupper(name,
2);
First = substrn(name,1,p-1);
Last = substrn(name,p);

put _all_;

cards;
SanjeevKumar
YaswanthJuluri
Juluri
;;;;
run;
Quartz | Level 8

Re: Split the string into two parts.

Lets see if any one provides more simpler then above...

Fluorite | Level 6

Re: Split the string into two parts.

Not to be a buzzkill, but in the U.S., we have names like MaryAnneSmith or CecildeMille, where you might need to do a little exception processing.

Pardon my OCD

Karl

Quartz | Level 8

Re: Split the string into two parts.

This is actually why I did my code the way I did Karl. If the name has more than one uppercase, the first two sections will be put into the first name.

MaryAnne  Smith.

However if you have sir names like

EgglyMcBagelface

Then _null_'s code would give the correct Eggly McBagelface. So theoritically you have to be cautious using either approach, or change your code quite a bit to look out for 3 capital letters.

_null_, for the record, you sir are a wizard.

Brandon

Re: Split the string into two parts.

Basically if these are indeed proper names then just aint' no good way to do it.

If I was a wizard I'd clone myself and go fishing.  I do know my functions, some of them anyway.:smileylaugh:

Super User

Re: Split the string into two parts.

PRX is more powerful .

data _null_;

input name \$40.;

length first middle last \$ 40;

re = prxparse('/([A-Z][a-z]*)([A-Z][a-z]*)?([A-Z][a-z]*)?/o');

if prxmatch(re, name) then

do;

first = prxposn(re, 1, name);

middle = prxposn(re, 2, name);

last = prxposn(re, 3, name);

output;

end;

put _all_;

cards4;

SanjeevKumar

YaswanthJuluri

Juluri

MaryAnneSmith

CecildeMille

;;;;

run;

Ksharp

Onyx | Level 15

Re: Split the string into two parts.

Liked, PRX is indeed powerful. However, in term of some special cases mentioned above: MaryAnneSmith, where "MaryAnne" is first name in one piece; while in the case of EgglyMcDonald,  "McDonald" is the last name. That is just how people spell those names in Western culture. So if they are important to OP, then you may have to come up with a dictionary table to handle that.

Haikuo

Fluorite | Level 6

Re: Split the string into two parts.

I think this poor equine is totally deceased by now, and there's no way my meager skills would argue with _null_ or Ksharp. I only jumped into the fray because, as we all know, people's names are tricky, and I wanted to encourage the OP to check the results of whatever solution he selected for exceptions.

Just as a postscript: OK, I cheated on Cecil B. DeMille by purposely messing with the orthography (deMille vs. DeMille). Nevertheless, I think the great Cecil B. would take issue with all the proffered solutions, as they all give his first name as "Cecilde".

Super User

Re: Split the string into two parts.

For this situation, I took HaiKuo's advice , make a dictionay table to maintain it.

Ksharp

Quartz | Level 8

Re: Split the string into two parts.

Thank you all for your valuable suggestions.

Regards.

Sanjeev.K

Discussion stats
• 12 replies
• 7273 views
• 8 likes
• 6 in conversation