Announcement

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

  • Counting the number of variables with same stub

    Hello,
    I am trying to count the number of variables that use the same stub and verify that there are at least two such variables.
    I want to do something like:
    ds stub*
    I know this will then return the the number of variables so named in r(varlist).
    Can anyone tell me how I can then check if there are at least two elements in the list?
    Or perhaps there is a better way of doing this?
    Regards,
    John L.

  • #2
    Here's two ways, the first is preferred if you do not have Stata 13

    Code:
    sysuse auto, clear
    
    ds m*
    
    local nwords :  word count `r(varlist)'
    
    display `nwords'
    
    display wordcount("`r(varlist)'")

    Comment


    • #3
      -wordcount()- is also available in Stata 12.1. So I wonder if the statement

      Here's two ways, the first is preferred if you do not have Stata 13
      was made based on the belief that it's not available before Stata 13, or rather there is some other, underlying reason.
      You should:

      1. Read the FAQ carefully.

      2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

      3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

      4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

      Comment


      • #4
        Indeed, I wasn't very clear. Before Stata 13, the limit on Stata strings was 244 characters. So using any string function to manipulate a macro could yield unexpected results. Here's a quick example in Stata 9:

        Code:
        . forvalues i = 1/30 {
          2.         local vlist `vlist' a_rather_long_variable_name`i'
          3. }
        
        . 
        . local nwords :  word count `vlist'
        
        . 
        . display `nwords'
        30
        
        . 
        . display wordcount("`vlist'")
        9
        The same code works well in Stata 13:

        Code:
        . forvalues i = 1/30 {
          2.         local vlist `vlist' a_rather_long_variable_name`i'
          3. }
        
        . 
        . local nwords :  word count `vlist'
        
        . 
        . display `nwords'
        30
        
        . 
        . display wordcount("`vlist'")
        30
        So if you are using Stata 12 or earlier, you should always use the macro extended functions (see help extended_fcn) to manipulate macros.

        Comment


        • #5
          Excellent Robert.

          Thanks for the details!
          You should:

          1. Read the FAQ carefully.

          2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

          3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

          4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

          Comment


          • #6
            I think I have a similar questions. I receive data in excel spreadsheets from different community based organizations. My job is to verify that that the data they submit are clean. One of the things I need to do is to quickly count the number of variables. If the datasets have fewer or more variable than expected I will have to let them know. There are supposed to have 30 variable in their datasets. Is there a way to quickly count variables and if they are != 30 variables? Thank you
            Marvin

            Comment


            • #7
              The number of variables in the dataset is built in as c(k)

              Code:
              . sysuse auto, clear
              (1978 Automobile Data)
              
              . di c(k)
              12

              Comment


              • #8
                Thank you Nick!

                Comment


                • #9
                  Is it possible to use the number of stubs we have counted as the limit in the forvalues command? For example, using the stub counting method presented in #2 above
                  Code:
                  sysuse auto, clear
                  ds m*
                  local nwords : word count `r(varlist)'
                  display `nwords'
                  followed by a forvalues command which uses the stub count - this one isn't correct but intended to present the idea
                  Code:
                  forvalues i = 1/`nwords' {
                   }
                  Thank you, Dan

                  Comment


                  • #10
                    This question is also posted, with a reply, at the following topic.

                    https://www.statalist.org/forums/for...ch-observation

                    Comment

                    Working...
                    X