Announcement

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

  • Code debug creation of conditional variable

    Hello,

    I have written the following code in Stata and I get a r(32) error (too many parentheses). I don't understand why, as I should need 9 closing parentheses as I have nine conditions?
    Thanks much for your help

    foreach year of local years {
    generate state`year' = cond((poste`year' == "assistant" & rang`year' == "R1"), "state1", ///
    cond((poste`year' == "assistant" & rang`year' == "R2"), "state2", ///
    cond((poste‘year’ == “assistant” & rang‘year’ == “R3”), “state3” ///
    cond((poste`year' == "associé" & rang`year' == "R1"), "state4", ///
    cond((poste`year' == "associé" & rang`year' == "R2"), "state5", ///
    cond((poste`year' == "associé" & rang`year' == "R3"), "state6", ///
    cond((poste`year’ == "full" & rang`year' == "R1"), "state7", ///
    cond((poste‘year’ == "full" & rang`year' == "R2"), "state8", ///
    cond((poste`year' == "full" & rang`year' == "R3"), "state9", ///
    "", "")))))))))
    }

  • #2
    The punctuation is wrong in so far as

    Code:
    rang‘year’ == "R3"
    should be

    Code:
    rang`year' == "R3"
    and

    Code:
    poste‘year’ == "assistant"
    should be

    Code:
    poste`year' == "assistant"
    Watch for other similar slips. The punctuation around a local macro reference should be

    Code:
    ` '
    and that around a literal string should be

    Code:
    ""
    That said, I believe that the inside of the loop might be reduced to

    Code:
    egen work  = group(poste`year' rang`year')
    gen state`year' = "state" + strofreal(work)
    drop work
    without or at worst with a qualifier

    Code:
    if inlist(poste`year', "assistant", "associé", "full")  & inlist(rang`year',  "R1", "R2", "R3")
    Last edited by Nick Cox; 24 May 2023, 14:33.

    Comment


    • #3
      Thanks much Nick. I have a state in my dataset but it does not loop.It works till the last line of code (if inlist etc.): I get a warning "ambiguous abbreviation" (r111), I tried to add a * but then get "invalid name" (r198). Thanks again!

      Comment


      • #4
        The qualifier must appear as a qualifier; it is not a separate command.

        Comment

        Working...
        X