Announcement

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

  • Generating a varlist using loops

    Dear all,

    I have several variables with a similar structure of name (i.e., ratio_XXXXXXXXXX_co_LLL, where X and L are numbers). Let me mention that there are variables with the same XXXXXXXXXX number but different LLL, and the comment is also valid in reverse. I need to define a varlist of all these variables.
    Is it possible to define the varlist using a loop instead of doing it manually?

    Thanks!

  • #2
    No need for a loop. Take a look at the help files for 'varlist', 'unab', and 'ds' for ideas on how to build these types of lists. (also I'm not sure what the phrase " the comment is also valid in reverse " refers to but note that the asterisk wildcard does not need to know how many characters are in the XXXXX or LLL parts)

    examples:

    Code:
    unab test: ratio_*_co_*
    di `"`test'"'
    
    ds ratio_*_co_* 
    loc test2 `r(varlist)'
    di `"`test2'"'
    Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

    Comment


    • #3
      Thank you, Eric, I will follow your advice!

      Comment


      • #4
        Dear Eric and colleagues,

        I found the suggestion very useful. However, I have another related question. I would need to build a list of just all the variables with equal XXXXXXXXXX part (variables are named as ratio_XXXXXXXXXX_co_LLL, where X and L are numbers). For example, I would like to build a varlist of only those variables with ratio_11111111112_co_LLL for all LLL numbers. The problem is that I have more than 3000 categories of XXXXXXXXXX, so it is not a reasonable solution to type every ten digits individually. The ds command works great, but I do not know how to use it here (maybe I should require a previous loop?)

        Code:
        ratio_11111111112_co_001
        ratio_11111111112_co_005
        ratio_11111111112_co_007
        ratio_11111111112_co_010
        ratio_11111111112_co_015
        ratio_11111111112_co_025
        Thanks for the time to read this post.

        Comment


        • #5
          I am not sure if I understand but (e.g.)

          Code:
          ratio_?????????2_co_???
          and

          Code:
          ratio_??????????_co_???
          and

          Code:
          ratio_*_co_???

          might all be useful as wildcards.


          Comment

          Working...
          X