Announcement

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

  • ICPSR-style codebook creation in a Word doc?

    Has anyone run across a command that produces a ICPSR-style codebook in Word format? Something that looks roughly like the attached screenshot for each variable in a dataset? It needs to include the variable name, variable label, type, actual values, value labels, frequencies, etc.

    I'm aware of -codebook-, but there's not a great way to get its results into a table in Word without lots of post-pasting formatting. I'm also aware of -codebookout- from SSC, which saves an Excel file but it lacks frequencies.

    Since I'm on Stata 15, and I'm going to need to do this more than once, I know I can roll my own solution using -putdocx-. But before I fall down that particular custom code rabbit hole, I figured I'd ask around to see if anyone smarter than me has a better idea.


    Click image for larger version

Name:	Untitled.png
Views:	1
Size:	168.1 KB
ID:	1453895

  • #2
    Troy,

    I have had some code that produced output to HTML in this kind of format (example), but in never occurred to me that someone would want to reuse such a simple thing rather than write his/her own up to their liking. If this post gets 25 likes I'll package that as an ado and submit to SSC.

    Best, Sergiy

    Comment


    • #3
      I ended up writing my own command, which I've called -ajicbook-, to do this. I've attached the do-file here in case others find it useful. Syntax is:

      ajicbook [varlist] using filename

      varlist is optional; if none is specified it assumes all vars.

      using filename is required and is the output file in .docx format.

      the command outputs a codebook styled similarly to ICPSR's codebooks.
      Attached Files

      Comment


      • #4
        Thanks for sharing. However, when trying to run with
        Code:
        sysuse auto
        ajicbook _all using "d:/statist/stata/data/autorep"
        I receive the error
        Code:
        java.lang.NoSuchMethodError: Method com.stata.sfi.DataFrame.dropVar(Ljava/lang/String;I)I name or signature does not mat
        > ch
        failed to create document
        r(198);
        If subsequently I run the commands
        Code:
        set trace on
        ajicbook using "d:/statist/stata/data/autorep"
        the file autorep.docx will be produced with a large empty table. The trace command shows a series of error messages, e.g.
        Code:
        ------------------------------------------------------------------------- begin putdocx._docx_table_set_cell ---
        
        <snip>
        
        - mata: docx_table_set_cell_wrk(`"`font'"', `"`halign'"', `"`valign'"', `"`shading'"', `"`borders'"', `"`bold'"'
        > , `"`italic'"', `"`underline'"', `"`strikeout'"', `"`script'"', `rowspan', `colspan', `"`span'"', `"`append'"', `"`sto
        > cell'"', `"`nformat'"', `linebreaks', `"`smallcaps'"', `"`allcaps'"')
        = mata: docx_table_set_cell_wrk(`""', `""', `""', `""', `""', `""', `""', `""', `""', `""', 1, 1, `""', `""', `"
        > "', `""', 1, `""', `""')
        java.lang.NoSuchMethodError: modCell
        java.lang.NoSuchMethodError: addLinebreakToCell

        Comment


        • #5
          Thanks! That’s weird for at least two reasons. First, I didnt know _all existed. Instead, leave varlist blank. It’ll run against all vars.

          Second, I’m not using Java at all. It’s all Stata and about one line of Mata. I’m guessing StataCorp implemented -putdocx- in Java?

          If anyone has any suggestions for a fix, I’d love to see it.

          Comment


          • #6
            Here's an updated version, with a few bug fixes for other issues. One thing that this code doesn't do is any sort of user input checking to make sure that the file specified in using is a valid filepath or filename. If you give it an invalid path, it should error with r(198) failed to save document. That's not exactly the same as what Dirk Enzmann was seeing though.
            Attached Files

            Comment


            • #7
              Regarding the issue stated in #4: The problem is solved (it has been an issue of my Stata installation, see here).

              Comment


              • #8
                For future folks arriving here by searching: I’ve rewritten this and submitted it to SSC as wordcb. To install, in Stata type

                Code:
                ssc install wordcb
                wordcb has more features and fewer rough edges than the do-file in this thread.

                Comment

                Working...
                X