Sample 25238: Using the SCAN Function
Using the SCAN function from SAS Functions by Example
Suppose you want to produce an alphabetical list by last name, but your NAME variable contains FIRST, possibly
a middle initial, and LAST name. The SCAN function makes quick work of this. Note that the LAST_NAME variable
in PROC REPORT has the attribute of ORDER and NOPRINT, so that the list is in alphabetical order of last name but
all that shows up is the original NAME variable in First, Middle, and Last name order.
DATA FIRST_LAST;
INPUT @1 NAME $20.
@21 PHONE $13.;
***Extract the last name from NAME;
LAST_NAME = SCAN(NAME,-1,' '); /* Scans from the right */
DATALINES;
Jeff W. Snoker (908)782-4382
Raymond Albert (732)235-4444
Steven J. Foster (201)567-9876
Jose Romerez (516)593-2377
;
PROC REPORT DATA=FIRST_LAST NOWD;
TITLE "Names and Phone Numbers in Alphabetical Order (by Last Name)";
COLUMNS NAME PHONE LAST_NAME;
DEFINE LAST_NAME / ORDER NOPRINT WIDTH=20;
DEFINE NAME / DISPLAY 'Name' LEFT WIDTH=20;
DEFINE PHONE / DISPLAY 'Phone Number' WIDTH=13 FORMAT=$13.;
RUN;
About the Author
Ron Cody, Ed.D., is a professor and researcher at the Robert Wood Johnson Medical School in New Jersey as
well as a national instructor for SAS Institute Inc. A SAS user since 1977, Ron has the extensive knowledge and
innovative style that have made him a popular presenter at local, regional, and national SAS conferences.
He has authored or coauthored numerous books as well as countless articles in medical and scientific journals.
His books are available from the online bookstore.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
Suppose you want to produce an alphabetical list by last name, but your NAME variable contains FIRST, possibly a middle initial, and LAST name. The SCAN function makes quick work of this. This tip is from Ron Cody.
| Type: | Sample |
| Topic: | SAS Reference ==> Functions ==> Character SAS Reference ==> Procedures ==> REPORT Internal Administration ==> BBU Non SAS Authors ==> Ron Cody SAS Reference ==> DATA Step
|
| Date Modified: | 2006-01-11 03:03:01 |
| Date Created: | 2005-02-21 13:59:29 |
Operating System and Release Information
| SAS System | Base SAS | All | 8 TS M0 | n/a |