## problem with scan function

Solved
Frequent Contributor
Posts: 81

# problem with scan function

Hi,

I am trying to extract the the first name with middle name if it is available in the name but not getting a way to find the same. Can anyone help me in doing so..

data original;

input Name \$ 30.;

datalines;

Jeffrey Smith

Ron Cody

Alan Wilson

Alfred E. Newman

;

%print (original);

data first_last;

set original;

length First Last \$ 15;

FirsNmiddle = scan(Name,1,' ');

run;

Accepted Solutions
Solution
‎09-14-2014 01:57 PM
Posts: 3,852

## Re: problem with scan function

Posted in reply to AnandSahu
data original;
input Name \$30.;

* can you assume last name is 1 "word" ?;

call scan(name,-1,p,l,' ');
firstNMI = substrn(name,1,p-1);
last = substrn(name,p);

datalines;
Jeffrey Smith
Ron Cody
Alan Wilson
Alfred E. Newman
;;;;
run;
proc print;

run;

All Replies
Solution
‎09-14-2014 01:57 PM
Posts: 3,852

## Re: problem with scan function

Posted in reply to AnandSahu
data original;
input Name \$30.;

* can you assume last name is 1 "word" ?;

call scan(name,-1,p,l,' ');
firstNMI = substrn(name,1,p-1);
last = substrn(name,p);

datalines;
Jeffrey Smith
Ron Cody
Alan Wilson
Alfred E. Newman
;;;;
run;
proc print;

run;
Frequent Contributor
Posts: 81

## Re: problem with scan function

Posted in reply to data_null__

thanx for helping me....I have one more question.

Why did you use 4 semi-colons here??

Posts: 3,852

## Re: problem with scan function

Posted in reply to AnandSahu

No reason, they would be needed with CARDS4 but not here.  Just something I like to do.

Super User
Posts: 10,778

## Re: problem with scan function

Posted in reply to AnandSahu
```data original;
input Name \$30.;
* can you assume last name is 1 "word" ?;
firstNMI = prxchange('s/\w+\$//o',1,strip(name));
datalines;
Jeffrey Smith
Ron Cody
Alan Wilson
Alfred E. Newman
;;;;
run;
```

Xia Keshan

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
• 4 replies
• 273 views
• 0 likes
• 3 in conversation