www.sas.com > Service and Support > Technical Support
 
Technical Support SAS - The power to know(tm)
  TS Home | Intro to Services | News and Info | Contact TS | Site Map | FAQ | Feedback

/**********************************************************************/
/* Title:  Using the SCAN Function                                    */
/*                                                                    */
/* Goal:   Break up a character variable into separate variables.     */
/*                                                                    */
/**********************************************************************/

/* In SAS Version 7 and beyond, the SCAN function allows you to       
   specify a negative value for selecting words in the character      
   value.  If a negative value is used, the SCAN function searches    
   the character value from the end and scans the value from right    
   to left.  Prior to Version 7, the SCAN function only can search    
   from left to right.                                                
   
   In the following examples, the fullname variable is scanned to     
   break up into separate first name and last name variables. 
*/
                                                                        
                                                                        
/* V7 and beyond Method */                                              
                                                                        
data names;                                                             
  infile cards truncover;                                  
  input Fullname $upcase30.;                               
  First=scan(Fullname,1,' ');                              
  Second=scan(Fullname,2,' ');                             
  Last=scan(Fullname,-1,' ');                              
  if First in ('MRS.','MR.','DR.') and Second ne Last then 
     First=scan(Fullname,2,' ');                           
  drop Second;                                             
datalines;                                                   
Mrs. Smith                                               
Bradly Horton                                            
Holly Dunn                                               
Mr. Jim Rempkin                                          
Mike Logan                                               
Dr. Susan Ross                                           
;                                                        

proc print;                                              
run;                  


/* V6 Method */                                                
                                                               
data names;                                                    
  infile cards truncover;                                        
  input Fullname $upcase30.;                                     
  First=scan(Fullname,1,' ');                                    
  Second=scan(Fullname,2,' ');                                   
  Third=scan(Fullname,3,' ');                                    
  if Third=' ' then Last=second;                                 
  if First in ('MRS.','MR.','DR.') and Second ne Last then do;   
     First=Second;                                               
     Last=Third;                                                 
  end;                                                           
  drop Second Third;                                             
cards;                                                         
Mrs. Smith                                                     
Bradly Horton                                                  
Holly Dunn                                                     
Mr. Jim Rempkin                                                
Mike Logan                                                     
Dr. Susan Ross                                                 
;                                                              

proc print;                                                    
run;                                


/* RESULTS (Both methods) */ 
                                                                     
                The SAS System                                       
                                                                     
 Obs    Fullname           First     Last                            
                                                                     
  1     MRS. SMITH         MRS.      SMITH                           
  2     BRADLY HORTON      BRADLY    HORTON                          
  3     HOLLY DUNN         HOLLY     DUNN                            
  4     MR. JIM REMPKIN    JIM       REMPKIN                         
  5     MIKE LOGAN         MIKE      LOGAN                           
  6     DR. SUSAN ROSS     SUSAN     ROSS                            
                                                                     
                              
Copyright (c) 2004 SAS Institute Inc. All Rights Reserved.
Terms of Use & Legal Information | Privacy Statement