![stata egen stata egen](https://i.ytimg.com/vi/NgWMgTdsp7Q/mqdefault.jpg)
> label define popcl 1 "low" 2 "medium" 3 "high" Let’s label them as low, medium and high. * Remember we categorized pop_c into three categories: 1,2 and 3 Poplt5 long %12.0gc Pop, label variable pop0_17 "Pop, label variable pop_c "Categorized population" Here we create another new variable called pop_c2 then do the recode in the same manner as we did for pop_c. We can use the -recode- command to recode variables as well. Then we create a new variable called pop_c and transform the original variable pop into three categories. Here we create the youth population variable again, but this time we make it into thousands and replace the one we just created. replace-: replace contents of existing variables > order state state2 region pop poplt5 pop0_17
![stata egen stata egen](https://slidetodoc.com/presentation_image/48ee7a9cb3a8eb6852752f1a3b75bdc1/image-16.jpg)
* Summary statistics for the three variables Poplt5 long %12.0gc Pop, generate pop0_17 = poplt5 + pop5_17 State2 str2 %-2s Two-letter state abbreviation
![stata egen stata egen](https://miro.medium.com/max/1400/1*8qBDPuKnT6H7-_0uI_srjg.png)
Variable name type format label variable label Contains data from /Applications/Stata/ado/base/c/census.dta Many other summary measures don't yield to a similar, simple trick and in that sense rangestat includes much more general coding. Mean for others = (total - value for self) / (count - 1) But while the solution for means is easy using an identity Not surprisingly, the option excludeself was explicitly added for these kinds of problem. rangestat (SSC) can be used here and gives one-line solutions. (If the numbers get big, work with doubles.)ĮDIT 2 March 2018 That was a newer post in an old thread, which in turn needs updating. How do I create variables summarizing for each individual properties of the other members of a group? Gen avprice = (wttotal - price * weight) / (sumwt - weight)Īs before, if price or weight is ever missing, you need more complicated code, or just to ensure that you exclude such observations from the calculations. egen wttotal = total(weight * price), by(market)
STATA EGEN CODE
Indeed the code above did that already, but it is often overlooked. We can exploit the fact that total() works on expressions, which can be more complicated than just variable names. Presumably we want a weighted average of others' prices given some weight. So far, this possibly looks like no more than a small variant on previous code, but it does extend easily to using weights. If no value is missing, the last line above simplifies to gen avprice = (total - price) / (n - 1) egen's count() is another way to do this.Ĭode given earlier gives the wrong answer if missings are present as they are included in the count _N.Įven if a value is missing, the average of the other values still makes sense. If there are missing values, we don't want to include them in the count, but we can use !missing() which yields 1 if not missing and 0 if missing. The total() function of egen ignores missing values in its argument. Gen avprice = (total - cond(missing(price), 0, price)) / cond(missing(price), n, n - 1) In a egen framework that is going to look like egen total = total(price), by(market)Įgen n = total(!missing(price)), by(market)
![stata egen stata egen](https://i.stack.imgur.com/EqZqm.png)
At its simplest, total of others = total of all - this value. The more general technique is to work with totals. This is an old thread still of interest, so materials and techniques overlooked first time round still apply.