Type : INTERNE_ERSTELLUNG
Die von diesem Makro verarbeiteten Daten (die Wortliste und das Trennzeichen) werden direkt als Parameter bei seinem Aufruf bereitgestellt, z.B. über intern definierte Makrovariablen oder Literale.
| 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; |