Statalist


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

st: RE: using if sentences in Stata


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: using if sentences in Stata
Date   Thu, 27 Nov 2008 17:59:20 -0000

First, note that

if shift == -1 { 

means 

if shift[1] == -1 { 

which is probably why you are not getting the results you expect. 

There is a big difference between the -if- command and the -if- qualifier. 
This has been the focus of several recent threads on this list, including one started by Ashim Kapoor. See also the FAQ 

FAQ     . . . . . . . . . . . . . . . . . . . . .  if command vs. if qualifier
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  J. Wernow
        6/00    I have an if command in my program that only seems
                to evaluate the first observation, what's going on?
                http://www.stata.com/support/faqs/lang/ifqualifier.html

Also, note that (e.g.) 

replace flow= (-1*`cap')

-replace-s in _all observations. 

You can combine two or more -if-s. You just use as many conditions as are needed together with whatever operators are needed. 

. sysuse auto, clear 
. list foreign rep78 if foreign == 1 & rep78 == 4

     +-----------------+
     | foreign   rep78 |
     |-----------------|
 55. | Foreign       4 |
 56. | Foreign       4 |
 58. | Foreign       4 |
 59. | Foreign       4 |
 62. | Foreign       4 |
     |-----------------|
 63. | Foreign       4 |
 70. | Foreign       4 |
 72. | Foreign       4 |
 73. | Foreign       4 |
     +-----------------+

. list foreign rep78 if foreign == 1 | rep78 == 4

     +------------------+
     |  foreign   rep78 |
     |------------------|
  5. | Domestic       4 |
 15. | Domestic       4 |
 24. | Domestic       4 |
 29. | Domestic       4 |
 30. | Domestic       4 |
     |------------------|
 33. | Domestic       4 |
 35. | Domestic       4 |
 38. | Domestic       4 |
 47. | Domestic       4 |
 53. |  Foreign       5 |
     |------------------|
 54. |  Foreign       3 |
 55. |  Foreign       4 |
 56. |  Foreign       4 |
 57. |  Foreign       5 |
 58. |  Foreign       4 |
     |------------------|
 59. |  Foreign       4 |
 60. |  Foreign       3 |
 61. |  Foreign       5 |
 62. |  Foreign       4 |
 63. |  Foreign       4 |
     |------------------|
 64. |  Foreign       . |
 65. |  Foreign       3 |
 66. |  Foreign       5 |
 67. |  Foreign       5 |
 68. |  Foreign       5 |
     |------------------|
 69. |  Foreign       5 |
 70. |  Foreign       4 |
 71. |  Foreign       5 |
 72. |  Foreign       4 |
 73. |  Foreign       4 |
     |------------------|
 74. |  Foreign       5 |
     +------------------+

My impression is that you need -if- qualifiers, but you are using -if- commands. Only very rarely in practice does an -if- command pivot on a variable's value. 

On a minor detail, negation 

replace flow= -`cap'

is even easier than multiplying by -1. 

Nick 
[email protected] 

Linn Renée Naper

I am trying to program a "double if-sentence" in Stata.
Is this possible? And what do I do wrong here?


Here is what I write. The results is that Stata
only performs the commands after the second "else":
replace flow=`cable_cap'.


if shift==-1 {{
if (flow[_n-1] + `cap')<`add' {
replace flow= (-1*`cap')
}
else {
replace flow=flow[_n-1]-`add'
}
}
else {
if (flow[_n-1] - `cap')>`add' {
replace flow=`cap'
}
else {
replace flow=flow[_n-1]+`add'
}
}

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'.


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



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index