Announcement

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

  • Combination of foreach & forvalues

    Hi All,

    I have a series of variables like x1, x2, x3,...x9 and y1,y2,y3, ..y9. How can I produce tabulation for x1 by y1, x2 by y2, x3 by y3,....x9 by y9 using stata foreach and forvalues codes together? Many thanks inadvance. Ghasem

  • #2
    That needs just one loop, say

    Code:
    forval j = 1/9 { 
        tab x`j' y`j' 
    }
    There is a foreach solution too.

    Comment


    • #3
      Thank you so much Nick, may I ask what would be code for "foreach", please?

      Comment


      • #4
        That's set as an exercise.

        The key here is that the variable elements in the loop are the suffixes 1(1)9.

        The prefixes x y are constant.

        You could loop over 1(1)9 using foreach, but it's not necessary or even an especially good idea.
        Last edited by Nick Cox; 29 Jul 2024, 10:17.

        Comment


        • #5
          Many thanks again.

          Comment

          Working...
          X