Announcement

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

  • Create numlist based on values?


    I am currently struggling a bit with Stata syntax. As I am using the "margins" command, I want to specify an option. The following is from the "help margins":

    at(age=(20(10)50)) does the same as at(age=(20 30 40 50)); that is,
    you may specify a numlist.
    If I do not want to hardcode the numeric values into the numlist, but want to have it dynamic, how can I do that? So for example, I want the numlist to go from min(age) to max(age) in 10 even steps. Or alternatively, I want to go from one standard deviation below mean to one standard deviation above mean in 3 even steps.

    I feel like I probably cannot do this within the parenthesis of margins, but even when trying to construct a numlist prior to the margins account with these values, it seems that I am constantly failing at the syntax. "help numlist" isn't that helpful for me, as it only gives hardcoded numbers.

  • #2
    Code:
    sysuse auto, clear
    
    //  NUMLIST WITH 10 EVEN STEPS FROM MIN TO MAX
    summ mpg, meanonly
    
    local delta = (r(max) - r(min))/9
    local atspec mpg = (`r(min)'(`delta)')`r(max)')
    
    regress price mpg
    margins, at(`atspec')
    
    //  NUMLIST FROM -1 SD TO +1 SD IN 3 EVEN STEPS
    summ mpg
    local atspec mpg = (`=`r(mean)'-`r(sd)'' `r(mean)' `=`r(mean)'+`r(sd)'')
    
    margins, at(`atspec')
    The source material you need to read for this is not about -numlist-s, it's about -macro-s.

    Comment


    • #3
      Thanks for the example and code snippet! This entire thing with ` and ' is completely new to me, will read up on that in the source material

      Comment

      Working...
      X