Announcement

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

  • bys x : egen use

    Hello, I am a novice user of stata....

    I ask you for help solving my problem generating new var...


    I was trying to generate new variable which was to take a mean by district (var name : "name_3") excluding myself ("hhid")....

    Can you guys give me some tips to create such var in my situation?

    below is the code that I tried, but seems make no sense..

    bys name_3 : egen peerdiv1 = mean(diversity1) if 000
    name_3 hhid di1 di1 di1 di1 di1 ?
    An Minh 909 2 1 0.996933 0.0625 4
    An Minh 910 1 0 1 0.015625 5
    An Minh 911 1 0 1 0.015625 5
    An Minh 912 4 1 0.924703 0.25 2
    An Phu 913 1 0 1 0.015625 5
    An Phu 914 1 0 1 0.015625 5
    An Phu 915 1 0 1 0.015625 5
    An Phu 916 2 1 0.555556 0.0625 1

    Appreciate your wisdom in advance,

    Manny









  • #2
    I am hard put to understand your data example. Please read and act on http://www.statalist.org/forums/help#stata to use dataex to give examples that people can copy and paste into their Stata. Here the column headers seem to imply copying from a spreadsheet as in Stata di1 could be a variable name for one variable only. It's not clear which variable is diversity1 nor what you could mean by if 000. Further, some of the variables are presumably irrelevant to your problem but it's not clear which.

    All that said, please consider this worked example:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str7 name_3 int hhid byte diversity
    "An Minh" 909 2
    "An Minh" 910 1
    "An Minh" 911 1
    "An Minh" 912 4
    "An Phu"  913 1
    "An Phu"  914 1
    "An Phu"  915 1
    "An Phu"  916 2
    end
    
    . encode name_3, gen(district)
    
    . * you must install rangestat before you can use it.
    . ssc install rangestat
    
    . rangestat (mean) diversity, int(district 0 0) excludeself
    Code:
    . l name_3 district hhid diversity diversity_mean , sepby(district)
    
         +--------------------------------------------------+
         |  name_3   district   hhid   divers~y   diversi~n |
         |--------------------------------------------------|
      1. | An Minh    An Minh    909          2           2 |
      2. | An Minh    An Minh    910          1   2.3333333 |
      3. | An Minh    An Minh    911          1   2.3333333 |
      4. | An Minh    An Minh    912          4   1.3333333 |
         |--------------------------------------------------|
      5. |  An Phu     An Phu    913          1   1.3333333 |
      6. |  An Phu     An Phu    914          1   1.3333333 |
      7. |  An Phu     An Phu    915          1   1.3333333 |
      8. |  An Phu     An Phu    916          2           1 |
         +--------------------------------------------------+
    Here for example for hhid 910 the mean 2 is the mean for the other households in the same district (1 + 1 + 4) / 3 and for 916 also 1 is the mean of 1 1 1 for the others in the same district.

    Hope that helps. See also https://www.stata.com/support/faqs/d...ng-properties/
    Last edited by Nick Cox; 18 Apr 2019, 01:16.

    Comment


    • #3
      Hi Nick!

      It worked as you explained above!

      That was exactly what I wanted!

      Thank you so much for your help!

      Best, Manny

      Comment

      Working...
      X