Published on :
Utility Macro CREATION_INTERNE

Returns the last word from a delimited list

This code is also available in: Deutsch Français
The LASTWORD macro is designed to extract the last word from a specified character string. It accepts two parameters: 'words' (the input string containing the words) and 'sep' (the character(s) used as delimiters between words, with a space by default).
Its operation is optimized according to the SAS© version: for SAS© 8 and later, it uses the %SCAN macro function with the -1 argument to directly retrieve the last word. For earlier versions, it implements an iterative loop that uses %SCAN to parse words one by one until the last one is identified. This macro is frequently used within %LET statements to assign the extracted word to a macro variable.
Data Analysis

Type : CREATION_INTERNE


The data processed by this macro (the word list and the separator) is provided directly as parameters during its call, for example via internally defined macro variables or literals.

1 Code Block
MACRO
Explanation :
This block defines the `%lastword` macro which takes a list of words (`words`) and an optional separator (`sep`, a space by default) as input. It declares local variables `N` and `W`. The code checks the SAS version using `%sysevalf(&sysver >= 8)`. If SAS is version 8 or later, it uses `%scan(&words, -1, %quote(&sep))` to directly extract the last word, as `%scan` with a negative index allows access to elements from the end. For earlier versions, a `%do %while` loop is used to sequentially parse the words in the list until `%scan` returns an empty value, indicating the end of the list. The last word found is stored in the macro variable `W` and returned as the macro's result.
Copied!
1/*--------------------------------------------------------------*
2 * Name: lastword.sas *
3 * Title: Return the last word from a delimited list of words *
4 Doc: http://www.datavis.ca/sasmac/lastword.html
5 *--------------------------------------------------------------*
6 * Author: Michael Friendly <friendly @yorku.ca> *
7 * Created: 26 Jan 2006 10:51:43 *
8 * Revised: 26 Jan 2006 10:51:43 *
9 * Version: 1.0 *
10 *
11 *--------------------------------------------------------------*/
12 /*=
13=Description:
14
15 The LASTWORD macro returns the last word from a delimited list of words.
16 This is useful for some generic forms of DATA Step BY processing with
17 first. and last. BY processing.
18 
19==Method:
20 
21 In Version 8+, it uses the %scan function in the form %scan(&words,-1,&sep)
22 The original version of this macro is by Richard A. DeVenezia.
23 
24=Usage:
25 
26 The LASTWORD macro is defined with positional parameters. The first
27 parameter is required for any useful result.
28 The macro is often used in a %LET statement, in the form
29
30 %let lastclass = %lastword(&class);
31
32==Parameters:
33 
34* WORDS A list of words separated by something
35 
36* SEP The word separator. [Default: SEP=%STR( )]
37
38==Example:
39 
40 %let string= Able Baker Charlie;
41 %put Last word of "&string" is "%lastword(&string)";
42 
43 %let classvar = Treatment Poison;
44 data test;
45 set animals;
46 by &classvar;
47 if first.%lastword(&classvar) then do;
48 ...
49 end;
50 
51 =*/
52 
53 
54%macro lastword(
55 words,
56 sep=%str( )
57 );
58 
59%* Richard A. DeVenezia - 940729; *%
60%*
61%* extract last word from list of words separated by a specified character; *%
62%*
63%* words - original list of words, separated with something; *%
64%*;
65 
66%local N W;
67%IF %sysevalf(&sysver >= 8) %THEN %DO;
68 %let W = %scan(&words, -1, %quote(&sep));
69 %END;
70 
71%ELSE %DO;
72 %let N=1;
73 %let W=%scan(&words,&N,%quote(&sep));
74 
75 %let N=2;
76 %DO %while (%scan(&words,&N,%quote(&sep))^=);
77 %let W=%scan(&words,&N,%quote(&sep));
78 %let N=%eval(&N+1);
79 %END;
80%END;
81 
82 &W
83 
84%mend;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Nom: lastword.sas, Auteur: Michael Friendly <friendly @yorku.ca>, Créé/Révisé: 26 Jan 2006. Version originale: Richard A. DeVenezia. Documentation: http://www.datavis.ca/sasmac/lastword.html