lastword Return the last word from a delimited list of words lastword

SAS Macro Programs: lastword

$Version: 1.0 (26 Jan 2006)
Michael Friendly
York University


The lastword macro ( [download] get lastword.sas)

Return the last word from a delimited list of words

The LASTWORD macro returns the last word from a delimited list of words. This is useful for some generic forms of DATA Step BY processing with first. and last. BY processing.

Method

In Version 8+, it uses the %scan function in the form %scan(&words,-1,&sep) The original version of this macro is by Richard A. DeVenezia.

Usage

The LASTWORD macro is defined with positional parameters. The first parameter is required for any useful result. The macro is often used in a %LET statement, in the form

  %let lastclass = %lastword(&class);

Parameters

WORDS
A list of words separated by something
SEP
The word separator. [Default: SEP=%STR( )]

Examples

   %include macros(lastword);        *-- or include in an autocall library;
   %let string= Able Baker Charlie;
   %put Last word of "&string" is "%lastword(&string)";
   %let classvar = Treatment Poison;
   data test;
       set animals;
       by &classvar;
       if first.%lastword(&classvar) then do;
           ...
           end;

See also