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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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