UCLA Academic Technology Services HomeServicesClassesContactJobs
Help the Stat Consulting Group by giving a gift             
Loading

Mplus Class Notes
Entering Data


Mplus version 5.2 was used for these examples.

1.0 Entering free format data

Data files for Mplus are just plain ASCII text files. "Free format", where the values for each of the variables are separated by a delimiter like a blank or a comma, is one way in which data can be found in a text file. The example below contains the first 20 lines from a file called hsb.dat. Here you can see the variables are separated by commas, and the variable names are not on the first line.  The variables in the file are id, female, race, ses, schtyp, prog, read, write, math, science and socst.

 70,0,4,1,1,1,57,52,41,47,57
121,1,4,2,1,3,68,59,53,63,61
 86,0,4,3,1,1,44,33,54,58,31
141,0,4,3,1,3,63,44,47,53,56
172,0,4,2,1,2,47,52,57,53,61
113,0,4,2,1,2,44,52,51,63,61
 50,0,3,2,1,1,50,59,42,53,61
 11,0,1,2,1,2,34,46,45,39,36
 84,0,4,2,1,1,63,57,54,58,51
 48,0,3,2,1,2,57,55,52,50,51
 75,0,4,2,1,3,60,46,51,53,61
 60,0,4,2,1,2,57,65,51,63,61
 95,0,4,3,1,2,73,60,71,61,71
104,0,4,3,1,2,54,63,57,55,46
 38,0,3,1,1,2,45,57,50,31,56
115,0,4,1,1,1,42,49,43,50,56
 76,0,4,3,1,2,47,52,51,50,56
195,0,4,2,2,1,57,57,60,58,56
114,0,4,3,1,2,68,65,62,55,61
 85,0,4,2,1,1,55,39,57,53,46

The Mplus commands to read the data are shown below.  These are the commands that you can enter into a blank Mplus text file and save as an input file (.inp). The title, data and variables command blocks are required. The Analysis command block is included so that we can check the data. We will go into this command block in more detail in the next unit.

Title:
  Entering data example free format using hsb.dat
Data:
  File is "D:/hsb.dat";
Variable:
  Names are
    id female race ses schtyp prog read write math science socst;
  Usevariables are
    id female race ses schtyp prog read write math science socst;
Analysis:
  Type = basic;

After saving and running the .inp file, you can look in the output file for "INPUT READING TERMINATED NORMALLY" appearing below the entered code. This is a good first check that your data were read in successfully.  We will discuss further checks in the next section.

2.0 Entering fixed format data

The file fixed.dat contains ten observations with the data in fixed columns. The codebook for the data is given below.

 195  094951
 26386161941
 38780081841
 479700  870
 56878163690
 66487182960
 786  069  0
 88194193921
 98979090781
107868180801
codebook
variable name column number
id 1-2
a1 3-4
t1 5-6
gender 7
a2 8-9
t2 10-11
tgender 12

Fixed format data are handled using a fortran-type format statement in the data command block. The Mplus commands are shown below.  3F2.0 indicates that the file begins with three variables each of length two.  These are followed by one variable of length one (F1.0), then two of length 2 and one of length 1 (2F2.0, F1.0).  This matches what we see in the codebook. 

Title:
  Entering data example fixed format using fixed.dat
Data:
  File is "D:/fixed.dat";
  Format is (3F2.0, F1.0, 2F2.0, F1.0);
Variable:
  Names are
    id a1 t1 gender a2 t2 tgender;
  Usevariables are
    id a1 t1 gender a2 t2 tgender;
Analysis:
  Type = basic;

Again, after saving and running this input, you can check the output to see if  "INPUT READING TERMINATED NORMALLY" appears.

3.0 Entering data using Stata

If you are a Stata user, ATS has written a command, stata2mplus, that will convert a Stata dataset to an Mplus ASCII data file plus the necessary commands to read in the data. You can get the stata2mplus ado file by typing findit stata2mplus.

Here is the Stata command to load and convert the Stata dataset hsb2.dta to Mplus.  It generates a .dat file containing the dataset and the input file needed to read the dataset into Mplus.  It stores both in the present working directory in Stata (enter pwd to get the path) with the dataset name: hsb2.dat and  hsb2.inp.

use "http://www.ats.ucla.edu/stat/stata/notes/hsb2.dta", clear
stata2mplus using hsb2

Looks like this was a success.
To convert the file to mplus, start mplus and run
the file hsb2.inp

The code from the input file created appears below. The file contains more detail about the data file than our earlier examples but all of the same command blocks are present. Again, the analysis type = basic is included to allow you to run descriptive statistics in order to insure that the data were input correctly.

Title:
  Stata2Mplus conversion for hsb2.dta
  List of variables converted shown below
  id :
  female :
    0: male
    1: female
  race :
    1: hispanic
    2: asian
    3: african-amer
    4: white
  ses :
    1: low
    2: middle
    3: high
  schtyp : type of school
    1: public
    2: private
  prog : type of program
    1: general
    2: academic
    3: vocation
  read : reading score
  write : writing score
  math : math score
  science : science score
  socst : social studies score
Data:
  File is hsb2.dat ;
Variable:
  Names are
     id female race ses schtyp prog read write math science socst;
  Missing are all (-9999) ;
  Usevariables are
     id female race ses schtyp prog read write math science socst;
Analysis:
  Type = basic ;

4.0 Entering missing data using Stata

Our next example of entering data is to enter a version of the hsb dataset that has missing data. Below is the Stata code for reading the missing data file and converting it to an Mplus data file along with an Mplus input file (hsbmis.inp).

use http://www.ats.ucla.edu/stat/mplus/seminars/IntroMplus/hsbmis.dta, clear
stata2mplus using hsbmis
Looks like this was a success.
To convert the file to mplus, start mplus and run
the file hsbmis.inp

The input file for this example is identical to the previous example except for the file name. 

5.0 Entering data from SPSS

If you are an SPSS user, you can prepare your data to be read into Mplus with a few steps detailed in SPSS FAQ: How can I move my data from SPSS to Mplus?. Starting from the hsb2.sav dataset, once you have created a .csv file, hsb2.csv, without variable names, the code below can read in your data.

Title:
  Entering data from SPSS
Data:
  File is "D:/data/hsb2.csv";
Variable:
  Names are
	id female race ses schtyp prog read write math science socst;
Analysis:
  Type = basic;

6.0 Entering missing data from SPSS

If your SPSS data contains missing data, complete the same steps you would for non-missing SPSS data, but note the values used for missing values. For example, if -999 is the value used in coding missing values, then the previous example's code would be amended with a Missing line in the Variable: block indicating this. Below, we use hsbmis.csv.

Title: 
  Missing data from SPSS
Data:
  File is "D:\data\hsbmis.csv" ;
Variable:
  Names are 
     id female race ses schtyp prog read write math science socst;
  Missing are all (-999) ; 
Analysis: 
  Type = basic ;

7.0 Entering missing data from a raw data file with dots (.)

Our last example of entering data is entering data with a raw data file that has dots (.) to represent missing values. Say that you had a data file that was either comma separated, tab separated, or space separated where . was used to indicate missing values.  You can indicate such missing values with a Missing are .; statement in the variable block like in the example below which reads the hsbmisdot.dat data file.

Title: Missing with dots;
Data:
  File is hsbmisdot.dat ;
Variable:
  Names are id female race ses schtyp prog read write math science socst;
  Missing are .;
Analysis:
  Type = basic;

The results of this are identical to the above example.


How to cite this page

Report an error on this page or leave a comment

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.