Bookmark and Share

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, is already up and running.

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

Re: st: Generating a Variable indicating Change

From   Rebecca Pope <>
Subject   Re: st: Generating a Variable indicating Change
Date   Thu, 6 Jun 2013 10:54:45 -0500

Do you mean that you want the new variable to be the amount of the
change or whether a change occurred? I've given an example of both.

Let's say you have the following variables (for future reference, it
is a good idea to provide some idea of the relevant variables &
structure of your data):

croptype - an ID variable for the type of crop
year - the year of measurement
acres - the number of acres of land devoted to crop(i) in year(t)

I have assumed that you have crop-level panel data based on your
question. Please correct me if I'm wrong.

*** begin example ***
 // use by and Stata's subscripts to calculate amount of change
bysort croptype (year): gen amtchng = acres-acres[_n-1]

 // use Stata's lag operator and logical test to create indicator
 // for whether or not change occurred
bysort croptype (year): gen anychng = (acres-acres[_n-1] != 0) if _n!=1
*** end example ***

The second -bysort- isn't strictly necessary if you run both commands.
You can just use -by- the second time.


On Thu, Jun 6, 2013 at 7:53 AM, Kelvin Mulungu
<> wrote:
> Hi All,
> I want to generate a variable that is supposed to show the change in
> land planted to a certain crop. Is there a single command for doing
> this or I just lag the variable first, then get the difference?
> Thanks.
> *
> *   For searches and help try:
> *
> *
> *
*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index