Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
st: RE: lag
From
Nick Cox <[email protected]>
To
"'[email protected]'" <[email protected]>
Subject
st: RE: lag
Date
Thu, 31 May 2012 11:01:01 +0100
If -sale[_n-4]- is not defined because _n-4 <= 1 then Stata evaluates -sale[_n-4]- as missing, meaning . or system missing. Only values of .a ... .z can be greater than that.
This may be clearer if your code is re-written as one statement:
by cusip: generate sale_decrease5 = sale <= sale[_n-4]
What is being evaluated is the expression
sale <= sale[_n-4]
and this will be returned as 1 or 0, depending on whether it is true. Missing values of either argument don't affect that.
This is on all fours with (say)
. di (42 <= .)
1
If you want your new variable to be . if either argument is missing you can do this
by cusip: generate sale_decrease5 = sale <= sale[_n-4] if !missing(sale, sale[_n-4])
and then your indicator will have possible values 0, 1 and missing.
You could also do this:
by cusip: generate sale_decrease5 = sale <= sale[_n-4] if _n > 4
See also
FAQ . . . . . . . . . . . . . . . . . . . . . . . True and false in Stata
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
8/05 What is true and false in Stata?
http://www.stata.com/support/faqs/data/trueorfalse.html
Nick
[email protected]
Fabian Schönenberger
With the following command I want to generate a dummy variable; 1 if current sales are lower than 4 years ago, 0 if sales are larger for each company:
by cusip: generate sale_decrease5=0 if sale>sale[_n-4]
by cusip: replace sale_decrease5=1 if sale<=sale[_n-4]
I expected for each cusip 4 missings at the beginning, since there is no _n-4. However, the command gives 1 for each cusip in the first four years.
How do I have to adapt the formula to get dummy variables after the first 4 years passed?
*
* 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/