Announcement

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

  • Minimum or maximum from a set contained in global macro

    Hi,

    I would like to create a local macro that would contain only the maximum value of a set contained in a global macro:

    Code:
    global M_per 6 12 24 36 72 120
    
    * I would like to input the max of $M_per, which is 120 in the local variable max_M_Per 
    local max_M_Per 120
    Can someone help?

  • #2
    . global M_per 6 12 24 36 72 120

    . local max_M_per = max(`: subinstr global M_per " " ", ", all')

    . di "`max_M_per'"
    120


    I'm not sure what you're trying to do, but it sure sounds as if you'd benefit from learning Mata.

    Comment


    • #3
      Same thing that Joseph does, but in two steps, and with a clarification: the -max- function does exactly what you want, however it operates on comma delimited lists, and you have a space delimited list in your macro:

      Code:
      . local M_per 6 12 24 36 72 120
      
      . local M_per : subinstr local M_per " " ",", all
      
      . dis "`M_per'"
      6,12,24,36,72,120
      
      . local max_M_per = max(`M_per')
      
      
      .  dis "`max_M_per'"
      120

      Comment


      • #4
        As a small exercise in making the simple complicated

        Code:
        . local M_per 6 12 120 24 36 72
        
        . mata: st_numscalar("max", max(strtoreal(tokens(st_local("M_per"))')))
        
        . scalar li max
               max =        120

        Comment


        • #5
          Is Nick's solution more general or robust than Joseph's (Nick published after Joseph)? Sorry to ask, I'm not proficient in Mata. And thanks to all!

          Comment


          • #6
            No; it is not more general or robust. I was riffing on the theme by Joseph Coveney, running that Mata can be useful too.

            The one complication I can think of is missing values, but the max() function in Stata and the max() function in Mata both ignore missing values to the extent possible.

            Comment


            • #7
              Francois can come up with other complications, Nick.
              Here is one complication he came up with on this thread
              https://www.statalist.org/forums/for...bal-macro-list
              presence of comment in the macro.

              I do not know which notion of "robustness" Francois has in mind, but the two approaches respond differently in the face of this complication:

              Code:
              . local M_per 6 12 24 36 72 120 /*240*/
              
              . local M_per : subinstr local M_per " " ",", all
              
              . dis "`M_per'"
              6,12,24,36,72,120,/*240*/
              
              . local max_M_per = max(`M_per')
              [/] not found
              r(111);
              
              . local M_per 6 12 24 36 72 120 /*240*/
              
              . mata: st_numscalar("max", max(strtoreal(tokens(st_local("M_per"))')))
              
              . scalar li max
                     max =        120
              
              
              .
              Originally posted by Nick Cox View Post
              No; it is not more general or robust. I was riffing on the theme by Joseph Coveney, running that Mata can be useful too.

              The one complication I can think of is missing values, but the max() function in Stata and the max() function in Mata both ignore missing values to the extent possible.
              Last edited by Joro Kolev; 28 Jul 2020, 11:31.

              Comment


              • #8
                Originally posted by Joro Kolev View Post
                I do not know which notion of "robustness" Francois has in mind, but the two approaches respond differently in the face of this complication
                Again, the difference is observed only when executing the code from the command line (see below).

                The other notion of robustness that maybe you're alluding to can be summarized as: given that such block commenting is not supposed to be allowed at the command line, and so is either a syntax error in that context or else it's to be taken literally, the first approach is in this sense more robust in that it gives rise to an error.

                .ÿ
                .ÿversionÿ16.1

                .ÿ
                .ÿclearÿ*

                .ÿ
                .ÿlocalÿM_perÿ6ÿ12ÿ24ÿ36ÿ72ÿ120ÿ/*240*/

                .ÿlocalÿM_perÿ:ÿsubinstrÿlocalÿM_perÿ"ÿ"ÿ",",ÿall

                .ÿdisÿ"`M_per'"
                6,12,24,36,72,120

                .ÿlocalÿmax_M_perÿ=ÿmax(`M_per')

                .ÿdisÿ"`max_M_per'"
                120

                .ÿlocalÿM_perÿ6ÿ12ÿ24ÿ36ÿ72ÿ120ÿ/*240*/

                .ÿmata:ÿst_numscalar("max",ÿmax(strtoreal(tokens(st_local("M_per"))')))

                .ÿscalarÿliÿmax
                ÿÿÿÿÿÿÿmaxÿ=ÿÿÿÿÿÿÿÿ120

                .ÿ
                .ÿexit

                endÿofÿdo-file


                .

                Comment

                Working...
                X