Hi Experts,
i have a data like below in that i need to separate into words only clue we have is starting from the second Upper letter is next word.
for example if your take PraveenKumar i need to separate two words Praveen and Kumar.
Can anyone please help me how to separate the data into two words.
data wanted;
x='PraveenKumar';output;
x='RamSita';output;
x='JorgePaul';output;
run;
You can solve it by using regular expressions:
data want;
set wanted;
a = prxmatch('/[a-z][A-Z]/', x);
fname = substr(x,1,a);
lname = substr(x,a+1);
run;
Prxmatch looks for the first combination och lowcase character followed by an uppercase character 🙂
//Fredrik
This solution trawls through the text and puts a space next to any capital letter (so if there are 3 it will turn into 3 words)
data have;
x='PraveenKumar';output;
x='RamSita';output;
x='JorgePaul';output;
run;
data want(drop = i letter);
set have;
attrib y length=$50;
y = "";
do i = 1 to length(x);
letter = substr(x,i,1);
if letter = upcase(letter) then y = catx(" ",y,letter);
else y = cats(y,letter);
end;
run;
You can solve it by using regular expressions:
data want;
set wanted;
a = prxmatch('/[a-z][A-Z]/', x);
fname = substr(x,1,a);
lname = substr(x,a+1);
run;
Prxmatch looks for the first combination och lowcase character followed by an uppercase character 🙂
//Fredrik
data have;
x='PraveenKumar';output;
x='RamSita';output;
x='JorgePaul';output;
run;
data want;
set have;
fname= char(compress(x,,'KU'),1);
lname= char(compress(x,,'KU'),2);
run;
Your code is incorrect. Needs tweaking. Char is not the function to use I am afraid
data have;
x='PraveenKumar';output;
x='RamSita';output;
x='JorgePaul';output;
run;
data want;
set have;
_iorc_=findc (x,,'U');
First_name=substr(x,_iorc_,findc (x,,'U', _iorc_ + 1)-_iorc_);
Last_name=substr(x,findc (x,,'U', _iorc_ + 1));
run;
And another one
data have;
x='PraveenKumar';output;
x='RamSita';output;
x='JorgePaul';output;
run;
data want;
set have;
First_name=substr(x,anyupper (x),anyupper (x,2)-1);
Last_name=substr(x,anyupper(x,2));
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.