Announcement

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

  • local variable definition based of last character of string variable being odd or even

    Hi,

    I am trying to define a local variable based on the last character of global variable (M_crisis_* or M*) being an odd or even number:

    Code:
    local M_benchmark $M_crisis_5
    *local M_benchmark $M4
    *local M_benchmark $M_crisis_7
    
    *** What I am trying to do:
    * if the last character of {$M_crisis_5 , $M4, $M_crisis_7} if an odd number, in this case 5 or 7, then:
    * local t = odd + 2   (in this case: 5+2 or 7+2)
    * if the last character of {$M_crisis_5 , $M4, $M_crisis_7) if an even number, in this case 4, then:
    * local t = even + 1  (in this case 4+1)
    Also, I need global variable in my case. I am not seeking to replace global by local variables in the solution I am looking for. Thanks a lot in advance.

    Can anyone help? Thanks.



  • #2
    To do what you want to do for the last character of a local macro:

    Code:
    . local M_benchmark M_crisis_5
    
    . dis mod(real(substr("`M_benchmark'",-1,1)),2)
    1
    
    . local M_benchmark M_crisis_2
    
    . dis mod(real(substr("`M_benchmark'",-1,1)),2)
    0

    Comment


    • #3
      M_crisis_5 itself is a list of variables though. Something like:
      global M_crisis_5 "LogSize DY MOM36 retvol"

      Any idea?

      Comment


      • #4
        You need to distinguish between the name of the macro and the value. Your title for this topic should be "local variable definition based of last character of global macro name being odd or even"

        Code:
        . global M_crisis_5 "LogSize DY MOM36 retvol"
        
        . local M_benchmark_name M_crisis_5
        
        . local M_benchmark $`M_benchmark_name'
        
        . macro list _M_benchmark_name _M_benchmark
        _M_benchmark_name:
                        M_crisis_5
        _M_benchmark:   LogSize DY MOM36 retvol
        
        . display mod(real(substr("`M_benchmark_name'",-1,1)),2)
        1

        Comment


        • #5
          It's difficult to follow what you want here, although it seems to involve jumping between one set of variable names held in one global macro and another set of variable names held in another.

          The last character of a global macro name is just the last character of a string that is the name:

          Code:
          . di substr("M_crisis_5", -1, 1)
          5
          
          . local last = substr("M_crisis_5", -1, 1)
          
          . di `last'
          5
          The other technique you ask for is bumping up to the next higher odd integer, which is adding 2 to an odd integer and 1 to an even integer. Here is one way to do it:


          Code:
          . local in = 5
          
          . local out = cond(mod(`in', 2), `in' + 2, `in' + 1)
          
          . di `out'
          7
          
          . local in = 6
          
          . local out = cond(mod(`in', 2), `in' + 2, `in' + 1)
          
          . di `out'
          7
          noting that mod(`in', 2) evaluates to 1 or 0 precisely when mod(`in', 2) == 1 evaluates to 1 or 0.

          On terminology what was said at
          https://www.stata.com/statalist/arch.../msg01258.html holds true with the additional warning that "global variable" now has quite a different meaning in Mata.

          EDIT William Lisowski made the same first point.
          Last edited by Nick Cox; 01 Aug 2020, 05:38.

          Comment


          • #6
            Thanks all.

            Many thanks to William who understood exactly what I wanted, your answer solved my problem!

            Comment

            Working...
            X