Let's apply formatting to #1 OUTSIDE of quotations:ĭisplay "here it is formatted " %10.2f `mpg_mean'
![stata local stata local](http://cdn.stateuniversity.com/assets/logos/images/1086/large_stata_center.jpg)
note that it's really long after the decimal Now let's call the unformatted macro from #1:ĭisplay "here it is unformatted: `mpg_mean'" Local mpg_mean_neat = strltrim("`: display %10.2f r(mean)'") this combines an equals sign and colon. here's formatting mixed with a trim command, Local mpg_mean_fmt: display %10.2f r(mean) apply formatting while generating the local generate an unformatted macro, equals sign Formatting during generation of macros is only useful for macros intended to be displayed later on.) sysuse auto, clear (Note: I strongly recommend against formatting/rounding/reducing precision of any macros that you are generating if you will later do math on them. Here’s a demo script of what I’m getting at. (Note that the “trim” command has been replaced by “strltrim” and a lot of other related commands that you can find in –help string functions–.) As a bonus, it turns out that you can also apply formatting (e.g., %4.2f) of a macro when displaying within a string using the “: display” subcommand strategically surrounded by opening and closing tick marks.
![stata local stata local](https://miro.medium.com/max/1400/1*jcqPswAsHyVF6JTA-AwRxg.png)
It turns out that not only can you apply formatting while generating a macro with the “: display” subcommand, you can also trim extra spaces from the generated macro at the same time.
STATA LOCAL HOW TO
I came across this post on the Stata List by Nick Cox, which details how to do just that: Instead, I wanted to apply the format while also generating the macro itself. That creates issues when generating string output from numerical macros, since my prior strategy of applying numerical formatting (eg %4.2f) didn’t work when displaying a numerical macro in a string (i.e., embedding it within quotations).
STATA LOCAL CODE
(I don’t like the round command because it’s tricky to code in a program for reasons I won’t get into.) Instead, I have applied the number formatting when displaying the macro. I’ve hadn’t previously been able to apply formatting (eg %4.2f) while generating macros, outside of applying the “round” command. One problem in using macros is that raw r- or e-level data are really long and not amenable to output in tables for publication without formatting.
![stata local stata local](https://www.princeton.edu/~otorres/Stata/dofile_files/image044.jpg)
(Local macros are temporary, global macros are more persistent.) This is particularly useful when generating summary statistics collected in macros after the –sum– command, or displaying a subset of components from a regression, such as the beta coefficient and 95% confidence intervals, or P-values (details on how to manipulate regression function results with macros are here). It allows you to grab information from r- or e-level data after executing a Stata command then calling that back later.