BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SarathSV
Calcite | Level 5

?data extract;

length Name $15.;

input Name $ Age;

cards;

TomHanks    39

WillSmith     35

ChristopherLee 38

RusselCrow  39

;

how I could extract the Name variable into Fname and Lname

Like

Fname   Lname      Age

Tom       Hanks       39

Wll         Smith        35

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

Or just using Prxchange:

data extract;

length Name $15.;

input Name $ Age;

cards;

TomHanks 39

WillSmith 35

ChristopherLee 38

RusselCrow 39

;

data want;

  set extract;

  fname=prxchange('s/([A-Z][a-z]+)([A-Z][a-z]+)/$1/o',-1, name);

  lname=prxchange('s/([A-Z][a-z]+)([A-Z][a-z]+)/$2/o',-1, name);

  run;

Or for a none PRX approach,

data want_noprx;

  set extract;

  lp=anyupper(substr(name,2));

  fname=substr(name,1,lp);

  lname=substr(name,lp+1);

  run;

Haikuo

View solution in original post

5 REPLIES 5
Ksharp
Super User
data extract;
length Name $15.;
input Name $ Age;
cards;
TomHanks    39
WillSmith     35
ChristopherLee 38
RusselCrow  39
;
data FirstLastNames;
length first last $ 16;
re = prxparse('/([A-Z][a-z]+)([A-Z][a-z]+)/o');
set extract;
if prxmatch(re, name) then
do;
last = prxposn(re, 1, name);
first = prxposn(re, 2, name);
end;
run;

Ksharp

SarathSV
Calcite | Level 5

Hi,

I am getting the result but in the output variable 'last' is only displaying  variable  'first' is not displaying.

Regards,

Sarath sankar V

Ksharp
Super User

Why? I have no such problem.

SarathSV
Calcite | Level 5

Hi ,

Thanks Sharp I got it it was some mistake done by me . be in touch.

Regards

Sarath Sankar V

Haikuo
Onyx | Level 15

Or just using Prxchange:

data extract;

length Name $15.;

input Name $ Age;

cards;

TomHanks 39

WillSmith 35

ChristopherLee 38

RusselCrow 39

;

data want;

  set extract;

  fname=prxchange('s/([A-Z][a-z]+)([A-Z][a-z]+)/$1/o',-1, name);

  lname=prxchange('s/([A-Z][a-z]+)([A-Z][a-z]+)/$2/o',-1, name);

  run;

Or for a none PRX approach,

data want_noprx;

  set extract;

  lp=anyupper(substr(name,2));

  fname=substr(name,1,lp);

  lname=substr(name,lp+1);

  run;

Haikuo

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 1164 views
  • 4 likes
  • 3 in conversation