Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to import multiply-imputed Mplus data files into mi data?

    Can anyone recommend the best way to import separate, multiply-imputed Mplus .dat data files (plain ASCII text files) for analysis using Stata? It looks like this could be done using -mi import flongsep-, after converting all the Mplus datafiles into .dta files, and starting with an original (incomplete) dataset in memory, right? Has anyone written a program to convert the M datafiles to .dta format?

    Thanks,
    Peter
    [email protected]
    Last edited by Peter Liberman; 14 Nov 2014, 23:51.

  • #2
    I have no experience with SAS whatsoever, but there is a import sasxport command. So if you have access to SAS it might be easier to use this format than plain ASCII, although the latter should also be easily imported, given information about the exact setup.

    A program that converts M files would probably basically be a foreach loop over the filenames. Something along the lines

    Code:
    loc mysasfies : "<my_sas_directory>" file "*.xpt"
    
    loc m = 0
    foreach fn of loc mysasfiles {
        import sasxport "<my_sas_directory>/`fn'" ,clear
        loc newfn <dtastub>`++m'
        save "<my_dta_directory>/`<dtastub>'"
    }
    where the elements in < and > are to be replaced.

    Best
    Daniel
    Last edited by daniel klein; 15 Nov 2014, 03:10.

    Comment


    • #3
      Thanks, Daniel. I think I can figure out how to adapt this to import and destring ASCII text files, which would save a lot of time from doing this manually!

      Comment


      • #4
        Hi Daniel,

        Would you be so kind as to elaborate a bit on this? Free to take a pass--or direct me to a good primer--if an explanation for someone with such a primitive (nonexistent) understanding of programming would take too much time!

        I figure on doing this using the -infile- command, which works fine to convert individual files from mplus (.dat files). I tried adapting the program you sketched out, to run -infile- for 50 .dat files in the "mplusg" directory, and save the datafilee as mifile1.dta, mifile2.dta, mifile3.dta, etc. I thus replaced <dtastub> with "mifile". I also added "dir" to the loc command, because without it I got an error message:
        "/Users/peter/Documents/mplusg not allowed
        r(101);
        The following program [I've removed the variable names] ran the first infile command:
        loc mymplusfiles : dir "/Users/peter/Documents/mplusg" files "*.dat"
        loc m = 0
        foreach fn of loc mymplusfiles {
        infile VARLIST using "/Users/peter/Documents/mplusg/`fn'" ,clear
        loc newfn mifile`++m'
        save "/Users/peter/Documents/statag/`mifile'"
        }
        But it returned an error message on save:
        file /Users/peter/Documents/statag/.dta already exists
        r(602);
        There is nothing in the "statag" directory. However, I imagine I have done something wrong in specifying the directory or new file names to be saved.

        Many thanks for any advice. This would be a real timesaver if I could get it to work!

        Best,
        Peter

        Comment


        • #5
          I also added "dir" to the loc command, because without it I got an error message:
          "/Users/peter/Documents/mplusg not allowed
          r(101);
          Yes, you are right. Now let me just spot your coding-error in return. In the call to save you refer to the local mifile, which is never defined. You instead want to refer to newfn. Here is the code, with minor changes

          // we change the directory to make typing easier
          cd "/Users/peter/Documents/mplusg"

          loc mymplusfiles : dir . files "*.dat"
          loc m = 0
          foreach fn of loc mymplusfiles {
          infile VARLIST using `"`fn'"' ,clear
          // compound quotes are probably not necessary
          loc newfn mifile`++m'
          save `"`newfile'"'
          }
          Best
          Daniel

          Comment


          • #6
            Works brilliantly, after I fixed typo of "newfile" in the save command, replacing it with "newfn".

            I owe ya one!

            Comment

            Working...
            X