Announcement

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

  • Feeding an unknown number of variables into the max function

    Hi All,

    I am currently writing a program and as part of the program I will need to find the maximum value of an unknown number of variables. I was hoping to feed the variable list into a local macro and the put the local macro into the max function however I have hit a few walls with this. My current issues is that I have managed to create a macro with the required text but it doesn't pick the variables up as variable names but rather as a long string. I have attached the code I have so far below (using the example of when var is 3, but in the main program var will be an unknown number):

    Code:
        gen total = _N
    
        local totlist ".,"
        
          /* Create var count */
          
          local var 3
          foreach i of numlist 1/`var' {
              gen T`i' = 1 if rando == `i'
              gen T`i'count = sum(T`i' != .)
              bysort T`i'count: gen A`i' = _n
              replace A`i' = . if T`i'count == 0
                  
            if `i' != `var' {
                local totlist `totlist' "A`i', "
                di `totlist'
             }
              
             else if `i' == `var' {
                local totlist `totlist' "A`i'"
                di `totlist'
             }
          }
          
          local fullist "`totlist'"
          local fullist : subinstr local fullist ".," "", word
          di `fullist'
    
          gen maximum = max(`fullist')
    This code causes an error as instead of passing through the numbers contained in the variables A1 A2 and A3 it passes in the string "A1, A2, A3". However if I use the code:

    Code:
                local fullist "A1, A2, A3"
    
                gen maximum = max(`fullist')
    Then this will give me the code that I am looking for.

    Thank you in advance for any advice!!

    Very best wishes,
    Cydney

  • #2
    I'm not exactly sure where your code is going wrong. It is complicated and hard to follow, where as a simpler, transparent approach will achieve your goal:

    Code:
    sysuse auto, clear
    
    ds price-length
    local vlist `r(varlist)'
    
    local vlist: subinstr local vlist " " ", ", all
    
    gen wanted = max(`vlist')
    illustrates the approach. Note that the original local macro vlist can be set in any way you like. And it is not necessary to know, or ever count, the number of variables in it.

    Comment


    • #3
      Hi Clyde,

      Sorry for the delay in replying I ended up putting this piece of work down for a little bit.
      Your solution is wonderful except for the fact I do not want to find the minimum value of all variables in the dataset but a subset. I know that the r(varlist) will contain all dataset variables, is there a way to make this a subset of the variables?

      Best wishes,
      Cydney

      Comment


      • #4
        So, feed to ds the variables you want to use. In fact in the auto data too, price-length is not all variables, but just a subset. r(varlist) is only a list of all variable names if that is what you ask for.

        You have changed the question to wanting the minimum, but the answer is the same other than which function you use.

        Comment


        • #5
          Sorry Clyde I completely missed how that worked yes Nick you're right that is perfect thank you very much!

          Best wishes,
          Cydney

          Comment

          Working...
          X