Announcement

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

  • How to generate a local list with numbers in a simple way

    Hi all, I have a simple question. Say I want to process filenames or variable names with consecutive numbers such like "abc1xyz", "abc2xyz", "abc3xyz", "abc4xyz", ... , "abc100xyz", so I need to generate a local list of these names. Directly using local list "abc1xyz......" makes the code be too long if there are too many consecutive numbers, so I would like to ask if there is any simple way to generate local list with consecutive numbers like this? Many thanks in advance!

    PS: I know that if I want to do loop. I can use forvalues x=1/100 and if I want to keep or drop variables I can use keep abc*xyz. But still, sometimes I need the list, for example when I need to keep variables with odd numbers in this list "abc1xyz", "abc2xyz", "abc3xyz", "abc4xyz", ... , "abc100xyz".


  • #2
    Code:
    forval j = 1/100 { 
         local wanted  abc`j'xyz
         // work with that  
    }

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Code:
      forval j = 1/100 {
      local wanted abc`j'xyz
      // work with that
      }
      Thank you for your quick reply!

      I'm still a bit confusing. I try an example.
      First i generate abc1xyz to abc100xyz

      Code:
      forvalues j=1/100{
      gen abc`j'xyz=1
      }

      Then i generate list:

      Code:
      forvalues j=1/100{
      local wanted abc`j'xyz
      }

      But the list only contain abc100xyz, for example, when I try to drop all variables:

      Code:
      drop `wanted'

      Only the last one which is abc100xyz is dropped.

      I actually want to generate a list like this: local list "abc1xyz abc2xyz abc3xyz abc4xyz abc5xyz abc6xyz... ..abc98xyz abc99xyz. abc100xyz"

      Thank you in advance




      Comment


      • #4
        Originally posted by Nick Cox View Post
        Code:
        forval j = 1/100 {
        local wanted abc`j'xyz
        // work with that
        }
        And also, now I even do not know how to generate simple list like this: local list "1 2 3 4 5 6 7 8 9 10 11 12... ... 90 91 92 93 94 95 96 97 98 99 100", but directly typing this numbers makes the code be so long....

        Comment


        • #5
          Try this:

          Code:
          . forvalues i = 1/100 {
            2. local l "`l' `i'"
            3. }
          
          . dis "`l'"
           1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 
          > 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
          >  74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1 2 3 4 5 6 7 8 9 10 
          > 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
          >  46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 8
          > 0 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
          >  18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5
          > 2 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 
          > 87 88 89 90 91 92 93 94 95 96 97 98 99 100
          Originally posted by Shuoge Qian View Post

          And also, now I even do not know how to generate simple list like this: local list "1 2 3 4 5 6 7 8 9 10 11 12... ... 90 91 92 93 94 95 96 97 98 99 100", but directly typing this numbers makes the code be so long....

          Comment


          • #6
            You already know how to call up the varlist through a wildcard, as you explained in #1.

            In practice you rarely need the list otherwise, but it is naturally possible:

            Code:
            forval j = 1/100 { 
                 local list `list' abc`j'xyz 
            }
            ​​​​​​​

            Comment


            • #7
              Originally posted by Joro Kolev View Post
              Try this:

              Code:
              . forvalues i = 1/100 {
              2. local l "`l' `i'"
              3. }
              
              . dis "`l'"
              1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
              > 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
              > 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1 2 3 4 5 6 7 8 9 10
              > 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
              > 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 8
              > 0 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
              > 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5
              > 2 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
              > 87 88 89 90 91 92 93 94 95 96 97 98 99 100
              Thank you so much! I understand!

              Comment


              • #8
                Originally posted by Nick Cox View Post
                You already know how to call up the varlist through a wildcard, as you explained in #1.

                In practice you rarely need the list otherwise, but it is naturally possible:

                Code:
                forval j = 1/100 {
                local list `list' abc`j'xyz
                }
                Got it! Thx

                Comment

                Working...
                X