Solved
New Contributor
Posts: 3

# How to extract the Name into Fname and Lname?

?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

Accepted Solutions
Solution
‎06-20-2013 11:49 AM
Posts: 3,167

## Re: How to extract the Name into Fname and Lname?

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

All Replies
Super User
Posts: 10,784

## Re: How to extract the Name into Fname and Lname?

```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

New Contributor
Posts: 3

## Re: How to extract the Name into Fname and Lname?

Hi,

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

Regards,

Sarath sankar V

Super User
Posts: 10,784

## Re: How to extract the Name into Fname and Lname?

Why? I have no such problem.

New Contributor
Posts: 3

## Re: How to extract the Name into Fname and Lname?

Hi ,

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

Regards

Sarath Sankar V

Solution
‎06-20-2013 11:49 AM
Posts: 3,167

## Re: How to extract the Name into Fname and Lname?

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

🔒 This topic is solved and locked.