Announcement

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

  • Loop for selecting variables from different files and appending in a new dataset

    Dear all,

    I'm using DHS surveys and I'd like to create a loop to select the same variables from different files (surveys) and append them in one new dataset. STATA doesn't point out any errors but the final dataset is not correct as variables don't match (i.e Brazil's region attributed with Kabul). Also I intended to create a PCA score for each country based on these variables. Here is my code:

    Code:
    local files : dir . files "*.dta"
    
    
    tempfile nfile
    save `nfile', emptyok
    
    foreach f of local files {
         use `"`f'"', clear
         keep hv000 hv007 hv024
         sort hv000 hv007 hv024
         pca hv000 hv007 hv024 , comp(2) covariance
         append using `nfile'
         save `"`nfile'"', replace
    }
    label data "jj, long format"
    save `"`dir1'jj.dta"', replace
    Thank you,

    Best.
    Last edited by Gedeao Locks; 07 Aug 2018, 04:02.

  • #2
    This is not easy to diagnose, but I can't see anything wrong with your code for appending.
    Stata won't have mixed up values for different regions unless you have asked it to do so.
    There could be some issue with value labels being applied incorrectly, i.e., having one meaning in one dataset and another in another dataset.

    This will be easier to diagnose with a data example of two of your files. Please post them using dataex; see the FAQ on how and why: https://www.statalist.org/forums/help#stata
    Also please explain if separate files are for separate regions, or?

    Comment


    • #3
      No, each file is a country-year survey. What I would like is to automatize the code; you're right, Regions have label values for each file, so when I append the files, my Region variable has the label value from my first file. Is it possible to solve this?

      Thank you again.

      Comment


      • #4
        Decode the labelled variables before you append them.
        https://www.stata.com/manuals13/dencode.pdf

        For more specific code, post data examples.

        Comment


        • #5
          Great, that solved. Thank you so much!

          Comment

          Working...
          X