Labeling Stata Variables, and Get Label and all Value Labels from Variables (DO, more see: Fan and Stata4Econ)

-------------------------------------------------------------------------------------------------------------------------------------------------------------------
      name:  stata_fs_label
       log:  C:\Users\fan/Stata4Econ//prog/basics/fs_label.smcl
  log type:  smcl
 opened on:   7 May 2020, 20:48:56


. 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 "Labeling Stata Variables, and Get Label and all Value Labels from Variables"

. . ///--- Load Data > set more off

. sysuse auto, clear (1978 Automobile Data)

. . /////////////////////////////////////////////////////////////////////////////// > ///--- Labeling > /////////////////////////////////////////////////////////////////////////////// > . label variable make "Make and Model from the mtcars dataset"

. . label define foreign_lab 0 "domestic made" 1 "foreign made", modify

. label values foreign foreign_lab

. . /////////////////////////////////////////////////////////////////////////////// > ///--- Get Label Values > /////////////////////////////////////////////////////////////////////////////// > . ///--- Variable Labels show > labelbook foreign_lab, d

------------------------------------------------------------------------------------------------------------------------------------------------------------------- value label foreign_lab -------------------------------------------------------------------------------------------------------------------------------------------------------------------

values labels range: [0,1] string length: [12,13] N: 2 unique at full length: yes gaps: no unique at length 12: yes missing .*: 0 null string: no leading/trailing blanks: no numeric -> numeric: no definition 0 domestic made 1 foreign made

variables: foreign



. . ///--- Get Variable Label and Values hard-coded > local st_var_label : variable label foreign

. local st_foreign_val_0_lab : label foreign_lab 0

. local st_foreign_val_1_lab : label foreign_lab 1

. . di "st_var_label:`st_var_label'" st_var_label:Car type

. di "st_foreign_val_0_lab:`st_foreign_val_0_lab'" st_foreign_val_0_lab:domestic made

. di "st_foreign_val_1_lab:`st_foreign_val_1_lab'" st_foreign_val_1_lab:foreign made

. . ///--- Get Variable Label and Values more Automated > /* > For automated value printing etc: > Given Variable Name: > 1. get the label of the variable > 2. get all value labels > 3. get the number of observation each value of categorical > 4. generate string based on these > */ . . * 0. Var name . global st_var "foreign"

. * 1. get variable label . local st_var_label : variable label ${st_var}

. global st_var_label "`st_var_label'"

. * 2. all values of foreign label . local st_var_val_lab_name: value label ${st_var}

. levelsof ${st_var}, local(ls_var_levels) clean 0 1

. di "`st_var_val_lab_name'" foreign_lab

. di "`ls_var_levels'" 0 1

. * 3. Number of Observations from Each category . tab ${st_var}, matcell(mt_obs)

Car type | Freq. Percent Cum. --------------+----------------------------------- domestic made | 52 70.27 70.27 foreign made | 22 29.73 100.00 --------------+----------------------------------- Total | 74 100.00

. * 4. all label values . global st_var_val_labs ""

. local it_ctr = 0

. foreach it_foreign_lvl of numlist `ls_var_levels' { 2. local foreign_lvl_lab : label `st_var_val_lab_name' `it_foreign_lvl' 3. di "`it_foreign_lvl':`foreign_lvl_lab'" 4. local it_ctr = `it_ctr' + 1 5. if (`it_ctr' > 1 ) { 6. global st_var_val_labs "${st_var_val_labs}, " 7. } 8. global it_cate_obs = el(mt_obs, `it_ctr', 1) 9. global st_var_val_labs "${st_var_val_labs}`it_foreign_lvl'=`foreign_lvl_lab' [N=${it_cate_obs}]" 10. } 0:domestic made 1:foreign made

. * 5. final outputs . di "${st_var_label}" Car type

. di "For Outcome ${st_var_label}: ${st_var_val_labs}" For Outcome Car type: 0=domestic made [N=52], 1=foreign made [N=22]

. global slb_table_varinfo "${st_var_label} (${st_var_val_labs}, NA excluded from Regression)"

. di "${slb_table_varinfo}" Car type (0=domestic made [N=52], 1=foreign made [N=22], NA excluded from Regression)

. . ///--- End Log and to HTML > log close _all name: stata_fs_label log: C:\Users\fan/Stata4Econ//prog/basics/fs_label.smcl log type: smcl closed on: 7 May 2020, 20:48:56 -------------------------------------------------------------------------------------------------------------------------------------------------------------------