Announcement

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

  • Foreach Loop with if condition inside the brackets

    Hello everybody,

    I have a problem and was not able to find a solution for this.

    I want to do a loop including an if function looking like this:

    foreach var of varlist BTMCH* {
    xtile E`var' = `var' if SMVJ*==1, nq(5)

    xtile E`var' = `var' if SMVJ*==2, nq(5)
    xtile E`var' = `var' if SMVJ*==3, nq(5)
    xtile E`var' = `var' if SMVJ*==4, nq(5)
    xtile E`var' = `var' if SMVJ*==5, nq(5)
    }


    I have BTMCH2002 BTMCH2003,..., BTMCH2013 and also SMVJ2002,...,SMVJ2013

    So I want to create new variables called EBTMCH* for BTMCH2002 if SMVJ2002 = 1, for SMVJ2002=2,... and then also for BTMCH2003 if SMVJ2003=1, for SMVJ2003=2,... until BTMCH2013 if SMVJ2013=1, for SMVJ2013=2,...

    The error message is: SMVJ* is an invalid name. So the problem is how to define SMVJ* differently to be able to execute the program.

    Can you help me with this please.

    Thanks a lot for your help

    Best Max




  • #2
    Pete,

    How about something like:

    Code:
    forvalues yr=2002/2013 {
    xtile EBTMCH`yr' = BTMCH`yr' if SMVJ`yr'==1, nq(5)
    
    etc...
    }
    Regards,
    Joe

    P.S. Please take note of the Statalist preference for using full names (see the FAQ for more details).

    Comment


    • #3
      Joe,
      I will change my name.

      Thanks a lot for your fast reply.

      best Max

      Comment

      Working...
      X