*CENTERING TIME-VARYING VARIABLES;
data democ;
set "C:\...\democ"; *replace ... with your path name;
run;
*examine original time-varying negative affect variable;
proc corr data=democ;
var negaffect;
run;
proc freq data=democ;
tables negaffect;
run;
*grand-mean centering;
**automatic version using PROC STANDARD;
data democ; set democ;
negaffectcg = negaffect; *copy the variable in a datastep so you don't overwrite it in the next step;
*cg stands for "centered grand";
run;
proc standard data=democ out=democ mean=0; *mean=0 does the centering;
var negaffectcg;
run;
proc corr data=democ;
var negaffect negaffectcg;
run;
proc freq data=democ;
tables negaffect negaffectcg;
run;
*Person-Mean Centering;
**First, calculate the mean of NA for each person;
***If you use the grand-mean centered variable to start, the person-means will have an overall mean of 0, which is
useful in model building;
proc means data=democ nway noprint;
class id;
var negaffectcg;
output out=means1 /*this makes a new dataset with only 1 row per person that contains the mean of NA for that person*/
mean=negaffectcb; *cb stands for "centered between";
run;
*sort and merge these datasets together;
proc sort data=democ; by id date hour minute; run;
proc sort data=means1; by id; run;
data democ;
merge democ means1;
by id;
negaffectcw = negaffectcg - negaffectcb; *center on the person-means, cw stands for "centered within";
run;
*negaffectcw is the person-mean centered negative affect variable, it captures within-person variance (how much people differ from themselves);
*negaffectcb is the mean of NA for each person, this captures between-person variance (how much people differ from each other);
*the correlation between these two should be 0 because these levels of variance are independent;
proc corr data=democ;
var negaffectcb negaffectcw;
run;