# st: re: double if-sentences in Stata

 From Christopher Baum To statalist@hsphsun2.harvard.edu Subject st: re: double if-sentences in Stata Date Thu, 27 Nov 2008 14:00:32 -0500

```<>
Linn said

Here is what I write. ...
```
I thus try to write a command where the executed command if the result (if shift==-1) is true also includes an if-formulation, and the same with the executed command in the case where the result of the initial criterion is false. If the shift value is -1, what I do with the variable flow depends on whether the sum (flow[_n-1]+`cap') is larger or smaller than the value given in `add'. Similarly, if the shift variable isn't -1 but 1, what I do with flow depends on the sum (flow[_n-1]-`cap') and whether this sum is smaller or larger than the value given by `add'.
```
```
Others have pointed out that unlike other programming languages, the - if- command is rarely the answer to a problem where you want to test the value of each observation. I believe that this code does what you descirbed in your original posting.
```----------------
clear
set obs 10
set seed 20081127
g t = _n
g cap = log(t+1)
g add = _pi + runiform()
tsset t
g byte shift = cond(runiform() > 0.5, 1, -1)
g flow = 5 in 1
replace flow = ///
(shift == -1 ) * cond(L.flow + cap < add, -cap, L.flow - add) ///
+ (shift == 1) * cond(L.flow - cap > add, cap, L.flow + add) in 2/l
list
-----------------
```
In this case, I have avoided any use of -if- (as a command or qualifier!) by using the cond() function and the Boolean test, i.e. (foo == bar) evaluates to 1 if the condition is true and 0 if it is false. Also note that the construction of the running variable -flow- must avoid the first observation, lest a missing value be propagated.
```
Kit Baum, Boston College Economics and DIW Berlin
http://ideas.repec.org/e/pba1.html
An Introduction to Modern Econometrics Using Stata:
http://www.stata-press.com/books/imeus.html

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