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

SPSS Macros for replacing missing data

SPSS macro

SPSS Macros for replacing missing data

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 statistics correctly.

I wrote a few SPSS Macros that make it very easy to replace the missing values on your data.  Here are the two macros

 
*///////////////.
DEFINE !Rep_Miss  (Beg !TOKENS (1) /Prez !TOKENS (1)  /End !TOKENS (1))
Do if !PREZ>0.
do repeat v=!BEG to !END.
if missing (v) v=0.
end repeat.
end if.
exe.
!ENDDEFINE.
*///////////////.

/*!Rep_Miss  Prez=presentvariable  Beg=v11  End=v19.

*///////////////.
DEFINE !Rep_Miss2  (Beg !TOKENS (1)   /End !TOKENS (1))
do repeat v=!BEG to !END.
if missing (v) v=0.
end repeat.
exe.
!ENDDEFINE.
*///////////////.
/*!Rep_Miss2   Beg=v11  End=v19.

 

SPSS macro makes for easy splitting of data into separate files of equal numbers

SPSS Macro

SPSS macro

If you frequently need to have your data split into equal groups, this SPSS macro is the way to go!  You can just tell it how many groups you want and the variable you want to create to store group belonging and WHAM!  it randomizes the list and adds a new field showing the groups.

In the code below I show two examples of the SPSS macro.  One that will randomize the group and split it out, the second allows you to use another variable before breaking them into groups (In case you want a certain order to them.)  Both macros greatly reduce the amount of work needed to do in SPSS.

 

 
*/////////////////////.
DEFINE !Rand_Group (Groups !TOKENS (1) / NewVar !TOKENS (1))
Match files file=* / DROP !NewVar.
Compute Rand=RV.UNIFORM(0,1).
SORT CASES BY Rand (A).
compute row=$casenum.
RANK VARIABLES=row (A) /NTILES (!Groups) into !NewVar /PRINT=YES /TIES=HIGH.
Match files file=* /drop row Rand.
VARIABLE LEVEL !NewVar (Nominal).
Var label !NewVar "Random Group".
Val labels !NewVar 1'One' 2'Two' 3'Three' 4'Four' 5'Five' 6'Six' 7'Seven' 8'Eight' 9'Nine' 10'Ten'.
Freq !NewVar.
!ENDDEFINE.
*/////////////////////.
!Rand_Group Groups=10 NewVar=Rand_GP.

/* If you want to first sort them on a variable the below macro will handle that for you.

*/////SPSS macro 2////////////////.
DEFINE !Group (Sorter !TOKENS (1) / Groups !TOKENS (1) / NewVar !TOKENS (1))
Match files file=* / DROP !NewVar.
SORT CASES BY !Sorter (A).
compute row=$casenum.
RANK VARIABLES=row (A) /NTILES (!Groups) into !NewVar /PRINT=YES /TIES=HIGH .
Match files file=* /drop row.
VARIABLE LEVEL !NewVar ( ORDINAL ).
Var label !NewVar "Group".
Val labels !NewVar 1'One' 2'Two' 3'Three' 4'Four' 5'Five' 6'Six' 7'Seven' 8'Eight' 9'Nine' 10'Ten'.
Freq !NewVar.
!ENDDEFINE.
*/////////////////////.
!Group Sorter=numords Groups=10 NewVar=Dec_Frequency.

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 …

SPSS macro – Repurposing the file path for multiple uses

SPSS macro
I write a TON of SPSS code and work with colleagues around the world as well as on different computers which have different drives – paths available. This SPSS macro shows how you can get around a lot of the headaches dealing with these issues.  There are some other SPSS commands that can be used similarly however I find this exceptionally helpful as I can reference it when I’m merging files, exporting to text / Excel, saving output, etc.

SPSS macro

 
/***********************Save file macro********************************.
Define !Sav (File=!TOKENS(1)) Save OutFile=!path+!file+'.sav' /Compressed. !enddefine.

/***********************Get file and show N********************************.
Define !Get (File=!TOKENS(1))
Get File=!path+!file+'.sav'. 
DATASET NAME $DataSet WINDOW=FRONT.
exe.
show n.
!enddefine.

 

Syntax for changing width of SPSS string variable

SPSS string variableUse Syntax to change the with of SPSS string variable

String variables in SPSS are still a pain to deal with and merging data files together with a key variable that is a string is NOT fun in SPSS.  The string variable needs to be the same width in each data file and needs to be sorted the same (ascending-descending).  I can’t tell you the number of times I’d curse the old format!

My below macro makes it a breeze to change the width of your SPSS string variables.  The process is pretty straight forward.

Years ago I established some defaults for variables that were frequently in my data sets like: email, First name, Last name, company name, Job title, Job Role, etc.  Surprisingly setting the width to wider than they are doesn’t actually take up any extra file size so, as a rule of thumb, I recommend doubling what you typically see (otherwise you’ll end up with truncated data)

Here is the macro I use which automates the process of changing the SPSS string text width.  🙂

 
*///resize variable////////////.
DEFINE !CNGWDTH (NAME !TOKENS (1) /WIDTH !TOKENS (1))
STRING NEWVAR (!WIDTH).
compute newvar=!name.
EXE.
DELETE VARIABLES !NAME.
RENAME VARIABLES NEWVAR=!NAME.
!ENDDEFINE.
*///////////////.

!CNGWDTH Name=email   Width=A65.

BTW- if you were reading this with the goal of changing the column width of variables in the data editor (think of Excel and how you resize the columns) the above macro has nothing to do with it.  Below is the code you’d use to change email to 30 wide; Country to 20 wide; and the rest to 6 wide.

 VARIABLE WIDTH ALL (6)  email (30) country (20).