Announcement

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

  • Appending datasets by keeping the common variabels only

    Hi,
    I want to append datasets for various countries, each of which contains variables that others don't. I want to append them in a way that'll keep only the variables present in all. I'm not sure how to show this in dataex as there are several datasets. I tried my luck with the following as missing vars as coded as '.'
    Code:
     append using "F:\Uganda\ugla.dta", keep if country_id !=.
    But no luck yet.
    Please let me know if this doable.
    Thanks in advance.

  • #2
    Maybe something analogous to the following. (Begin at the "Begin here" comment.) There are two passes through the set of datasets, the first to build the list of common variables and the second to do the appending.
    Code:
    version 16.0
    
    clear *
    
    quietly sysuse auto
    preserve
    
    drop make
    quietly save nomake
    
    restore
    preserve
    drop price
    quietly save noprice
    
    restore
    drop mpg
    quietly save nompg
    
    drop _all
    
    *
    * Begin here
    *
    local common
    foreach dataset in nomake noprice nompg {
        quietly describe using `dataset', varlist
        local `dataset' `r(varlist)'
    
        if missing("`common'") local common ``dataset''
        else local common : list common & `dataset'
    }
    
    foreach dataset in nomake noprice nompg {
        if _N == 0 use `common' using `dataset'
        else append using `dataset', keep(`common')
    }
    
    exit

    Comment


    • #3
      Originally posted by Joseph Coveney View Post
      Maybe something analogous to the following. (Begin at the "Begin here" comment.) There are two passes through the set of datasets, the first to build the list of common variables and the second to do the appending.
      Code:
      version 16.0
      
      clear *
      
      quietly sysuse auto
      preserve
      
      drop make
      quietly save nomake
      
      restore
      preserve
      drop price
      quietly save noprice
      
      restore
      drop mpg
      quietly save nompg
      
      drop _all
      
      *
      * Begin here
      *
      local common
      foreach dataset in nomake noprice nompg {
      quietly describe using `dataset', varlist
      local `dataset' `r(varlist)'
      
      if missing("`common'") local common ``dataset''
      else local common : list common & `dataset'
      }
      
      foreach dataset in nomake noprice nompg {
      if _N == 0 use `common' using `dataset'
      else append using `dataset', keep(`common')
      }
      
      exit
      Thanks a lot Joseph! I'm not very familiar with functions like 'local' and loop, so I'm not sure how to load the datasets before applying this procss. There are about 30 datasets in the directory, should I use them as -local directory- instead of -local common- in my case?

      Comment


      • #4
        I'm not very familiar with functions like 'local' and loop
        A good way to gain familiarity would be to review Chapter 18 Programming Stata in the Stata User's Guide PDF included with your Stata installation and accessible through Stata's Help menu.

        Comment


        • #5
          Originally posted by William Lisowski View Post

          A good way to gain familiarity would be to review Chapter 18 Programming Stata in the Stata User's Guide PDF included with your Stata installation and accessible through Stata's Help menu.
          Thanks for the reference!

          Comment

          Working...
          X