Hi,

I have a simple program that estimates that regresses an outcome on (a) event-time dummies (i.e. dummies for time relative to year in which a particular event happens), (b) age dummies and (c) year dummies. I then estimate margins at the unique values of the event-time variable (e.g., 1 period before event, 2 periods before event,...) that abstract from the effect of the event (i.e., I set the event-dummies to zero; put differently, I consider the effect at the value of the constant). Here is the program:


Code:
    cap program drop event_study
    program define event_study, rclass    

        *** Syntax
            syntax [varlist] [if], [                 ///
                outcome(string)                  ///
                time(string)                          ///
                baselevel(string)                ///                
                age_F(string)                     ///
                years(string)]                                    
                
        *** Regression
            reg     `outcome'                ///
                    ib`baselevel'.`time'    ///
                    i.`age_F'                    ///
                    i.`years', r
                    
        *** Margins
             margins, over(`time') at(`time' = `baselevel') nose
    end
The code works well. However, the margins command is incredibly slow. Is this because I use factor variables? Is there an alternative way to estimate the margins faster? Would you do it by hand? I am not sure how to do it in a general form (the program allows for a flexible set of covariates).

Thank you so much for your help!