Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down at the end of May, and its replacement, **statalist.org** is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
"Martin Weiss" <martin.weiss1@gmx.de> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
st: AW: AW: generate new variable from differencing where subtracted variable is fixed |

Date |
Fri, 5 Mar 2010 17:32:39 +0100 |

<> Disassemble this into separate processes if you want to: ************* clear* inp int year byte(x1 x2) 1960 5 8 1960 4 10 1960 2 3 1961 5 7 1961 1 2 1961 3 5 end //get first value per year into new var //use -bysort- here as in NJC`s //http://www.stata-journal.com/sjpdf.html?articlenum=pr0004 bys year: gen byte x2_1=x2[1] //form diffs gen byte diff=x1-x2_1 list, noo sepby(year) ************* You first use -bysort- to have Stata interpret any subscripts as within the groups fed as a -varlist- to -bysort-. See NJC´s article cited in the code. The result is year-specific, so the next step can afford to omit the -bysort-, and take the difference unconditionally... HTH Martin -----Ursprüngliche Nachricht----- Von: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Martin Weiss Gesendet: Freitag, 5. März 2010 17:23 An: statalist@hsphsun2.harvard.edu Betreff: st: AW: generate new variable from differencing where subtracted variable is fixed <> ************* clear* inp int year byte(x1 x2) 1960 5 8 1960 4 10 1960 2 3 1961 5 7 1961 1 2 1961 3 5 end bys year: gen diff=x1-x2[1] list, noo ************* HTH Martin -----Ursprüngliche Nachricht----- Von: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Johannes Schoder Gesendet: Freitag, 5. März 2010 17:00 An: statalist@hsphsun2.harvard.edu Betreff: st: generate new variable from differencing where subtracted variable is fixed Dear Statalist users: I try to calculate the difference between two variables for a panel data set, where the subtracted variable (here x2) is always the first observation of each year: For example: Year x1 x2 difference: x1-x2 if x2 is the first obs. of the year 1960 5 8 5-8 1960 4 10 4-8 1960 2 3 2-8 1961 5 7 5-7 1961 1 2 1-7 1961 3 5 3-7 I tried something like: bysort year: gen difference= x1-x2[_n==1] but Stata does only calculate the first observation for each year and otherwise generates missings. Thanks a lot for any hint! Regards, Johannes -- Johannes Schoder Socioeconomic Institute University of Zurich Hottingerstr. 10 8032 Zurich Phone: +41 44 634 0612 Fax: +41 44 634 4987 E-mail: johannes.schoder@soi.uzh.ch * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/ * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/ * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

**References**:**st: generate new variable from differencing where subtracted variable is fixed***From:*Johannes Schoder <johannes.schoder@soi.uzh.ch>

**st: AW: generate new variable from differencing where subtracted variable is fixed***From:*"Martin Weiss" <martin.weiss1@gmx.de>

- Prev by Date:
**st: RE: generate new variable from differencing where subtracted variable is fixed** - Next by Date:
**st: RE: RE: how to choose the # for --set seed #--** - Previous by thread:
**st: AW: generate new variable from differencing where subtracted variable is fixed** - Next by thread:
**st: RE: generate new variable from differencing where subtracted variable is fixed** - Index(es):