Use variable labels in putdocx text - stata

I am trying to create a word document with a number of histograms. I would like to describe each histogram with text using the variable label. I know that putdocx allows you to use functions and expressions from the documentation. I looked at the documentation for functions and expressions. It only has a documentation about label values (13.11). But I also know that I can extract the variable label using the local approach. So, I am wondering if it is possible to use variable label in putdocx text.
use https://www.stata-press.com/data/r17/nlsw88.dta
histogram age
graph export "path/histogram.age.png", replace as(png)
** Using local to store the variable label
local age_lab: variable label age
di "`age_lab'"
putdocx begin
putdocx paragraph, style(Heading1)
putdocx text ("Histogram of `age_lab'")
putdocx image ("histogram.age.png")
putdocx save histogramreport, replace
Crossposted on Stata Forum.

Answer from Stata Form:
Use : var lab age instead of = age_lab in putdocx.
use https://www.stata-press.com/data/r17/nlsw88.dta
histogram age
graph export "path/histogram.age.png", replace as(png)
putdocx begin
putdocx paragraph, style(Heading1)
putdocx text ("Histogram of `: var lab age'")
putdocx image ("histogram.age.png")
putdocx save histogramreport, replace

Related

Extracting the 'end' of a string, conditions or regular expression?

I have a data table - can be extracted to text or spreadsheet - The column has random text with areas in square metres that I want to copy to a new column in hectares. (So parse text and divide by 10,000).
e.g.
Deposited Plan 172499, 53,310 m2
Deposited Plan 166167, 853 m2
This plan has no area stated
Section 21 Block I Wellington District, 403,573 m2
Output column should have:
5.3310
0.0853
40.3573
Is there a way I can automate this in LibreOffice Calc, or with a regular expression editor like TextCrawler? Or perhaps using an AutoIt script?
Try with this
/([0-9]+,*[0-9]+\sm2)$/

Scatter plot color by variable

I want to make an scatter plot in Stata with points colored according to a categorical variable.
The only way I've found to do this, is to code colors in layers of a twoway plot.
However, this seems a rather convoluted solution for such a simple operation:
twoway (scatter latitud longitud if nougrups4 ==1, mcolor(black)) ///
(scatter latitud longitud if nougrups4 ==2, mcolor(blue)) ///
(scatter latitud longitud if nougrups4 ==3, mcolor(red)) ///
(scatter latitud longitud if nougrups4 ==4, mcolor(green))
Is there a simpler and automatic way to do this?
In this case, the categorical variable nougrups4 came from a cluster analysis. A general solution would be fine, but also a specific solution to draw clusters.
This is how I would do this by hand:
sysuse auto, clear
separate price, by(rep78)
tw scatter price? mpg
drop price?
Or in one line using Nick Cox's sepscatter command from SSC:
sepscatter price mpg, separate(rep78)
The latter command can also output other type of plots with the recast() option.
There isn't a 'simpler' built-in solution for what you want to do.
However, here's a simple wrapper command, which you can extend to meet your needs:
capture program drop foo
program define foo
syntax varlist(min=1 max=3)
quietly {
tokenize `varlist'
levelsof `3', local(foolevels)
local i = 0
local foocolors red green blue
foreach x of local foolevels {
local ++i
local extra `extra' || scatter `1' `2' if `3' == `x', mcolor("`: word `i' of `foocolors''")
}
twoway `extra'
}
end
And a toy example:
clear
set obs 10
generate A = runiform()
generate B = runiform()
generate C = .
replace C = 1 in 1/3
replace C = 2 in 4/7
replace C = 3 in 8/10
foo A B C

insert variable value label for graph title

I can't quite grasp how to insert a variable's value labels for titles to a graph.
For example, in sysuse auto, the variable foreign takes the value of 0 or 1 where 0 is labeled "Domestic" and 1 is labeled "Foreign".
In the following snippet, I want to plot the average price for each category of the variable foreign using a loop:
sysuse auto, clear
forvalues i=0/1{
local t = foreign[`i']
graph bar (mean) price if foreign == `i', ///
over(rep78, sort(price) descending) asyvars ///
title("`t'") name(p_`i', replace) nodraw
local graphs `graphs' p_`i'
}
gr combine `graphs'
but it does not even display the category value correctly in the title.
What am I doing wrong?
Your code
local t = foreign[`i']
sets the local macro t to the value of the variable foreign first in observation 0 and then in obseration 1: these will be missing and 0, respectively.
What you want is the value label corresponding to the values 0 and 1, which you can obtain with
local t : label (foreign) `i'
Swap this into your code and your graphs will be labelled Domestic and Foreign, respectively.
The syntax of the replacement command may be unfamiliar; macro "extended functions" are described in help extended_fcn.
Note that either of these graph commands
sysuse auto, clear
graph bar (mean) price , ///
over(rep78, sort(price) descending) asyvars over(foreign)
graph bar (mean) price , ///
over(rep78, sort(price) descending) asyvars by(foreign)
uses value labels automatically and produces a combined graph directly. This may not be the main question, but the original code is not a good solution on those grounds.

saving extracted statistic from regression in a dataset: Stata

I wonder if you could help me figure it out a quite simple question of how I can save an extracted statistic from a regression in a separate dataset (or file), and also add more statistics from the other regression to it later?
For example, the statistic from one regression can be extracted as e(f) and from another one is also as e(f).
Roger Newson's parmest is great for dealing with "resulsets," which are Stata datasets created from the output of a Stata command. The help file has a nice example of combining three regressions into a single file that I modified here to include R^2 [stored in e(df_r)]:
sysuse auto, clear
tempfile tf1 tf2 tf3
parmby "reg price weight", lab saving(`"`tf1'"', replace) idnum(1) idstr(M1) escal(r2)
parmby "reg price foreign", lab saving(`"`tf2'"', replace) idnum(2) idstr(M2) escal(r2)
parmby "reg price weight foreign", lab saving(`"`tf3'"', replace) idnum(3) idstr(M3) escal(r2)
drop _all
append using `"`tf1'"' `"`tf2'"' `"`tf3'"'
list idnum idstr es_1, noobs nodis

How can I import and use labels from one Stata file to the current?

I have file aa with a variable x which is labeled with value label x_lab. I would like to use this value label on the variable x of Stata file bb:
use bb, clear
label value x x_lab
How can I import the value label x_lab?
You can use label save, which saves value labels in a do-file:
label save x_lab using label.do
use bb, clear
do label.do
See Stata help for label.
This answer technique didn't work for me as I wanted the variable labels created with e.g. label var connected "connected household", not the value labels.
Instead I used this advice: http://statalist.1588530.n2.nabble.com/st-How-to-export-variables-window-td3937733.html
*************
sysuse auto, clear
log using mylog, name(newlog) replace
foreach var of varlist _all{
di _col(3) "`var'" _col(20) "`:var label `var''"
}
log close newlog
//translate from proprietary format
translate mylog.smcl mylog.txt, replace
!start mylog.txt
*************
To fix the labels that extended over multiple lines so they just used a single one, I then replaced the \n > for the oversized labels with nothing (in regex mode in atom). I could easily save into TSV from there.
Specifically:
Clean up header and footer text in the logfile output.
On Mac: use "\n" instead of "\r\n".
On Windows: first "\r\n -> ""
then whitespace at beginning "\r\n " --> "\r\n"
then convert whitespace with 3 or more spaces in middle to tabs " +" --> "\t"
(Edit manually additional errors on tab if there are still some left)
save as mylog.tsv
open in Excel, and use table of labels as needed.