NOTE: This page was created using SPSS version 17.0.2.
Group-mean centered and grand-mean centered variables are often used in multilevel models. Creating a single centered variable is simple enough to do, but creating several group-mean centered or grand-mean centered variables at once takes a little bit of programming. In the first example below, we will create an SPSS macro called group_cvars to create a series of group-mean centered variables. In the second example, we will create a macro called grand_cvars to create a series of grand-mean centered variables. We need to create macros for these tasks because we will be creating multiple new variables. We will use do-loops within the macros to assign names and values to the new variables.
For these examples, we will use the hsb2 dataset. Specifically, we will use the continuous variables read, write and math for both examples. We will also use the categorical variable ses for the second example. The variable ses has three levels. We will first show the syntax to be used, and then explain the syntax.
Creating grand-mean centered variables
In the example below, we will create a macro called grand_cvars and use it to create grand-mean centered variables for the variables read, write and math in our data set. When we are finished, we will have three new variables added to our data set, read_c, write_c and math_c. To create a grand-mean centered variable, you simply take the mean of the variable and subtract that mean from each value of the variable. To create a series of grand-mean centered variables, we will need to include two pieces of information: the list of variables to be grand-mean centered and suffix to add to the end of the name of those variables, which is how we will name our new variables.
define grand_cvars( vlist = !charend('/') /suffix = !cmdend ) compute one_temp = 1. exe. !do !vname !in (!vlist) !let !nname = !concat(!vname, !suffix)aggregate /outfile=* mode=addvariables overwrite = yes /break =one_temp /y_temp=mean(!vname). compute !nname = !vname - y_temp. exe. !doend delete variables y_temp one_temp. !enddefine. grand_cvars vlist = read write math /suffix = _c.
Let’s look at the different parts of the syntax above.
define grand_cvars( vlist = !charend('/') /suffix = !cmdend )
This first part of the syntax is used to create and name the macro, and to pass the arguments to the macro. The command used to create a macro is define. After issuing this command, the name of the macro is provided. In our example, this is grand_cvars. Next, we need to define the order of the input of the arguments. To create grand-mean centered variables, we need to know the list of the variables that we want to center and the suffix to the name of the new variables. (In this macro, we will use the variable names of the variables to be grand-mean centered plus a suffix.) Because we have two pieces of information that we need to pass to the macro, we will need to create two arguments; we will call these arguments vlist and suffix. We will have the user separate the arguments with a slash (/); this makes clear where each argument ends. We use the !charend keyword to tell SPSS that the vlist argument ends when the slash is encountered. The !cmdend keyword indicates that the suffix argument ends when the end of command marker (i.e., the period at the end of the macro call) is encountered.
compute one_temp = 1. exe.
The two lines of syntax above are needed only if you are using SPSS version 16 or earlier. The compute command is used to create a variable called one_temp that is equal to 1 (in other words, it is a constant), and the execute command (shortened to exe.) is used to create the variable immediately. The variable one_temp is used on the break subcommand in the aggregate command. However, as of SPSS version 17, the break subcommand is no longer a necessary subcommand with aggregate, so this step is unnecessary. Of course, it can be included if you are using SPSS version 17 or later with no problem.
Now that we have created, named and assigned arguments to the macro, we can begin the process of creating the grand-mean centered variables.
!do !vname !in (!vlist) !let !nname = !concat(!vname, !suffix)
We will begin by creating a loop to move through the list of variables to be grand-mean centered. We use the do command for this. (The do-loop ends later with the doend command.) Macro commands and macro variables must begin with a !. Both !do and !let are macro commands. In our do-loop, we will call the indexing variable !vname, and it loops through the variables in !vlist. On the !let command, we create the macro variable !nname that will be the concatenation of !vname and !suffix. Notice that there are no periods at the ends of these commands.
aggregate /outfile=* mode=addvariables overwrite = yes /break =one_temp /y_temp=mean(!vname).
Our next task is to create the grand-mean centered variables, and we will use the aggregate command to do this. We need to use some subcommands with the aggregate command. The first is the outfile subcommand, and the star (*) is used to indicate that we do not want to create a new data set. The mode=addvariables option is used to indicate that we want the new variables to be added to the current data set. (If we used the outfile = * option without the mode=addvariables option, SPSS would replace the current data file with a file containing only the newly created variables; by including both options, we will have the newly created variables added to the current data set.) We use the overwrite=yes option so that the next time the macro loops through, the variable called y_temp is overwritten with the grand mean for the next variable. On the break subcommand, we indicate the group variable, one_temp. Because the variable one_temp is a constant, the data are not really broken into groups; this is done because this subcommand is necessary if you are using SPSS version 16 or earlier. As mentioned above, if you are using SPSS version 17 or later, you do not need this subcommand. Finally, we create the new variable (called y_temp), which contain the grand mean, using the mean function.
compute !nname = !vname - y_temp. exe. !doend delete variables y_temp one_temp. !enddefine.
To create the grand-mean centered variable, we use the compute command. The macro variable !nname is the difference between the macro variable !vname and y_temp (which was created using the aggregate command). We include the execute command (shortened to exe.) to have SPSS create !nname immediately, and then we end the do-loop with !doend. Please note that there is no period at the end of this command. We use the deleted variables command to remove the variables y_temp and one_temp from our current data set, and then we end the macro with the !enddefine command.
grand_cvars vlist = read write math /suffix = _c.
Now we are ready to use our new macro. We start by giving the macro name, grand_cvars. Next, we supply the list of variables to be grand-mean centered and the suffix to include at the end of the variable names.
Creating group-mean centered variables
To create group-mean centered variables, we will again need to provide the list of variables to be group-mean centered and the suffix. In addition, we will also need to indicate the grouping variable.
define group_cvars( group = !charend('/') /vlist = !charend('/') /suffix = !cmdend ) !do !vname !in (!vlist) !let !nname = !concat(!vname, !suffix)aggregate /outfile=* mode=addvariables overwrite = yes /break =!group /y_temp=mean(!vname). compute !nname = !vname - y_temp. exe. !doend delete variables y_temp. !enddefine.group_cvars group = ses /vlist = read write math /suffix = _c.
Let’s consider the first part of the syntax above.
define group_cvars( group = !charend('/') /vlist = !charend('/') /suffix = !cmdend )
This first part of the syntax is used to create and name the macro, and to pass the arguments to the macro. The command used to create a macro is define. After issuing this command, the name of the macro is provided. In our example, this is group_vars. Next, we need to define the order of the input of the arguments. To create group-mean centered variables, we need to know the grouping variable, the list of the variables that we want to center and the suffix to the name of the new variables. (In this macro, we will use the variable names of the variables to be group-mean centered plus a suffix.) Because we have three pieces of information that we need to pass to the macro, we will need to create three arguments; we will call these arguments group, vlist and suffix. We will have the user separate the arguments with a slash (/); this makes clear where each argument ends. We use the !charend keyword to tell SPSS that the group argument ends when the slash is encountered. The vlist argument ends when the next slash is encountered. The !cmdend keyword indicates that the suffix argument ends when the end of command marker (i.e., the period at the end of the macro call) is encountered.
Now that we have created, named and assigned arguments to the macro, we can begin the process of creating the group-mean centered variables.
!do !vname !in (!vlist) !let !nname = !concat(!vname, !suffix)
We will begin by creating a loop to move through the list of variables to be group-mean centered. We use the do command for this. (The do-loop ends later with the doend command.) Macro commands and macro variables must begin with a !. Both !do and !let are macro commands. In our do-loop, we will call the indexing variable !vname, and it loops through the variables in !vlist. On the !let command, we create the macro variable !nname that will be the concatenation of !vname and !suffix. Notice that there are no periods at the ends of these commands.
aggregate /outfile=* mode=addvariables overwrite = yes /break =!group /y_temp=mean(!vname).
Our next task is to create the group-mean centered variables, and we will use the aggregate command to do this. We need to use some subcommands with the aggregate command. The first is the outfile subcommand, and the star (*) is used to indicate that we do not want to create a new data set. The mode=addvariables option is used to indicate that we want the new variables to be added to the current data set. (If we used the outfile = * option without the mode=addvariables option, SPSS would replace the current data file with a file containing only the newly created variables; by including both options, we will have the newly created variables added to the current data set.) We use the overwrite=yes option so that the next time the macro loops through, the variable called y_temp is overwritten with the group means for the next variable. On the break subcommand, we indicate the group variable by its macro name !group. Finally, we create the new variable (called y_temp), which contain the group means, using the mean function.
compute !nname = !vname - y_temp. exe. !doend delete variables y_temp. !enddefine.
To create the group-mean centered variable, we use the compute command. The macro variable !nname is the difference between the macro variable !vname and y_temp (which was created using the aggregate command). We include the execute command (shortened to exe.) to have SPSS create !nname immediately, and then we end the do-loop with !doend. Please note that there is no period at the end of this command. We use the deleted variables command to remove the variable y_temp from our current data set, and then we end the macro with the !enddefine command.
group_cvars group = ses /vlist = read write math /suffix = _c.
Now we are ready to use our new macro. We start by giving the macro name, group_cvars. Next, we supply the three necessary arguments: we name the group variable, the list of variables to be group-mean centered, and the suffix to include at the end of the variable names.