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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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