Announcement

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

  • Loop with alphabet

    Dear Stata-users,

    I have a short question, namely how do I make a loop out of the following:

    local z1 = "a"
    local z2 = "b"
    local z3 = "d"
    local z4 = "e"
    local z5 = "f"
    ...
    z26 = "z"


    Your help is very much appreciated!
    Christian




  • #2
    Here are a couple of techniques depending on whether it's more convenient to work with "abcd" or "a b c d".
    Check out -help string functions- to learn more.
    Code:
    local alpha = c(alpha) // built in "regular" english letters
    // OR Your own local alpha = " a ä b ß c d e f g h i j k l m"  
    //
    di "working with: `alpha'"
    //
    local nchar: word count `alpha'
    di "`nchar'"
    //
    forval i = 1/`nchar' {
       local z`i' = word("`alpha'", `i')
    }
    di "`z10'"
    //
    // Or if spaces are inconvenient
    local alpha = "abcdefghijklmnopqrstuvxyz"
    local nchar = strlen("`alpha'")
    forval i = 1/`nchar' {
       local z`i' = substr("`alpha'", `i', 1)
    }
    di "`z10'"

    Comment


    • #3
      Thank you Mike!

      Comment

      Working...
      X