Stata string delimit, string with quotes, string regression labels, etc. (DO, more see: Fan and Stata4Econ)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
name: stata_fs_strings
log: C:\Users\fan/Stata4Econ//prog/define/fs_strings.smcl
log type: smcl
opened on: 10 May 2020, 17:11:03
. log on $st_logname
(log already on)
.
. ///-- Site Link: Fan's Project Reusable Stata Codes Table of Content
> di "https://fanwangecon.github.io/"
https://fanwangecon.github.io/
. di "https://fanwangecon.github.io/Stata4Econ/"
https://fanwangecon.github.io/Stata4Econ/
.
. ///-- File Title
> global filetitle "Stata string delimit, string with quotes, string regression labels, etc."
.
. ///////////////////////////////////////////////////////////////////////////////
> ///--- String Operations
> ///////////////////////////////////////////////////////////////////////////////
>
. ///--- Search and Replace Text in Substring
>
. * replace quote in string
. di subinstr(`"dataVar1 " dataVar2"',`"""',"",.)
dataVar1 dataVar2
.
. * Replace quotes in string
. di subinstr(`" "dataVar1 dataVar2 " "dataVar2 dataVar3" "',`"""',"",.)
dataVar1 dataVar2 dataVar2 dataVar3
.
. * Replace & with /& in long string
. global scd ""
. global scd "${scd} Conditions: PA=(& el\_i\_mand\_talk\_m2a != -999 & S\_han !=.);"
. global scd "${scd} PB=(& el\_i\_mand\_talk\_m2a != -999 & S\_han == 0);"
. global scd "${scd} PC=(& el\_i\_mand\_talk\_m2a != -999 & S\_han == 1);"
. global scd "${scd} common=(S\_han !=. & AgeCloseYr\_i\_G1 <= 30 & H\_age <= 44"
. global scd "${scd} & (vE\_schCloseYr\_full >= 1998 | vE\_schCloseYr\_full == 0)"
. global scd "${scd} & (vE\_schCloseYr\_full >= 1998 | vE\_schCloseYr\_full == 0)"
. global scd "${scd} & (vE\_schCloseYr\_full >= 1998 | vE\_schCloseYr\_full == 0)"
. global scd "${scd} & (vE\_schCloseYr\_full >= 1998 | vE\_schCloseYr\_full == 0)"
. global scd "${scd} & (vE\_schCloseYr\_full >= 1998 | vE\_schCloseYr\_full == 0)"
. global scd "${scd} & (vE\_schCloseYr\_full >= 1998 | vE\_schCloseYr\_full == 0)"
. global scd "${scd} & (vE\_schCloseYr\_full >= 1998 | vE\_schCloseYr\_full == 0)"
.
. global scd = subinstr("${scd}","&","\&",.)
. di "${scd}"
Conditions: PA=(\& el\_i\_mand\_talk\_m2a != -999 \& S\_han !=.); PB=(\& el\_i\_mand\_talk\_m2a != -999 \& S\_han == 0); PC=(\& el\_i\_mand\_talk\_m2a != -999 \&
> S\_han == 1); common=(S\_han !=. \& AgeCloseYr\_i\_G1 <= 30 \& H\_age <= 44 \& (vE\_schCloseYr\_full >= 1998 | vE\_schCloseYr\_full == 0) \& (vE\_schCloseYr\_ful
> l >= 1998 | vE\_schCloseYr\_full == 0) \& (vE\_schCloseYr\_full >= 1998 | vE\_schCloseYr\_full == 0) \& (vE\_schCloseYr\_full >= 1998 | vE\_schCloseYr\_full == 0
> ) \& (vE\_schCloseYr\_full >= 1998 | vE\_schCloseYr\_full == 0) \& (vE\_schCloseYr\_full >= 1998 | vE\_schCloseYr\_full == 0) \& (vE\_schCloseYr\_full >= 1998 |
> vE\_schCloseYr\_full == 0)
.
. * Replace dash
. local tableRefName = "a_b_c"
. local tableRefName = subinstr("`tableRefName'","_","",.)
. di "`tableRefName'"
abc
.
. * replace pound
. local instrCap = "_d1_l1#_d1_l2 _d2_l2#_d2_l4"
. local cinstrCapF = subinstr(word("`instrCap'",1),"#"," ",.)
. di "`cinstrCapF'"
_d1_l1 _d1_l2
.
. ///////////////////////////////////////////////////////////////////////////////
> ///--- String Definitions and Regressions
> ///////////////////////////////////////////////////////////////////////////////
> ///--- Load Data
> set more off
. sysuse auto, clear
(1978 Automobile Data)
.
. ///--- Define Multiple Variables as global in delimit
> #delimit;
delimiter now ;
. global vars_rhs "
> mpg
> ib1.rep78
> headroom trunk
> weight
> ";
. #delimit cr
delimiter now cr
.
. di `"$vars_rhs"'
mpg ib1.rep78 headroom trunk weight
.
. ///--- Define String with Quotes
> #delimit;
delimiter now ;
. global st_coef_label "
> mpg "mpg variable"
> 1.rep78 "BASE GROUP CONSTANT = rep78 is 1"
> 2.rep78 "rep78 is 2"
> 3.rep78 "rep78 is 3"
> 4.rep78 "rep78 is 4"
> 5.rep78 "rep78 is 5"
> headroom "headroom variable"
> trunk "this is the trunk variable"
> weight "and here the weight variable"
> ";
. #delimit cr
delimiter now cr
.
. di `"$st_coef_label"'
mpg "mpg variable" 1.rep78 "BASE GROUP CONSTANT = rep78 is 1" 2.rep78 "rep78 is 2" 3.rep78 "rep78
> is 3" 4.rep78 "rep78 is 4" 5.rep78 "rep78 is 5" headroom "headroom variable" trunk "this is the t
> runk variable" weight "and here the weight variable"
.
. ///--- Describe and Summarize
> d $rhs_vars_list, f
Contains data from C:\Program Files (x86)\Stata14\ado\base/a/auto.dta
obs: 74 1978 Automobile Data
vars: 12 13 Apr 2014 17:45
size: 3,182 (_dta has notes)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
storage display value
variable name type format label variable label
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
make str18 %-18s Make and Model
price int %8.0gc Price
mpg int %8.0g Mileage (mpg)
rep78 int %8.0g Repair Record 1978
headroom float %6.1f Headroom (in.)
trunk int %8.0g Trunk space (cu. ft.)
weight int %8.0gc Weight (lbs.)
length int %8.0g Length (in.)
turn int %8.0g Turn Circle (ft.)
displacement int %8.0g Displacement (cu. in.)
gear_ratio float %6.2f Gear Ratio
foreign byte %8.0g origin Car type
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sorted by: foreign
. summ $rhs_vars_list
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
make | 0
price | 74 6165.257 2949.496 3291 15906
mpg | 74 21.2973 5.785503 12 41
rep78 | 69 3.405797 .9899323 1 5
headroom | 74 2.993243 .8459948 1.5 5
-------------+---------------------------------------------------------
trunk | 74 13.75676 4.277404 5 23
weight | 74 3019.459 777.1936 1760 4840
length | 74 187.9324 22.26634 142 233
turn | 74 39.64865 4.399354 31 51
displacement | 74 197.2973 91.83722 79 425
-------------+---------------------------------------------------------
gear_ratio | 74 3.014865 .4562871 2.19 3.89
foreign | 74 .2972973 .4601885 0 1
.
. ///--- Run Regression
>
. eststo clear
. eststo, title("reg1"): regress price $vars_rhs if foreign == 0
Source | SS df MS Number of obs = 48
-------------+---------------------------------- F(8, 39) = 6.08
Model | 265270208 8 33158776 Prob > F = 0.0000
Residual | 212697489 39 5453781.76 R-squared = 0.5550
-------------+---------------------------------- Adj R-squared = 0.4637
Total | 477967697 47 10169525.5 Root MSE = 2335.3
------------------------------------------------------------------------------
price | Coef. Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
mpg | 92.54338 163.0892 0.57 0.574 -237.3357 422.4224
|
rep78 |
2 | 1582.106 1987.532 0.80 0.431 -2438.058 5602.269
3 | 1832.208 1869.079 0.98 0.333 -1948.361 5612.776
4 | 921.8792 2015.345 0.46 0.650 -3154.542 4998.3
5 | 4172.732 2681.573 1.56 0.128 -1251.261 9596.725
|
headroom | -436.3395 519.777 -0.84 0.406 -1487.688 615.0087
trunk | -76.14985 136.1647 -0.56 0.579 -351.5689 199.2692
weight | 4.610868 1.053809 4.38 0.000 2.479338 6.742399
_cons | -10261.73 6562.752 -1.56 0.126 -23536.15 3012.686
------------------------------------------------------------------------------
(est1 stored)
. eststo, title("reg2"): regress price $vars_rhs if foreign == 1
note: 1b.rep78 identifies no observations in the sample
note: 5.rep78 omitted because of collinearity
Source | SS df MS Number of obs = 21
-------------+---------------------------------- F(6, 14) = 8.35
Model | 77099791.3 6 12849965.2 Prob > F = 0.0006
Residual | 21555563.2 14 1539683.09 R-squared = 0.7815
-------------+---------------------------------- Adj R-squared = 0.6879
Total | 98655354.6 20 4932767.73 Root MSE = 1240.8
------------------------------------------------------------------------------
price | Coef. Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
mpg | 41.78335 69.31091 0.60 0.556 -106.8738 190.4405
|
rep78 |
1 | 0 (empty)
3 | 866.061 1060.461 0.82 0.428 -1408.402 3140.524
4 | 1367.986 686.8411 1.99 0.066 -105.1419 2841.114
5 | 0 (omitted)
|
headroom | -237.1521 660.3787 -0.36 0.725 -1653.524 1179.219
trunk | 155.15 94.41051 1.64 0.123 -47.34039 357.6404
weight | 5.747012 1.259762 4.56 0.000 3.045091 8.448933
_cons | -9844.945 4889.272 -2.01 0.064 -20331.39 641.4998
------------------------------------------------------------------------------
(est2 stored)
.
. esttab, title("regtest") ///
> mtitle ///
> coeflabels($st_coef_label) ///
> varwidth(50)
regtest
----------------------------------------------------------------------------------
(1) (2)
reg1 reg2
----------------------------------------------------------------------------------
mpg variable 92.54 41.78
(0.57) (0.60)
BASE GROUP CONSTANT = rep78 is 1 0 0
(.) (.)
rep78 is 2 1582.1
(0.80)
rep78 is 3 1832.2 866.1
(0.98) (0.82)
rep78 is 4 921.9 1368.0
(0.46) (1.99)
rep78 is 5 4172.7 0
(1.56) (.)
headroom variable -436.3 -237.2
(-0.84) (-0.36)
this is the trunk variable -76.15 155.2
(-0.56) (1.64)
and here the weight variable 4.611*** 5.747***
(4.38) (4.56)
_cons -10261.7 -9844.9
(-1.56) (-2.01)
----------------------------------------------------------------------------------
N 48 21
----------------------------------------------------------------------------------
t statistics in parentheses
* p<0.05, ** p<0.01, *** p<0.001
.
. ///--- End Log and to HTML
> log close _all
name: stata_fs_strings
log: C:\Users\fan/Stata4Econ//prog/define/fs_strings.smcl
log type: smcl
closed on: 10 May 2020, 17:11:03
-------------------------------------------------------------------------------------------------------------------------------------------------------------------