Announcement

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

  • Nested loop syntax error

    Hi All,

    I am trying to create a loop in stata that looks like the following-

    foreach m of mnf_num {
    foreach n of num_date {
    drop if first_pos_qtr<n-4 & last_pos_qtr>n
    tab brgroups
    tab bigclass_num
    tab formgroups
    }
    }

    But I keep getting an invalid syntax error. Any edits I could make to the code so that this works? Thanks so much!

  • #2
    this is quite confusing: (1) each of your two -foreach- commands includes only one element so there is no reason to use -foreach-; (2) you never use `m'; (3) you never use `n' (though maybe your reference to n is supposed to be `n'?)

    please read the FAQ which will tell you, among other things, how to post using CODE delimiters and also gives advice on asking questions

    Comment


    • #3
      1) Are mnf_num and num_date variables or local macros? If they are variables and you want to reference their values, you need to use a macro to hold those values (possibly through -levelsof-) or use a numlist. So your loop should begin with:
      Code:
      foreach m of local mnf_num {
      or
      Code:
      foreach m of numlist 1 2 3 4 {
      2) You need to reference your macros with appropriate "adornments" within the loops:
      Code:
      drop if first_pos_qtr<n-4 & last_pos_qtr>n
      should be
      Code:
      drop if first_pos_qtr<`n'-4 & last_pos_qtr>`n'
      3) There is no reference to `m' in the code you sent, so I'm not sure if you need that loop.
      Stata/MP 14.1 (64-bit x86-64)
      Revision 19 May 2016
      Win 8.1

      Comment

      Working...
      X