Announcement

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

  • How to Append .txt file

    Hi, how can I append a dataset with another using files that are in .txt format?
    I tried to use the command
    Code:
     
    append using "C:\Users\enric\Desktop\DATASET-LFS-
    > 2014-2020\LFS_2009\2009_1\MICRODATI\bolla.txt
    but stata can't find the file, however when I convert the txt file to a data file the command works, how should I do?.
    Thanks for your help

  • #2
    You will have to convert your txt file to a Stata dataset for the append command, or indeed for most any Stata command, to be able to use it.

    Comment


    • #3
      thanks, have you any suggestion on how to do it with a loop cycle?
      Thank you

      Comment


      • #4
        Hi I tried this loop to create .dta file for each of the txt but it didn't work. Stata gives me an error of invalid file specification.
        Code:
         
         clear cd"C:\Users\MyLocalDirectory\" local list : dir . files "*.txt" foreach file of local list { import delimited using `file' local newf : subinstr local f ".txt" ".dta", all  save "`newf'", replace }
        What can I do?

        Comment


        • #5
          Based on the code you showed in post #1, the changes I recommend to the code in post #3 are highlighted in red.
          Code:
          clear 
          cd "C:\Users\enric\Desktop\DATASET-LFS-2014-2020\LFS_2009\2009_1\MICRODATI\" 
          local list : dir . files "*.txt" 
          foreach file of local list { 
              display `"importing: `file'"'
              import delimited using `"`file'"', clear
              local newf : subinstr local file ".txt" ".dta", all  
              save `"`newf'"', replace 
          }
          where I enclose the file names in compound double quotes in case they contain spaces or special characters. (See the output of help quotes for an explanation.)

          Here is a variant of that which creates the single file containing all the appended data without having to save individual files after importing each into a Stata dataset.
          Code:
          clear 
          cd "C:\Users\enric\Desktop\DATASET-LFS-2014-2020\LFS_2009\2009_1\MICRODATI\" 
          save alldata, replace emptyok
          local list : dir . files "*.txt" 
          foreach file of local list { 
              display `"importing: `file'"'
              import delimited using `"`file'"', clear
              append using alldata
              save alldata, replace 
          }

          Comment


          • #6
            thank you a lot William for you help. Do you know what happen when the number of variables are different for some .txt files? I obtained the appended file in dta fomat but I don't know if I can trust the table. the Name of each variables should be equal but I dont't know why some quarters have more variables, I hope that didn't interfer with the column with the same variables.

            Comment


            • #7
              In a dataset created by append, any variable will have missing values for all the observations that came from datasets that did not include that variable. That seems to be what you are seeing.

              I have been assuming your text files have a line of names preceding the lines of data. Append will always match the variable names, regardless of what order they are in in the dataset. So have additional variables, or variables not in the same order as in other datasets, does not make a difference.

              Comment


              • #8
                thank you so much!

                Comment

                Working...
                X