BookmarkSubscribeRSS Feed
honeyblue
Calcite | Level 5

I have a variable name with value NaveenKumar, HarishChandra, SivaKumar

But I want to separate as firstname and last name

 

ex Firstname ------------Naveen lastname   ----Kumar

 

2 REPLIES 2
andreas_lds
Jade | Level 19

@honeyblue wrote:

I have a variable name with value NaveenKumar, HarishChandra, SivaKumar

But I want to separate as firstname and last name

 

ex Firstname ------------Naveen lastname   ----Kumar

 


Do you have one variable containing the complete string or is the comma meant to separate observations?

andreas_lds
Jade | Level 19

Try:

data have;
   length name $ 40;
   input name;
   datalines;
NaveenKumar
HarishChandra
SivaKumar
;
run;

data want;
   set have;
   
   length firstname lastname $ 30;
   retain rx;
   drop rx;
   
   if _n_ = 1 then do;
      rx = prxparse('/([A-Z][a-z]+)([A-Z][a-z]+)/');
   end;
   
   if prxmatch(rx, name) then do;
      firstname = prxposn(rx, 1, name);
      lastname = prxposn(rx, 2, name);
   end;
   else do;
      put 'ERROR: Unable to extract firstname/lastname from data';
      put _all_;
   end;
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!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 371 views
  • 1 like
  • 2 in conversation