Announcement

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

  • Variable format not passing through local macro in .ado file

    I've written a function that, among other things, plots a date fixed effect coefficient's value across the date. I intend for this function to be agnostic to date format, so I've written it to extract the date variable's format, then specify that format in the plotting code. Here is the code I use:
    HTML Code:
        syntax varlist(max=1) [using/] [,                    ///
            MATname(string)        /// Name of matrix to save results in; default=M
            INDEXname(string)    /// Name of the index to be generated
            noBOUNDS            /// Optionally suppress upper/lower bounds
            noPLOT                 /// If specified, no plot will be generated    
            PTitle(string)        /// Title of plot in price index plot
            PSave(string)        /// Option to save plot, supplying name and filepath
            PTICKs(real 0)        /// Option to specify ticks in the plot
            ]
    ...
            // determine date variable type
            loc dtype `: format `varlist''
            loc odate : di `dtype' `odate'
            
            // if specified, set axis labels
            if (`pticks' != 0){
                qui sum `varlist' if ~missing(`indexname')
                loc xlab `=r(min)'(`pticks')`r(max)'
            }
            
            // generate plot
            //di "`dtype'"
            line `indexname' `varlist' if ~missing(`varlist'),                         ///
                xlab(`xlab', format(`dtype') angle(45)) xtitle("")             ///
                ylab(, angle(horizontal)) graphregion(color(white))                    ///
                note("Omitted date: `odate'")                                        ///
                ytitle("") title(`ptitle') name(`depvar', replace)        
    This code works and generates a plot with the data set I'm using, for which `varlist' is a monthly date variable in %tm format. However, the plot displays the x-axis labels as numbers, rather than in %tm. I tested the function with "%tm" in the place of `dtype' and it works. I've also double-checked that the `varlist' variable is in %tm format with a
    HTML Code:
    display "`: format `varlist''"
    command inside the function. It prints "%tm".

    Is it possible to use this approach within a .ado file? If so, what am I doing wrong here? I've tried using quotes around the relevant macros, and that doesn't work, either.
    Last edited by Danny Gold; 30 Dec 2021, 15:42.

  • #2
    The code you show looks fine and does what it should on my end -- I can't see any reason why it would produce a plot which displays the x-axis label without the %tm format.

    With that being said, you don't actually need to extract the variable's format in a local and transfer it to xlabel() to achieve your desired effect. The default behavior of xlabel() is to use the variable's existing format. The format() option is only useful if you wish to display a different format on the label than that of the variable itself.

    I would suggest simply removing the format() option altogether.

    Comment

    Working...
    X