Announcement

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

  • Trouble creating new variable from existing variables

    I'm new to Stata but familiar with stat programs I thought this would be easy. I'm trying to create a new variable (G02) which takes on the value of another variable (Obs) if a third variable's condition (Gap) is met. I tried to do this when the third variable was a string variable and got an error: type mismatch so I changed the third variable so that all were numeric and I still get an error - invalid syntax.

    generate G02 = Obs if Gap=2
    invalid syntax
    r(198);

    I did it again with generate G02 = . and then replacing G02 using the same if statement and got the same invalid syntax issue.

    I need to break down Gap into a bunch of new variables depending on the value of Gap. Why can't I make this work? Thanks for you help.

  • #2
    Never mind, figured it out. Stata apparently doesn't like just an = sign. It works with: generate G02=Obs if (Gap>=2) & (Gap<3)

    Comment


    • #3
      In Stata, as in many other programming languages, you have to distinguish between the assignment operator (=) and the equality relation (==). They are not interchangeable. You need
      Code:
      gen G02 = Obs if Gap == 2
      Added: Crossed with #2. Your solution will work provided Gap is an integer. But if you try the same kind of thing with a variable that is floating point, it will fail. Just use the == operator to refer to the equality relation.

      Comment


      • #4
        That's great, and much better than what I was doing to make it work, thank you!

        Comment

        Working...
        X