Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Looping over pairs of text and numbers

    Hi everyone, I have this code (I have simplified it here, but same concept)

    gen x_i = 0
    replace x_i= 1 if (a> 0.i )

    and I need to repeat this 10 times. The actual code is much longer than these two lines.

    Basically, "i" takes the following values: 00, 01, 02, 05, 10, 15, 20 etc. I don't know how to use a loop to get this done cause "i" is a string and at the same time, is going to be part of a number in a>0.i

    for example: a>0.0 a>0.01 a>0.02 etc

    Is this even possible ?



  • #2
    Yes it can be done:

    Code:
    foreach i in 00 01 02 05 10 15 20 {
        gen x_`i' = (a > 0.`i')
    }
    Note, by the way, that it is unnecessary to use both a generate and a replace command here As shown above, a single appropriate generate command will do it.

    Do read the manual section on -foreach-.

    But you may find that this doesn't work out quite as you expect. The problem is that numbers like 0.01, 0.02, etc. in general have no exact representation in binary, just as 1/3 has no exact representation as a finite decimal. Take a look at this:

    Code:
    clear
    set obs 1
    gen a = 1/100
    display a
    assert a == 0.01
    The point is that if your variable a was calculated in some way, the result will be start with the closest approximation to the true answer that Stata can achieve in a float variable. If the calculation was something that, in real number calculation would produce 0.01 as the answer, what Stata will come up with will be a little off from that. It might turn out to be, say, just a tad higher (or it could be just a tad lower). If it does come out a tad higher, then x_01 will be set to 1 when it should not be. There is no ideal solution to this problem. Just be aware that in "close call" cases things may turn out in unexpected ways.

    Comment


    • #3
      The code has an error
      assert a == 0.01
      assertion is false

      Comment


      • #4
        Co Ar: Wrong way round. The code is intended to show you that you won't (can't) have exact matches with 0.01 -- because 0.01 can't be held as an exact binary.

        For more on precision, see resources identified by

        Code:
        search precision
        For more on this kind of loop see http://www.stata-journal.com/sjpdf.h...iclenum=pr0051

        Comment


        • #5
          Ok thanks guys, I knew how to use foreach, just thought I couldn't put the text in the middle of the number like a > 0.`i' . I guess I should have tried first.

          And wow I had no idea about the exact binary value. I'll look into it to see how it will affect my code.

          Thanks again

          Comment


          • #6
            just thought I couldn't put the text in the middle of the number like a > 0.`i'
            Well, the thing to remember is that a Stata macro is nothing more or less than a string of characters. Wherever you refer to it by name with the ` before and ' after, Stata just substitutes those characters in that place, wherever it is in the code. That's why it can be used for numbers, variable names, commands, parts of commands, pieces of variable names, pieces of numbers, whatever. It's really this that makes them so useful.

            Comment

            Working...
            X