Transform Data with SPSS Macros saves an amazing amount of time!

transform data

Transform data

In order to correctly perform most multivariate techniques, your data needs to be normally distributed.

Having to transform (log, square root, etc.)  used to be a daily thing for me. It would take some doing to create new variables for the dozens or so that I’d need to transform.  Using macros I’ve written in SPSS makes it a breeze!  I’ve built SPSS macros that will convert a list of variables (and add a pre-fix to their names) for Logs, Natural logs, Square roots, etc.  I also built one that performs all of them (just to get a bit crazy)

 

Here are the SPSS macros that I wrote

 
*//////transform data//////////.
DEFINE !clog10  ( Vars=!CMDEND)
Set Error off.
!DO !I !IN (!Vars).
Compute !Concat(!I,"_l") =lg10(!I+1).
Var Label  !Concat(!I,"_l") !Concat(!i," (l)").
Format !Concat(!I,"_l") (F4.1).
Var width !Concat(!I,"_l") (5).
!DOEND. 
Set Error on.
exe.
!ENDDEFINE.
/*!clog10  Vars=arm atm.

DEFINE !clog10D  ( Vars=!CMDEND)
Set Error off.
!DO !I !IN (!Vars).
Compute !Concat(!I,"_l") =lg10(!I+1).
Var Label  !Concat(!I,"_l") !Concat(!i," (l)").
Format !Concat(!I,"_l") (F4.1).
Var width !Concat(!I,"_l") (5).
!DOEND. 
Match files file=* / DROP !Vars  . 
Set Error on.
exe.
!ENDDEFINE.
/*!clog10D  Vars=arm atm.

DEFINE !Ulog10  ( Vars=!CMDEND)
!DO !I !IN (!Vars).
Compute !Concat(!I,"_ul10") =10**(!I)-1.
Var Label  !Concat(!I,"_ul10") !Concat(!i," (ul10)").
Format !Concat(!I,"_ul10") (F8.0).
Var width !Concat(!I,"_ul10") (9).
!DOEND. 
exe.
!ENDDEFINE.

DEFINE !cLogN  ( Vars=!CMDEND)
!DO !I !IN (!Vars).
Compute !Concat(!I,"_ln") =ln(!I+1).
Var Label  !Concat(!I,"_ln") !Concat(!i," (ln)").
Format !Concat(!I,"_ln") (F4.1).
Var width !Concat(!I,"_ln") (5).
!DOEND. 
exe.
!ENDDEFINE.

Define !ULogN  ( Vars=!CMDEND)
!DO !I !IN (!vars).
Compute !Concat(!I,"_uln") =EXP(!I)-1.
Var label  !Concat(!I,"_uln") !Concat(!i," (uln)").
Format !Concat(!I,"_uln") (F4.1).
Var width !Concat(!I,"_uln") (5).
!DOEND. 
exe.
!ENDDEFINE.

DEFINE !csqrt  (Vars=!CMDEND)
!DO !I !IN (!vars).
if !I=0  !Concat(!I,"_s") =0.
If !I<>0 !Concat(!I,"_s") =sqrt(!I).
Var label  !Concat(!I,"_s") !Concat(!i," (s)").
Format !Concat(!I,"_s") (F4.1).
Var width !Concat(!I,"_s") (5).
!DOEND. 
exe.
!ENDDEFINE.
*////////////////.
/*!cSqrt  Vars=arm atm.

*////////////////.
DEFINE !Usqrt  (Vars=!CMDEND)
!DO !I !IN (!Vars).
If !I=0  !Concat(!I,"_Us") =0.
If !I <>0 !Concat(!I,"_Us") =(!I*!I).
Var Label  !Concat(!I,"_Us") !Concat(!i," (s)").
Format !Concat(!I,"_Us") (F4.1).
Var width !Concat(!I,"_Us") (5).
!DOEND. 
exe.
!ENDDEFINE.
*////////////////.
/*!Usqrt  Vars=arm atm.


*////perform all transform data ////////////.
DEFINE !cALL  ( Vars=!CMDEND)
!DO !I !IN (!Vars).
Compute !Concat(!I,"_l") =lg10(!I+1).
Compute !Concat(!I,"_ln") =ln(!I+1).
Compute !Concat(!I,"_s") =sqrt(!I+1).
Var Label  !Concat(!I,"_l") !Concat(!i," (l)") / !Concat(!I,"_ln") !Concat(!i," (ln)") / !Concat(!I,"_s") !Concat(!i," (s)").
Format !Concat(!I,"_ln") !Concat(!I,"_l") !Concat(!I,"_s") (F4.1).
Var width !Concat(!I,"_ln") !Concat(!I,"_l") !Concat(!I,"_s")(5).
If !Concat(!I,"_s")=1 !Concat(!I,"_s")=0.
!DOEND. 
exe.
!ENDDEFINE.
*////////////////.
/*!cALL  Vars=arm atm.

Trust me you’ll never want to transform data without them again!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.