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;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 476 views
  • 1 like
  • 2 in conversation