Using Macro Names that Substitute a Variable List in SPSS Procedures and Other Commands David Keane, PERD Research Associate Assume.. 1. a set of commands prepared in and run from the SPSS Syntax Editor. 2. a number of unique macro names that substitute a variable list when executed. Here are two macro definitions: * Example 1 . DEFINE mylist1 () var1 var3 var5 var7. !ENDDEFINE. DEFINE mylist2 () var2 var4 var6 var8. !ENDDEFINE. 3. that the first line in any example below starts in column 1. A. Here's a simple DO REPEAT block involving two or more stand-in variables that reference a variable list. Each list consists of variable names, not macro names. * Example 2 . DO REPEAT A = var1 var3 var5 var7 / B = var2 var4 var6 var8 . . {some commands that refer to stand-in vars A and B}. END REPEAT. The above will execute without generating an error state. B. Now assume a similar, multi-list DO REPEAT block that uses the two macros defined above. * Example 3 - generates a error . DO REPEAT A = mylist1 / B = mylist2 . . {some commands that refer to stand-in vars A and B}. END REPEAT. When, in example 3, the third line is parsed and the first macro name is encountered, SPSS seems to have trouble dealing with the fact that the DO REPEAT command line continues past its initial line. An error is generated; the variable, "var2", which is the first variable substituted by the macro "mylist2", is reported as an unknown command starting in column 1. It almost seems as if, when executing the macro name (substituting the var list), SPSS insists on starting the substitution text in column 1 of the line, rather than after " / B = ". Of course, any text beginning in column 1 is interpreted as an SPSS command, one not associated with the previous input line. Indeed, the previous command is flagged as having terminated, even though the previous line does not end with a command terminator, usually the period ("."). (Terminators are required in batch input but not in the Syntax Editor input provided the next command starts in column 1. Conversely, in the Editor, commands may start in any column provided the previous command line is explicitly terminated.) An example of what seems to be occurring, based on example 3 is: * Example 4 . DO REPEAT A = var1 var3 var5 var7 . var2 var4 var6 var8 . . {some commands that refer to stand-in vars A and B}. END REPEAT. The solution is to place all macro names on the same physical line, usually but not necessarily the initial line. See example 5. Of course a large number of macro names will result in a long command line. A long input line is word wrapped in the output file. * Example 5 - best practice . DO REPEAT A = mylist1 / B = mylist2 . . {some commands that refer to stand-in vars A and B}. END REPEAT. C. Much the same sort of problem can occur with the RECODE command. Best practice seems to be to place the entire set of macro names on one line, usually the initial line, as in example 6. In some instances SPSS will accept having the action specifications on the second (continuation) line, starting in column 2 or beyond. In other instances SPSS will insist that the action specifications be on the same physical line as the macro names. * Example 6 - best practice . RECODE mylist1 mylist2 (0,sysmis=101). RECODE mylist1 mylist2 (0,sysmis=101). D. The same problem arises with non-procedural commands. Example 7 shows a MISSING VALUES command that includes, on lines one and two, a list of 12 macro names for variable-list substitutions and two variable names (var8 var9). This layout results in an error. * Example 7 - generates a error . MISSING VALUES mylist1 w1of2 w2of2 w1of3 w2of3 w3of3 mylist2 w1of5 w2of5 w3of5 w4of5 w5of5 var8 var9 (101,102,103) . The error messages are: >Error # 4810 in column 256. Text: (End of Command) >The parenthesized list of missing values was omitted. >This command [Missing Values] not executed. mylist2 w1of5 w2of5 w3of5 w4of5 w5of5 var8 var9 (101,102,103) >Error # 1. Command name: var2 >The first word in the line is not recognized as an SPSS command. >This command not executed. Note that "var2" is the first variable substituted by macro "mylist2". If both the set of macro names and the specification (the values enclosed in parentheses) are provided on the same physical line, usually the command's initial line, the MISSING VALUE executes successfully. If, however, the var list continues to a second line holding either additional variables or macro names and the specification, or the specification alone, then an error is generated. The first variable on the second line, as in example 8, is flagged as an unknown command. * Example 8 - generates a error . MISSING VALUES mylist1 w1of2 w2of2 w1of3 w2of3 w3of3 var8 var9 (101,102,103) . Here "var8" and "var9" are variables, not macro names. The error messages are: MISSING VALUES mylist1 w1of2 w2of2 w1of3 w2of3 w3of3 >Error # 4810 in column 256. Text: (End of Command) >The parenthesized list of missing values was omitted. >This command not executed. var8 var9 (101,102,103) . >Error # 1. Command name: var8 >The first word in the line is not recognized as an SPSS command. >This command not executed. Again, because Syntax Editor allows new commands to begin past column 1 and a previous command to end without a explicit terminator, the second line causes problems for the spss compiler when the variable- list portion of the command spans two or more lines and the first line ends with a macro name. The best practice, as before, seems to be place the entire variable- list specification (macro names and, optionally variable names) on a single line, usually the initial line. Do so even where the command contains only a single macro that subtitutes variable names. * Example 9 - best practice . MISSING VALUES mylist1 ... var9 (101,102,103) . (The same rule likely applies to commands in which a macro substitutes text other than variable names but I haven't tested for this yet.) E. Finally, three examples using VALUE LABELS, another command for which macro names can be extremely useful in substituting for variable lists. Both examples 10 and 11 resulted in error messages reporting an unknown command, "var2" in example 10 and "0" in example 11, at the beginning of line 3. * Example 10 - generates a error . VALUE LABELS mylist1 w1of2 w2of2 w1of3 w2of3 w3of3 mylist2 w1of4 w2of4 w3of4 w4of4 w1of5 0 'zero' 1 'one' 101 'no response' . * Example 11 - generates a error . VALUE LABELS mylist1 w1of2 w2of2 w1of3 w2of3 w3of3 w1of4 w2of4 w3of4 w4of4 0 'zero' 1 'one' 101 'no response' . Example 12, in which the first value label is on the same line as all the macro names, executes successfully. * Example 12 - best practice . VALUE LABELS w1of2 w2of2 w1of3 w2of3 w3of3 w1of4 w2of4 w3of4 w4of4 0 'zero' 1 'one' 101 'no response' . This illustrates another complexity. For some but not all commands, the line holding the variables list must not end with that variables list. Rather, the variables list must be followed by at least one additional specification -- the first label definition in the example 12, the parenthesized missing values in example 9. ------------------------------------------------------------------------------- eof / rev. 2003-05-29 drk e:\spss tips\Using macros for var lists in procedures.txt