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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—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
  • 720 views
  • 1 like
  • 2 in conversation