Announcement

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

  • How to count the number of variables inside a local varlist

    Dear all,

    I have created a local varlist that contains several variables. Do you know how can I count the variables inside it and save the number in a scalar?

    Thanks.

  • #2
    Code:
    scalar var_count = `:word count `my_local_macro''
    Substitute the desired name of the scalar and the actual name of your local macro for the italicized portions.

    Comment


    • #3
      Thank you Clyde. Always your comments are very helpful.

      Comment


      • #4
        Hi all,

        I had the same question as Daniel's but with an additional complication that makes Clyde's answer not work.

        So, I have a list of variables with a similar structure: y_a_x, y_b_x, y_c_x, etc. Basically, what changes is the character in the middle, and since I'm working with several datasets and the characters change from one to another I've saved the list of variables in the following way to keep it general:


        local y "y_*_x"
        I want to count how many variables are saved in my local y but Stata keeps reading "y_*_x" as one word and so when I run:

        scalar var_count = `:word count `y''
        di var_count
        the answer is 1, even if there are no variables at all. So, I was wondering if you knew an alternative way of counting the number of variables in my local when I use the "*" character.

        Thanks.
        Last edited by Caterina Brest Lopez; 23 Feb 2023, 14:10.

        Comment


        • #5
          My understanding here is that Caterina is using the macro y to hold a wildcard descriptor of a varlist. In that case, one approach is:
          Code:
          ds `y'
          local varcount: word count `r(varlist)'

          Comment


          • #6
            describe, varlist will now also leave r(varlist) in its wake.

            See yet further unab which unpacks wildcards.

            (The answer to how many variable names are inside my local macro is in one sense none. Defining and referencing a local macro is just about supplying and demanding text. The macro machinery has such has no sense of what the text means. But that is not a problem.)

            Comment


            • #7
              Many thanks, Mike and Nick! Both of you are answers were really helpful! Just an addition to Mike's code: to avoid running into an error when y is empty and I want the local varcount to be 0 in that case, it's just enough to add a capture before ds


              cap ds `y'

              local varcount: word count `r(varlist)'

              Comment


              • #8
                #7 doesn't look quite right. If local macro y is empty ds `y' is equivalent to plain ds andreturns the complete varlist -- which will be empty if and only if there are no variables in memory. capture makes no difference to that

                Comment


                • #9
                  I think I explained myself wrong. Thanks, Nick, for pointing it out.
                  What I meant was that local macro y was empty in the sense that for a particular dataset, there were no variables consistent with the wildcard descriptor. However, the local y still contains the text "y_*_x". So, what happened to me in that case when I wrote the following code (without the capture):


                  local y "y_*_x"
                  ds `y'

                  local varcount: word count `r(varlist)'
                  was that I got the following error:

                  variable y_*_x not found
                  after line
                  ds `y'
                  . I understand that this happens because local macro y is not really empty and Stata runs the line "ds y_*_x" but since there are no such variables it runs into an error. By adding capture before ds, Stata keeps running, r(varlist) is empty, and local macro varcount is equal to 0 (which for the purposes of my exercise is what I needed).

                  I hope is clearer now what I meant.

                  Comment


                  • #10
                    Hi, I want to count the number of variables in a global varlist. For this the above command does not work. So I used

                    global mylist2 a b c
                    local locmylist2 $mylist2
                    scalar var_count = `:word count `locmylist2''
                    di var_count

                    Which works, however, I have to set up an additional local varlist. Can I directly count the variablesn in a global varlist?

                    Comment


                    • #11
                      Here are various more direct ways to answer #10. There is no need to copy a global macro to a local macro.

                      Below I've edited the output of macro list to remove irrelevant details.

                      Whether a global macro is a good idea in the first place is a different question!



                      Code:
                      . global mylist2 a b c
                      
                      . di wordcount("$mylist2")
                      3
                      
                      . local nw1 = wordcount("$mylist2")
                      
                      . local nw2 : list sizeof global(mylist2)
                      
                      . macro list
                      mylist2:        a b c
                      
                      _nw2:           3
                      _nw1:           3
                      EDIT: Let me repeat a simple point that is I suspect not always understood and indeed it's subtle and easy to misunderstand. All the code does is count words inside a string. Other than being bound by quotation marks, words in Stata are just defined by being separated by spaces.

                      The wordcount() or wordcount machinery has no sense whatever that the words it is counting are variable names. So, for example, a macro containing

                      Code:
                      a* b c
                      is still going to be counted by the code here as 3 words, regardless of the fact that in a varlist (a list of variable names) a* might well be a wildcard that would expand to two or more variable names. But as said earlier in the thread unab is able to help there.
                      Last edited by Nick Cox; 27 Jun 2024, 04:15.

                      Comment

                      Working...
                      X