Announcement

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

  • mata with if and forevalue

    HI!
    I have a problem. This is my code :
    forvalues p=1/$nb_p {
    forvalues i=1/$nb_p {
    if pair[`p',1]=pair[`i',1] {
    mata somme2`i'=somme-totob`p'
    }
    }
    }

    The error is "=exp not allowed
    r(101);"

    When I write if `p'=`i' or if pair[`p',1]=pair[`i',1] it fails.. but when I write if `p'!=`i' or if pair[`p',1]!=pair[`i',1] there is no error;..
    I don't understand why, I am lost...

    Thanks!
    Clara

  • #2
    In Stata, a single equals sign is assignment. To test for equality, you need a double equals sign. Try
    Code:
    if pair[`p',1] == pair[`i',1] {
    and see whether that helps.

    Comment


    • #3
      Oh I was sooo stupid!! Thank you very very much joseph!!

      Comment


      • #4
        No, not stupid.

        It's one of the more common errors that I still make when coding hastily, especially when working at the same time in other languages that use the single symbol for both assignment and comparison.

        It'd be nicer if Stata's error message added "did you intend '==' ?"

        Comment


        • #5
          Yes it would be good...

          Comment

          Working...
          X