Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# st: RE: generate new variable from differencing where subtracted variable is fixed

 From "Nick Cox" To Subject st: RE: generate new variable from differencing where subtracted variable is fixed Date Fri, 5 Mar 2010 16:29:20 -0000

```The solution is simpler.

bysort year : gen diff = x1 - x2[1]

Why is your code not working? Think it through step by step:

_n == 1 is true (1) when _n is 1 and false otherwise (0).

So x2[_n==1] is x2[1] or x2[0] as the case may be.

But x2[0] is not part of your data, and Stata can only substitute
missing when it evaluates it.

So -diff- is only non-missing in the first observation in each -year-,
as you report.

However, your solution needs care when extending to panels,

Nick
n.j.cox@durham.ac.uk

Johannes Schoder

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.

*
*   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/
```