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
That seems overly complicated.
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)
That seems overly complicated.
Lets see if any one provides more simpler then above...
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
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
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:
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
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
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".
For this situation, I took HaiKuo's advice , make a dictionay table to maintain it.
Ksharp
Thank you all for your valuable suggestions.
Regards.
Sanjeev.K
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.