Announcement

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

  • Creating a counter with alphabets

    Hi there,

    We often use a numeric 'counter' in loops. Example code:

    local i = 1
    foreach .....{
    ....`i'...
    local i = `i' + 1
    }

    How could I do it using alphabets? That is, I want to go along the sequence 'a b c ....'.

    Thanks in advance for your kind response.

  • #2
    Code:
    foreach x in `c(alpha)' {
        di "`x'"
    }
    See the output of

    Code:
    creturn list
    for other sequences stored in Stata. (Scroll to the end of the output.)

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Code:
      foreach x in `c(alpha)' {
      di "`x'"
      }
      See the output of

      Code:
      creturn list
      for other sequences stored in Stata. (Scroll to the end of the output.)
      Hi Nick,

      Thanks so much. However, I'm not actually looking to loop over them. The relationship isn't one-to-many but one-to-one. To illustrate:

      X-1-a
      X-2-b
      X-3-c
      Y-1-a
      Y-2-b
      Y-3-c

      So, I'm essentially looping over {X,Y} and then {1,2,3}, but thereafter want to form a one-to-one relationship b/w {1,2,3} and {a,b,c}; hence, wanted to use an alphabetic 'counter'. Does it make sense? :S

      Comment


      • #4
        I am reluctant to guess at how to modify concrete code as you don't give any. But schematically this may help:

        Code:
        tokenize "a b c" 
        foreach v in X Y { 
              di "`v'" 
              forval j = 1/3 { 
                    di "`i'" 
                    di "``i'''" 
             }
        }

        Comment


        • #5
          Code:
          tokenize `c(alpha)'
          forvalues i = 1/26 {
              di "letter `i' is ``i''"
          }
          ---------------------------------
          Maarten L. Buis
          University of Konstanz
          Department of history and sociology
          box 40
          78457 Konstanz
          Germany
          http://www.maartenbuis.nl
          ---------------------------------

          Comment

          Working...
          X