Announcement

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

  • How to edit a codebook like this picture?

    How to edit a codebook like this? I mean, is it formed by editing within Stata commands or from other software (Word or Latex) ?

    Click image for larger version

Name:	WechatIMG6.jpeg
Views:	1
Size:	412.7 KB
ID:	1516263

  • #2
    If you look at SSC packages for exporting codebooks, there are many options to choose from ( -findit codebook-), but I doubt any of them will match this format precisely. If you have some wiggle room in the presentation of the information I'd use a log file to export the -codebook- output (maybe with some -filefilter- post processing) or -descsave- from SSC to export codebook information I need.

    If you want precisely this layout, the last column would be the most work. -codebook- has some of the decisionmaking about presenting continuous vs. categorical/labeled values baked in, but for the table above you'd have to program your own parameters for this part.

    Here's a really quick example of one way to approach this. There are lots of decision points left in this (e.g., whether to include missings or not, what to do with unlabeled categorical vars, etc) and places where you could optimize this. After you get the final codebook.dta dataset you'd then decide whether you export to word/excel or latex (check out -texsave- for exporting a dataset to Latex). Code below requires installation of -distinct- (use -findit distinct- to install)

    HTH



    Code:
    clear
    sa codebook.dta, replace emptyok
    
    sysuse auto, clear
    keep mpg price for   rep78
    lab def rep78 1 "test" 2 "test2" 3 "test3" 4 "test4" 5 "test5", modify
    lab val rep78 rep78
    
    **would need to add some code to deal with string or unlabeled categorical vars if they exist
    loc z = 1
    foreach v of varlist * {
    preserve
    g lab = `"`:var lab `v''"'
    g var = `"`v'"'
    **how many values
    loc n
    qui distinct `v'
    loc n `r(ndistinct)'
    loc k = 1
    if `n' <= 10 & !mi(`n') { //categorical, assumes vallabs
    g val1 = ""
    g val2 = .
    qui levelsof `v', loc(l)   //add missing?
    loc k = `r(r)'
        loc o = 1
        foreach a in `r(levels)' {
        replace val2 = `a' in `o'
        loc vl `"`:val lab `v''"'
        replace val1 = `"`:lab `vl' `a''"'  in `o'
        loc `++o'
        }
        replace lab = "" in 2/l
    }
    if `n' >10 & !mi(`n') { //continuous
    qui su `v', d
    loc llabs `" From `r(min)' to `r(max)' "'
    g val1 = "Record in order"
    g val2 = `'"`llabs'"'
    replace var = "" in 2/l
    
    }
    
    keep lab var val*
    keep in 1/`k'
    *for sorting
    g sort = `z'
    loc `++z'
    
    cap tostring val2, replace
    append using codebook.dta
    sa codebook.dta, replace
    restore
    }
    
    u codebook, clear
    sort sort   val2
    l, noobs sepby(var)
    when you're done manipulating or resorting the table you can drop 'sort' before final export.

    Click image for larger version

Name:	Screen Shot 2019-09-14 at 8.51.44 AM.png
Views:	1
Size:	213.7 KB
ID:	1516318
    Last edited by eric_a_booth; 14 Sep 2019, 07:56.
    Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

    Comment


    • #3
      In my view the example you showed is done using latex.

      Comment

      Working...
      X