Announcement

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

  • Duplicates drop

    Hello all, I am trying to drop the duplicate id. For example in below i have computed the total of expenses for each categories for a household using egen var= total(var), by(WWWHH). Now i want the total for each variables corrosponding to each id. However, as can be seen in data, for id 1, the total of grains is appeared in first few rows while pules expenditure appears thereafter and so on. If i use the command duplicates drop then my pulses_exp milk_exp will all be missing. How i can total for example like
    id grains_exp pulse_exp
    1 2544 160
    and so on.
    ,
    Click image for larger version

Name:	stat_query.jpg
Views:	1
Size:	56.3 KB
ID:	1346121

  • #2
    Naveen:
    you may want to try:
    Code:
    set obs 10
    g id=1
    g grain_exp=10 in 1/5
    g pulse_exp=5 in 6/10
    collapse (mean) grain_exp pulse_exp, by( id)///advice: save your dataset in a new file before -collapse-
    su grain_exp pulse_exp
    egen total=rowtotal( grain_exp pulse_exp)
    Kind regards,
    Carlo
    (Stata 18.0 SE)

    Comment


    • #3
      What you need here is to collapse your data by id:
      Code:
      collapse (max) grains_exp pulses_exp milk_exp, by(WWWHH)

      Comment


      • #4
        Well, here's one way
        Code:
        tempfile tmpfil0
        
        // Primes the pump and allows trapping of all-missings
        preserve
        contract WWWHH, freq(discard)
        quietly save `tmpfil0'
        restore
        
        foreach var of varlist *_exp {
            preserve
            contract WWWHH `var', freq(discard)
            keep if !mi(`var')
            merge 1:1 WWWHH using `tmpfil0', assert(match) nogenerate noreport // Traps duplicates, also
            quietly save `tmpfil0', replace
            restore
        }
        
        use `tmpfil0', clear
        drop discard

        Comment


        • #5
          Thank you all, it really helped me.

          Comment

          Working...
          X