Announcement

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

  • Incrementing a local macro alphabetically

    You can bump a numeric value up by 1 with code like

    local x = `x' + 1

    How about alphabetically? So, for example, if local x = a, if I increment it it becomes b, then c, etc. I can think of convoluted ways to do this but I prefer something simple.
    -------------------------------------------
    Richard Williams, Notre Dame Dept of Sociology
    Stata Version: 17.0 MP (2 processor)

    EMAIL: [email protected]
    WWW: https://www3.nd.edu/~rwilliam

  • #2
    I don't think there's anything really neat for doing this. As you well know, Stata does not treat characters as integers, and there is no ascii() function that returns the ascii code of a character. One way to proceed, if you're just stepping through the lower case alphabet is:

    Code:
    local x  a
    display `"`x'"'
    
    forvalues i = 1/25 {
        local x = char(ceil(strpos("`c(alpha)'", "`x'")/2) + 97)
        display `"`x'"'
    }
    But after z it takes you into (, and from there back to a again. Probably simpler, and probably you've already considered it, is to just increment a numerical counter i and then -local x: word `i' of `c(alpha)'-

    Comment


    • #3
      I'd go with Clyde's suggestion in most contexts, but add that Mata does have an ascii() function even if Stata doesn't. (No idea why not.)

      Also, consider uchar() as below:

      Code:
      . mata: ascii("a")
        97
      
      . mata: ascii("Stata")
               1     2     3     4     5
          +-------------------------------+
        1 |   83   116    97   116    97  |
          +-------------------------------+
      
      . mata: uchar(97)
        a
      
      . di uchar(97)
      a
      
      forval w = 1/26 {
          uchar(96 + `w')
      }
      
      a
      b
      c
      ...
      Code:
      
      

      Comment


      • #4
        Thanks. I also came up with this:

        local rtag `=char(`bnum' + 96)'

        where bnum is the counter. I don't think bnum will go higher than 26.
        -------------------------------------------
        Richard Williams, Notre Dame Dept of Sociology
        Stata Version: 17.0 MP (2 processor)

        EMAIL: [email protected]
        WWW: https://www3.nd.edu/~rwilliam

        Comment


        • #5
          I'm a bit late to the dance, but unless I have underestimated Clyde's code (always possible!) the following seems to simplify his approach and extend it to an upper limit of 52 characters.
          Code:
          forvalues i = 1/52 {
              local x : word `i' of `c(alpha)' `c(ALPHA)'
              display `"`x'"'
          }

          Comment


          • #6
            Another Mata way of doing this is numtobase26()

            Code:
            mata
            for(i=20; i < 30; i++) {
                numtobase26(i)
            }
            end
            ---------------------------------
            Maarten L. Buis
            University of Konstanz
            Department of history and sociology
            box 40
            78457 Konstanz
            Germany
            http://www.maartenbuis.nl
            ---------------------------------

            Comment

            Working...
            X