problem with scan function

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 81
Accepted Solution

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
Respected Advisor
Posts: 3,775

Re: problem with scan function

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;

View solution in original post


All Replies
Solution
‎09-14-2014 01:57 PM
Respected Advisor
Posts: 3,775

Re: problem with scan function

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

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

Why did you use 4 semi-colons here??

Respected Advisor
Posts: 3,775

Re: problem with scan function

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

Grand Advisor
Posts: 9,571

Re: problem with scan function

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.

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

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