Announcement

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

  • How to loop over nonconsecutive numbers

    Say that I have numbers that I would like to loop over, but these numbers are nonconsecutive. What is the best way to loop over them?

    I can, of course, list them individually:

    Code:
    foreach i in 1 3 5 6 9 10 12 16 18 19 20 21 23 24 25 26 27 28 29 30 32 33 34 35 36 37 38 39 40 41 42 45 46 47 {
        di "`i'"
    }
    I would prefer to include ranges, though, especially if I have hundreds (or, hypothetically thousands) of numbers. In pseudo code, I want to do something like this (borrowing some R syntax):

    Code:
    foreach i in c(1, 3, 5:6, 9:10, 12, 16, 18:21, 23:30, 32:42, 45:47) {
        di "`i'"
    }
    In the Stata documentation for forvalues, it says that this is allowed:

    Code:
    forvalues k = 5 10 to 300 {
        summarize x`k'
    }
    I tried a modified version, but I get an error:

    Code:
    forvalues i = 1 3 5 6 9 10 12 16 18 to 21 23 to 30 32 to 42 45 to 47 {
        di "`i'"
    }
    What should I do?

  • #2
    Code:
    foreach i of numlist 1 3 5/6 9/10 12 16 18/21 23/30 32/42 45/47 {
        di "`i'"
    }

    Comment


    • #3
      Sorry I missed that. Thanks!

      Comment


      • #4
        it appears that you can mix and match with a numlist; so you would use
        Code:
        foreach ln of numlist 1(2)5 6 9 10 12 16 18(1)21 23(1)30 32(1)42 45(1)47 {
        whatever you want
        }
        you can change "ln" whatever you want to use

        see
        Code:
        help numlist
        edit: crossed with the above

        Comment


        • #5
          To Clyde Schechter's direct and helpful answer I want to add a simple question. Where do these numbers (typically) come from any way? That applies particularly to the larger question of how to handle hundreds or thousands of such, which you wouldn't want to handle in the same way.

          https://www.stata.com/support/faqs/d...-with-foreach/ may be helpful here.

          If the distinct integers are levels of a variable, you can scoop them up with levelsof.

          If the distinct integers are levels of a variable, then similarly mapping them to a new variable with values integers 1 up may tidy things up. egen, group() is the workhorse here: note its label() option.

          More detail at the linked FAQ.
          Last edited by Nick Cox; 03 Mar 2025, 14:05.

          Comment


          • #6
            #4 Rich Goldstein - thank you!

            #5 Nick Cox - I don't remember encountering this issue before. But in my current case, I have an Excel file that contains 145 sheets, but I only want to import about 110 of them. I am doing so as follows (omitting code from the second loop):

            Code:
            import excel using "${dir}/FILENAME.xlsx", describe
            return list
            
            local N = r(N_worksheet)
            forvalues i=1/`N' {
                local sheet`i' "`r(worksheet_`i')'"
            }
            
            forvalues i=1/`N' {
                di "`i'"
                di "`sheet`i''"
                import excel using "${dir}/FILENAME", sheet(`"`sheet`i''"') cellrange(B3) firstrow allstring clear    
            }
            And then I was just trying how to modify the second loop. Of course, in the loop, I could create a variable called n that takes value `i', and then after importing all sheets, use drop if inlist(n, Z), where Z is the list of numbers corresponding to the data from the sheets I want to exclude. The downside is that it takes some time to import the sheets, so it saves time to not have to load them in the first place.

            It is very possible that there are better approaches.
            Last edited by Todd Jones; 03 Mar 2025, 14:15.

            Comment


            • #7
              #6 gives good context. I hope it's fair to say that if your subset is defined in a simple systematic way there should be a way to implement that in Stata code (or any other code). Borges' classification of animals https://en.wikipedia.org/wiki/Celest...lent_Knowledge is an example of a coder's nightmare.

              Comment


              • #8
                Borges' classification of animals is fascinating!

                Comment

                Working...
                X