DATA Step, Macro, Functions and more

Split the string into two parts.

Accepted Solution Solved
Reply
Super Contributor
Posts: 276
Accepted Solution

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


Accepted Solutions
Solution
‎05-09-2013 11:32 AM
Respected Advisor
Posts: 3,799

Re: Split the string into two parts.

Posted in reply to Anotherdream

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;

View solution in original post


All Replies
Super Contributor
Posts: 418

Re: Split the string into two parts.

Posted in reply to kuridisanjeev

data answer;

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)

Super Contributor
Posts: 276

Re: Split the string into two parts.

Posted in reply to Anotherdream

Hi

Your code working perfectly.Thanks lot.

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

Solution
‎05-09-2013 11:32 AM
Respected Advisor
Posts: 3,799

Re: Split the string into two parts.

Posted in reply to Anotherdream

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;
Super Contributor
Posts: 276

Re: Split the string into two parts.

Posted in reply to kuridisanjeev

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

Contributor
Posts: 65

Re: Split the string into two parts.

Posted in reply to kuridisanjeev

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 Smiley Wink

Karl

Super Contributor
Posts: 418

Re: Split the string into two parts.

Posted in reply to kuridisanjeev

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

Respected Advisor
Posts: 3,799

Re: Split the string into two parts.

Posted in reply to Anotherdream

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
Posts: 10,018

Re: Split the string into two parts.

Posted in reply to kuridisanjeev

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

Respected Advisor
Posts: 3,156

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

Contributor
Posts: 65

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". Smiley Happy

Super User
Posts: 10,018

Re: Split the string into two parts.

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

Ksharp

Super Contributor
Posts: 276

Re: Split the string into two parts.

Posted in reply to kuridisanjeev

Thank you all for your valuable suggestions.

Regards.

Sanjeev.K

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 12 replies
  • 1253 views
  • 7 likes
  • 6 in conversation