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

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
FredrikE
Rhodochrosite | Level 12

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

View solution in original post

8 REPLIES 8
DanielLangley
Quartz | Level 8

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;
sasismylife
Fluorite | Level 6
Hi Daniel,

Thanks for your quick response:)
FredrikE
Rhodochrosite | Level 12

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

sasismylife
Fluorite | Level 6
Thanks Fredrik for your quick response.:)
kiranv_
Rhodochrosite | Level 12
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;
novinosrin
Tourmaline | Level 20

Your code is incorrect. Needs tweaking. Char is not the function to use I am afraid

novinosrin
Tourmaline | Level 20
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;
novinosrin
Tourmaline | Level 20

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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 8 replies
  • 2530 views
  • 3 likes
  • 5 in conversation