Announcement

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

  • How to use IF condition before running a inner loop of FORV

    I am struggling with imposing if condition on a inner loop. Consider the following example where a user can input five dependent variables with names S1 S2 S3 S4 S5 or S1H1 S1H2 S1H3 S1H4 S1H5,
    Code:
        local LHS : word 1 of `varlist'
        local S = substr("`LHS'",1,1)
        local H = substr("`LHS'",3,1)
        local NF: word count `varlist'
    
    
    forv s=1/`NF'{
      *IF condition is required here
          forv h=1/`NF'{
                reg `S'`s'`H'`h' `flist'
          }
    }
    let say that the user inputs the first series of dependent variables i.e. S1 S2 S3 S4 S5, then I just need the higher loop i.e.
    Code:
     forv s=1/`NF'{
    However, if the user inputs the second series i.e., S1H1 S1H2 S1H3 S1H4 S1H5, then I need both the outer and inner loops. i.e.
    Code:
    forv s=1/`NF'{
          forv h=1/`NF'{
                reg `S'`s'`H'`h' `flist'
          }
    }
    Last edited by Attaullah Shah; 27 Aug 2015, 12:22.
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

  • #2
    I think something like this will do, making use of the "programmer's if" documented in [P] Stata Programming Reference Manual rather than the "if clause" documented as part of the Stata command syntax.
    Code:
        local LHS : word 1 of `varlist'
        local S = substr("`LHS'",1,1)
        local H = substr("`LHS'",3,1)
        local NF: word count `varlist'
    
    
    forv s=1/`NF'{
      if "`H'" != "" {
        forv h=1/`NF'{
          reg `S'`s'`H'`h' `flist'
        }
      }
      else {
        reg `S'`s' `flist'
      }
    }
    Last edited by William Lisowski; 27 Aug 2015, 12:39.

    Comment

    Working...
    X