How to Prepare Data for Mplus

Jeremy Albright

Posted on


Mplus requires data to be read in from a text file without variable names, with numeric values only, and with missing data coded as a single numeric value, such as -999. A common workflow for preparing data to analyze in Mplus is to perform the variable cleaning in SPSS and then save the data as a text file. This guide shows the appropriate steps using the sample file bollen_sem.sav data that can be downloaded from this GitHub repository.

First, open the data file in SPSS. You should see the following:

Data in SPSS

These data do not include any missing values, but if they did we could easily convert all variables to have missing coded as -999. To do so we would go to Transform \(\rightarrow\) Recode into Same Variables, and we would see the following dialog box:

SPSS recode into same variables

Bring all of the variables over to the Numeric Variables box:

SPSS recode into same variables

In the new dialog box, select System-missing under Old Value. Enter -999 in the Value field under New Value. Click Add so that you see the following:

SPSS recode into same variables

Click Continue, then OK. The data are now ready to be saved.

Go to File \(\rightarrow\) Save As to open the Save Data As dialog box. By default, SPSS will save the file in native SPSS format with a .sav extension. Click the drop-down box to the right of the Save as type field and choose Tab delimited (*dat). Next, uncheck the box next to Write variable names to file. Finally, enter the name for the new data file, such as sem-bollen. You should see the following:

SPSS export data

Click Save.

There is one odd hiccup that occurs when you write the data from SPSS into a text file for Mplus. Without one final little adjustment, Mplus will not correctly read the data. To see this, open Mplus.

SPSS open Mplus

From inside Mplus, open the data file. This can be done by going to File \(\rightarrow\) Open… and navigating to the folder where you saved the data. By default, Mplus will only look for files with a .inp or .out extension. In the lower right, next to the File name field, change to All Files (.). You will now see the data file with the .dat exension.

Open data in Mplus from SPSS

You can now open the file. When you do so, you will see an odd set of characters at the very start of the file (highlighted here).

Mplus glitch

Delete these to avoid odd results.

Fixed Mplus data

Click File \(\rightarrow\) Save. Now your data are ready.

One benefit of starting with SPSS is that you can go back into SPSS, choose the Variable View tab, and highlight the variable names to copy them and paste into an Mplus input file.

Save SPSS data

After highlighting the variable names, go to Edit \(\rightarrow\) Copy to save the variable names to the clipboard. Then return to Mplus and paste in the values. Mplus requires that you type out the names in the order in which the columns appear in the data file. This will place them in the correct order.

Paste variable names into Mplus

Now it’s just a matter of formatting and adding in the rest of the Mplus code. The following runs the model:

Run the model in Mplus

Still have questions? Contact us!