BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kuridisanjeev
Quartz | Level 8

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
data_null__
Jade | Level 19

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

12 REPLIES 12
Anotherdream
Quartz | Level 8

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)

kuridisanjeev
Quartz | Level 8

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

data_null__
Jade | Level 19

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;
kuridisanjeev
Quartz | Level 8

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

KarlK
Fluorite | Level 6

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

Anotherdream
Quartz | Level 8

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

data_null__
Jade | Level 19

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:

Ksharp
Super User

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

Haikuo
Onyx | Level 15

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

KarlK
Fluorite | Level 6

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

Ksharp
Super User

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

Ksharp

kuridisanjeev
Quartz | Level 8

Thank you all for your valuable suggestions.

Regards.

Sanjeev.K

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 12 replies
  • 7273 views
  • 8 likes
  • 6 in conversation