Announcement

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

  • Problem with counting words in a local

    Hi all,

    In order to be able to run a do-file on different datasets, I'm creating locals with variables' names that are as generic as possible in the following way:

    Code:
        local pen "pen_*_pc"
        local dtx "dtx_*_pc"
        local con "con_*_pc"
        local dtr "dtr_*_pc"    
        local sub "sub_*_pc"    
        local itx "itx_*_pc"    
        local edu "edu_*_pc"
        local feesedu ""
        local hlt "hlt_*_pc"
        local feeshlt ""
        local oth ""
        local feesoth ""
    
        local pen_t "pen_*_t*"
        local dtx_t "dtx_*_t*"
        local con_t "con_*_t*"
        local dtr_t "dtr_*_t*"
        local sub_t "sub_*_t*"
        local edu_t "edu_*_t*"
        local feesedu_t ""
        local hlt_t "hlt_*_t*"
        local feeshlt_t ""
        local oth_t ""
        local feesoth_t ""
    The reason for this is that the potential datasets have a common structure in labeling variables but there might difference in the middle part. Thus, I put a * to be able to include all the variables that have the corresponding structure.

    Later on in the do-file, I need to count the number of variables in each local (as an input for something else) and I've started coding the following loop:

    Code:
      
     foreach var in pen dtx con dtr sub edu feesedu hlt feeshlt oth feesoth {
        
        
        local aux= wordcount("``var''")
        di `aux'
      
        local aux2= wordcount("``var'_t'")
        di `aux2'
     
        }
    However, it's not doing what I want. I set trace on and, for instance, for the first round of the loop, this is what I get:


    - foreach var in pen dtx con dtr sub edu feesedu hlt feeshlt oth feesoth{
    - local aux= wordcount("``var''")
    = local aux= wordcount("pen_*_pc")
    - di `aux'
    = di 1
    1
    - local aux2= wordcount("``var'_t'")
    = local aux2= wordcount("pen_*_t*")
    - di `aux2'
    = di 1
    1
    - }
    So, the problem is that the way I refer to the local with the variables' names, it displays literally "pen_*_pc" instead of the list of variables with that structure (say, "pen_x1_pc pen_x2_pc"). Hence, the wordcount function only counts one variable.

    Do you know how can I get Stata to properly count the number of variables in the local??

    Many thanks in advance!

  • #2
    Perhaps this should be the code?
    Code:
    foreach var in pen dtx .....  {
        unab varlist: ``var''
        display `"``varlist''"'
        local aux= wordcount("``varlist''")
        display `aux'
    }

    Comment


    • #3
      Code:
      unab varlist : ``var''
      local aux = wordcount("`varlist'")

      Comment


      • #4
        Thank you both for your help! In the end, I coded it in the following way:

        Code:
        capture unab varlist : ``var''
        local aux= wordcount("`varlist'")
        di `aux'
        Had to add the capture because in some cases the local was empty and it was creating problems with unab.

        Comment

        Working...
        X