SPSS Macro String Functions
SPSS defines a number of useful string manipulation functions for use in macros. They are all prefixed with ! by convention.
Quoting Parameters
SPSS macros are fundamentally a form of string manipulation, and therefore everything included in a macro definition is a string. As such, quoting is technically optional.
It is highly recommended that strings always be quoted. You are otherwise relying on the tokenizer to correctly interpret the beginning and end of a filename or value.
Consider the following:
Expression |
Result |
!UPCASE(abc) |
ABC |
!UPCASE('abc') |
ABC |
!UPCASE(a b c) |
error |
!UPCASE('a b c') |
A B C |
!UPCASE(a/b/c) |
error |
!UPCASE('a/b/c') |
A/B/C |
!UPCASE(!CONCAT(a,b,c)) |
ABC |
!UPCASE(!CONCAT('a','b','c')) |
ABC |
!UPCASE(!CONCAT(a, b, c)) |
ABC |
!UPCASE(!CONCAT('a ','b ','c ')) |
A B C |
!UPCASE(!CONCAT('a,b,c')) |
A,B,C |
!QUOTE(abc) |
'ABC' |
!QUOTE('abc') |
abc |
!QUOTE('Bill"s') |
'Bill"s' |
!QUOTE("Bill's") |
"Bill's" |
!QUOTE(Bill's) |
error |
!QUOTE(!UNQUOTE('Bill"s')) |
'Bill"s' |
Functions
!blanks(N) returns a string composed of N blank spaces.
!concat(value...) concatenate all values.
!eval(value) scans a value for macros and evaluates. This is necessary for evaluating an externally defined macro within a macro definition.
!head(value...) copies the first token from the values.
!index(value, pattern) returns the integer position of a pattern inside a value. If the value does not contain the pattern, this function returns 0.
!length(value) returns the integer length of a value.
!null returns an empty string. This is mostly useful for checking if a macro parameter was specified (i.e. !IF (!1 !NE !NULL)).
!quote(value) returns the value quoted, with internal quotes escaped.
!substr(value, index [, length]) copies a substring from a value. The substring starts at position index and continues for length characters. If length is not specified, the substring copies until the end of the original value.
!tail(value...) copies the second through last tokens from the values. The complement to !head().
!unquote(value) return the value un-quoted, with internal escaped quotes un-escaped.
!upcase(value) translates lowercase characters into uppercase.