Announcement

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

  • How can I count the number of elements in a numlist?

    Is there a command or function that counts the number of elements in a numlist?

  • #2
    Code:
    . numlist "1 2 3 4 5 6 7 8 9 10"
    
    . local count: word count `r(numlist)'
    
    . di `count'
    10

    Comment


    • #3
      This is not a very elegant solution in the context that I am about to use it.

      The context is that a user passes to me a valid Stata numlist, lets call it Numlist. I need to write a loop that does something for 1, for 2, for 3..., to the number of elements in the numlist.

      My initial idea, not elegant and not appealing as well, was to do the loop:

      Code:
      local counter = 1
      foreach num of numlist Numlist {
      do sth with a reference to `counter'
      local counter++
      }
      What Ali proposes is another way to go, I firstly expand Numlist into a local using -numlist- command, and then I count the elements in the local as Ali has shown.

      Is there any better option here?

      Comment


      • #4
        This seems more efficient:

        Code:
        program define fakeprog
            syntax,option(numlist)
            forval x = 1/`:word count `option''{
                di `x'
            }
        end
        Code:
        . fakeprog,option(10 15 100 1230 139)
        1
        2
        3
        4
        5

        Comment


        • #5
          It sounds like you need to iterate on the index of the numlist and not necessarily the values themselves, and any solution would need to account for compression of sequences within the list. But I presume that knowing the actual value in the list will be useful in itself. I think the counting loop that you proposed in #3 is most direct and clean for your purposes. I can't think of a more concise way (without say list comprehensions of python).

          Comment


          • #6
            This worked beautifully !

            Thank you very much, Ali !

            Originally posted by Ali Atia View Post
            This seems more efficient:

            Code:
            program define fakeprog
            syntax,option(numlist)
            forval x = 1/`:word count `option''{
            di `x'
            }
            end
            Code:
            . fakeprog,option(10 15 100 1230 139)
            1
            2
            3
            4
            5

            Comment


            • #7
              Hi Joro,
              If you don't care how many elements there are, so long as each element is processed, then another approach might be to use "gettoken" and "while":
              Code:
              local numlist 10 15 100 1230 139
              local remaining : copy local numlist
              while "`remaining'"!="" {
                  gettoken next remaining : remaining
                  disp `next'
              }
              This is arguably more elegant, but has potentially fewer safeguards.


              Comment


              • #8
                Maybe
                Code:
                matrix numbers = `=subinstr("`numlist'", " ", ",", .)'
                
                forvalues i = 1/`=colsof(numbers)' {
                
                    if (`i'==1) {
                    
                        // something using numbers[1,`i'] 
                    }
                   
                    else if (`i'==2) {
                    
                        // something else 
                    }
                    
                    // ...
                }
                
                * matrix numbers still available ...

                Comment

                Working...
                X