• Intro to AutoHotkey HotStrings with AutoHotkey Intermediate AutoHotkey GUIs are Easy with AutoHotkey Intro to DOS & AutoHotkey

Drastically reduce work needed for Factor Analysis with SPSS Macro

SPSS macroSPSS Macros drastically reduce the time needed to run factor analysis and creating summary variables.  In the video below I demonstrate how you can greatly increase the speed at which you analyze your data as well as creating the new variables with summary factor scores.

By running Factor analysis twice (but changing the output) you can make it much easier to create summary variables once you’re happy with your solution!

Video demonstrating SPSS Macro running Factor Analysis

Here is the SPSS macro for Factor analysis:

 
*///////////////.
DEFINE !Factor (Rotate !TOKENS (1)   / Title !Tokens (1)  / Sav  !Tokens (1)  /Varys !CMDEND)
Include "B:\SPSS\Syntax\Output_Off.SPS".
script "B:\SPSS\Scripts\Empty.sbs".

Include "B:\SPSS\Syntax\Show Names.SPS".
FACTOR  /VARIABLES !Varys  /PRINT ROTATION  /FORMAT SORT  /CRITERIA MINEIGEN(1) ITERATE(25)  /EXTRACTION PC 
 /CRITERIA ITERATE(50)   /METHOD=CORRELATION /ROTATION !Rotate  / !Unquote(!SAV).
Script "B:\Scripts\Parse\Clean viewer.sbs" ("Component Transformation Matrix").
Script "B:\Scripts\Parse\Clean viewer.sbs" ("Component Matrix").
script "B:\Scripts\S2\Wrap Rows.sbs".
*Script "B:\Scripts\Factor Flag.SBS".
Script "B:\Scripts\Flag above .45.SBS".
Script "B:\Scripts\Flag above .65.SBS".
Script  "B:\Scripts\Flag above .80.SBS".
Script "B:\Scripts\S2\Right Align Column.SBS".
Script "B:\Scripts\Parse\Clean viewer.sbs" ("Total Variance Explained").

*/ Second run of SPSS Macro *.
Include "B:\Syntax\Show Labels.SPS".
FACTOR  /VARIABLES !Varys  /PRINT ROTATION  /FORMAT SORT  /CRITERIA MINEIGEN(1) ITERATE(25)  /EXTRACTION PC 
 /CRITERIA ITERATE(50)   /METHOD=CORRELATION  /ROTATION !Rotate.
Script "B:\Scripts\Parse\Clean viewer.sbs" ("Component Transformation Matrix").
Script "B:\Scripts\Parse\Clean viewer.sbs" ("Component Matrix").
script "B:\Scripts\S2\Wrap Rows.sbs".
*Script "B:\Scripts\Factor Flag.SBS".
Script "B:\Scripts\Flag above .45.SBS".
Script "B:\Scripts\Flag above .65.SBS".
Script  "B:\Scripts\Flag above .80.SBS".
Script "B:\Scripts\S2\Right Align Column.SBS".
!ENDDEFINE.
*///////////////.
!Factor  Rotate=Varimax   SAV="SAVE REG(ALL)"      Varys=  to  .*/
/*EQUAMAX     QUARTIMAX  VARIMAX  */.

And here is the other SPSS macro which eases creating of a new factor.  It takes a little getting used to but is well worth the effort if you’re doing a lot of factor analysis.

*////////////////.
DEFINE !CreateFac  (NewFac !TOKENS (1) / Label !TOKENS (1)  / Vars=!CMDEND)
Compute !NewFac=Mean(!HEAD(!Vars) !DO !v !IN (!TAIL(!Vars)) , !v !DOEND ).
Format !NewFac (F5.2).
Var Label !NewFac !Label.
exe.
!ENDDEFINE.
*////////////////.
/*!CreateFac  NewFac=Joe Label="Joe Rocks" Vars=Enc_friend Proud Und_wrk_res Eth_Std Job_aligns MissPurp.

Learning Macros | Raynald’s SPSS Tools

This page is a simple introduction to macros. There are several fully (line- by-line) commented examples . Overview of several macro-related …

Programming with SPSS Syntax and Macros – Tarstud.hu

Instead, a SPSS macro could generate the SPSS Syntax for each interactive graph within a loop, substituting a new rating scale variable name per iteration.

SPSS macro to flip ordinal variable labels so they graph well in Excel

SPSS macroEven though I do nearly all my analysis in SPSS, I still prefer to use Excel to graph my data.  Unfortunately Excel inverts the legend when graphing. Creating a new variable, or re-coding the old one, is time consuming and errors can creep in. This SPSS macro to invert an ordinal variable with very little work.  Saves an amazing amount of time!

It also has a built-in way to filter out too small a sample size.

 

 */////////////////////.   
DEFINE !ISubgp  (Var !TOKENS (1) /Cut !TOKENS (1) )
AUTORECODE   VARIABLES=!Var  /INTO SubGroup   /DESCENDING   /BLANK=MISSING   /PRINT.
AGGREGATE /OUTFILE=* MODE=ADDVARIABLES OVERWRITEVARS=YES /BREAK=subgroup  /Sub_CT=N .
Select if Sub_CT >=!Cut.
if missing(Subgroup) Sub_CT=$Sysmis.
Freq SubGroup.
exe.
!ENDDEFINE.
*/////////////////////. 
!ISubgp Var=Var_toInvert   Cut=20 . 

SPSS macro

The below video demonstrates the usage of the SPSS macro.

 

Intro to SPSS Macro call; The lifesavers of redundant code

SPSS macro
Using SPSS Macros can save an incredible amount of time.  They have a somewhat-steep learning curve but are definitely worth it!  This is a short overview of the strength of using a macro to run the descriptives command.

Word of warning, make sure you re-define your macro before calling it.  This is an easy step to forget and you can waste a lot of time.

SPSS Macro Code

 
*///////////////.
DEFINE !Describer (Buy2 !TOKENS (1)   / Title !Tokens (1)  / ord !Tokens (1) /Varys !CMDEND)
Desc  !Varys     /sort (!ORD)  /stats Mean.
script "B:\SPSS\Scripts\S1\Swap Mean.SBS".
Script "B:\SPSS\Scripts\Change N to Count.SBS".
Script "B:\SPSS\Scripts\Parse\Move Row.sbs"("Other"). 
Script "B:\SPSS\Scripts\Parse\Move Row.sbs"("None of the above").
script 'B:\SPSS\Scripts\Parse\Change Title.sbs' (!Title).
Script "B:\SPSS\Scripts\S1\Size labels to avoid line-wrapping.sbs".
!ENDDEFINE.
*///////////////.
/*!Describer Buy2=XXX Ord=a  Title="Title"	Varys= Var1 Var2.

Video tutorials demonstrating the usage of the SPSS Macro

Additional SPSS macro links

A great resource to use and learn macros is Raynald Levesque’s site.  This page is his intro to macros page and this one walks through writing one.  He also has a great library of them located here.

Learning Macros | Raynald’s SPSS Tools

This page is a simple introduction to SPSS macros. There are several fully (line- by-line) commented examples of macro. Overview of several macro-related …

Quick Intro to SPSS progams | Glenn Thompson’s Homepage

Feb 13, 2010 Free Power Calculation Resources · automation program for  · Quick Intro to SPSS Working with SPSS …

Replacing missing data

https://www.youtube.com/watch?v=5CBm_F3zBOo A lot of online vendor tools provide a data value on a multi-select question however they leave the value missing if the respondent didn’t select it. Most of the analysis I want to do needs to have a zero (or some other value) present in order to calculate the …

Use an SPSS Macro to Open and Save Hundreds /Thousands of similar Files

SPSS macro to open and save

Using an SPSS Macro to Open and Save files can be quite a time saver!  In my example code & video I demonstrate how to adapt basic SPSS syntax into a macro that automates the process.   I also mention this macro which takes the reverse approach (automatically breaks the file into random groups).

The below code it just an example using SPSS macro to open and save text files which will have to be adapted to your needs.  Be sure to pay close attention to string variables and the widths you set them at.  Generally speaking, set them at double what SPSS assigns them.  In reality, there doesn’t seem to be any file-size change when you make them very wide so you also might just default to setting the width to 10X the width (then shrink the column widths with the Var Width command.

In the video I also demonstrate how I used Excel to help write my syntax for looping over all the files as well as merging the files together.

SPSS macro to open and save Text files

 
*///////////////.
DEFINE !OpenSav (In_Path !TOKENS (1) /Out_Path !TOKENS (1) / File_Name !TOKENS (1) )
 script "B:\SPSS\Scripts\Empty.sbs".
 
GET DATA  /TYPE=TXT  /FILE=!In_Path + !File_Name + ".txt"
 /DELCASE=LINE  /DELIMITERS="\t"  /QUALIFIER='"'  /ARRANGEMENT=DELIMITED  /FIRSTCASE=2  /IMPORTCASE=ALL  /VARIABLES=
 Company A120
 City A30
 State F3.0
 Sales_Rev F11.0
 Num_Emp F6.0.
DATASET NAME $DataSet WINDOW=FRONT.
Var width All (5) company city (10) .
SAVE OUTFILE=!Out_Path + !File_Name + ".sav"  /COMPRESSED.
!ENDDEFINE.
*///////////////.
!OpenSav In_Path="H:\temp\Files\"  Out_Path="H:\temp\Out\"    File_Name="Rand_1".

Video demonstrating using an SPSS macro to open and save files

  • Intro to AutoHotkey HotStrings with AutoHotkey Intermediate AutoHotkey GUIs are Easy with AutoHotkey Intro to DOS & AutoHotkey