Announcement

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

  • Stata 17 : Loop with a "or" condition (so-called "I" in Stata)

    Dear Statalist Users,


    I am trying to create a loop with all this code, but I was wondering if there was a simpler method, with a loop in particular. Here is my rudimentary code:

    Code:
    replace ansbef_1=. if ansbef_1==-9 | ansbef_1==-1
    replace ansbef_2=. if ansbef_2==-9 | ansbef_2==-1
    replace ansbef_3=. if ansbef_3==-9 | ansbef_3==-1
    replace ansbef_4=. if ansbef_4==-9 | ansbef_4==-1
    replace ansbef_5=. if ansbef_5==-9 | ansbef_5==-1
    replace ansbef_6=. if ansbef_6==-9 | ansbef_6==-1
    replace ansbef_7=. if ansbef_7==-9 | ansbef_7==-1
    replace ansbef_8=. if ansbef_8==-9 | ansbef_8==-1
    replace ansbef_9=. if ansbef_9==-9 | ansbef_9==-1
    replace ansbef_10=. if ansbef_10==-9 | ansbef_10==-1


    A big thank you for the one who answers me. I've been struggling with this for a while!

    Best Regards,

    --
    Michael Duarte Gonçalves

  • #2
    You don't need a loop for this.

    Code:
    recode ansbef_1-ansbef_10 (-1 -9 = .)
    assuming these variables are one after the other in order. Otherwise you could use some wildcard (something like ansbef_*), or in the worst case, list out all the variables in the same command.
    Last edited by Hemanshu Kumar; 03 Nov 2022, 07:58.

    Comment


    • #3
      Thank you so much Kumar!

      Michael

      Comment


      • #4
        Thank you so much Kumar!


        Michael

        Comment

        Working...
        X