Announcement

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

  • export codebook to text file

    Is there any way to export a codebook to a text file 'on the fly', rather than creating a log file and extracting it from there.
    I am editing/creating a dataset that is in flux and it would be nice to periodically create a codebook and send it directly to a text file.
    like
    codebook _all, compact put to "filename.txt"
    Is that possible

    Many thanks
    Don

  • #2
    Code:
    quietly log using filename.txt, text replace
    codebook
    quietly log close

    Comment


    • #3
      Sorry. The code in my above post won't quite work as you want. You will get "quietly log close" as the last line in filename.txt. But otherwise it's there. I can't offhand think of any way around that.

      Comment


      • #4
        Perfect!
        Many thanks
        Don

        Comment


        • #5
          Actually, I just figured out how to get rid of that "quietly log close" line at the end of the codebook file:

          Code:
          quietly {
              log using filename.txt, text replace
              noisily codebook
              log close
          }

          Comment


          • #6
            Thank you Clyde for providing this code. Is there a way to do the same for a compact codebook? I like the format when I write "codebook, compact". However, simply adding noisily codebook, compact to your code doesn't work. Any suggestions?

            Comment


            • #7
              In what sense does it not work? I run:

              Code:
              quietly {
                  log using filename.txt, text replace
                  noisily codebook
                  log close
              }
              
              type auto.txt
              and Stata gives me:

              Code:
              . sysuse auto, clear
              (1978 Automobile Data)
              
              . quietly {
              
              Variable      Obs Unique      Mean   Min    Max  Label
              --------------------------------------------------------------------------------------------------------------------------------------------
              make           74     74         .     .      .  Make and Model
              price          74     74  6165.257  3291  15906  Price
              mpg            74     21   21.2973    12     41  Mileage (mpg)
              rep78          69      5  3.405797     1      5  Repair Record 1978
              headroom       74      8  2.993243   1.5      5  Headroom (in.)
              trunk          74     18  13.75676     5     23  Trunk space (cu. ft.)
              weight         74     64  3019.459  1760   4840  Weight (lbs.)
              length         74     47  187.9324   142    233  Length (in.)
              turn           74     18  39.64865    31     51  Turn Circle (ft.)
              displacement   74     31  197.2973    79    425  Displacement (cu. in.)
              gear_ratio     74     36  3.014865  2.19   3.89  Gear Ratio
              foreign        74      2  .2972973     0      1  Car type
              --------------------------------------------------------------------------------------------------------------------------------------------
              
              .
              . type auto.txt
              
              Variable      Obs Unique      Mean   Min    Max  Label
              --------------------------------------------------------------------------------------------------------------------------------------------
              make           74     74         .     .      .  Make and Model
              price          74     74  6165.257  3291  15906  Price
              mpg            74     21   21.2973    12     41  Mileage (mpg)
              rep78          69      5  3.405797     1      5  Repair Record 1978
              headroom       74      8  2.993243   1.5      5  Headroom (in.)
              trunk          74     18  13.75676     5     23  Trunk space (cu. ft.)
              weight         74     64  3019.459  1760   4840  Weight (lbs.)
              length         74     47  187.9324   142    233  Length (in.)
              turn           74     18  39.64865    31     51  Turn Circle (ft.)
              displacement   74     31  197.2973    79    425  Displacement (cu. in.)
              gear_ratio     74     36  3.014865  2.19   3.89  Gear Ratio
              foreign        74      2  .2972973     0      1  Car type
              --------------------------------------------------------------------------------------------------------------------------------------------

              Comment


              • #8
                This is a neat workaround! Would it be possible to write out the codebook, compact output using putexcel?

                I can accomplish the same thing with a combination of tabstat and some other commands, but it's kind of a pain to use putexcel variable labels. The codebook, compact output already has the information I want 85% of the time.

                Thanks!

                Comment


                • #9
                  Unfortunately, but perhaps unsurprisingly, -codebook- doesn't leave anything behind in r() to work with. And the text file that gets written out by the code in #2 doesn't import usefully with -import delimited-. So there is nothing I can think of that would be less painful than working with -tabstat- etc.

                  Comment


                  • #10
                    Some of the information generated by codebook can also be generated with des command. For example, variable name, type, format, and varlable. If that serves your purpose, then asdoc (From SSC) can create a very neat table. See the following example. Yes, the describe command does not generate the number of obs, unique values, mean, and min. I have a plan to include support for codebook in the next version of asdoc.

                    Code:
                    ssc install asdoc, replace
                    sysuse auto, clear
                    asdoc des, position type isnumeric format vallab replace
                    Click image for larger version

Name:	Untitled.png
Views:	1
Size:	53.9 KB
ID:	1461516
                    Regards
                    --------------------------------------------------
                    Attaullah Shah, PhD.
                    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
                    FinTechProfessor.com
                    https://asdocx.com
                    Check out my asdoc program, which sends outputs to MS Word.
                    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

                    Comment


                    • #11
                      really helpful! thanks

                      Comment


                      • #12
                        Using asdoc seems very easy and produces a very clean codebook. A quick question to Dr. Attauler. Is it possible to ensure that full labels are produced with no cut words? I understand this is an old post but hopefully someone can help with this.

                        Comment


                        • #13
                          For a codebook with a bit more bells and whistles see ssc desc htmlcb
                          ---------------------------------
                          Maarten L. Buis
                          University of Konstanz
                          Department of history and sociology
                          box 40
                          78457 Konstanz
                          Germany
                          http://www.maartenbuis.nl
                          ---------------------------------

                          Comment


                          • #14
                            Nick Andrews here is one example of a lengthy label. If you have a case where the labels are cut, please post an example

                            Code:
                            sysuse auto
                            label var price "A very lengthy label for the price variable just for testing"
                            asdoc des, position type isnumeric format vallab replace
                            Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	38.4 KB
ID:	1574412
                            Regards
                            --------------------------------------------------
                            Attaullah Shah, PhD.
                            Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
                            FinTechProfessor.com
                            https://asdocx.com
                            Check out my asdoc program, which sends outputs to MS Word.
                            For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

                            Comment


                            • #15
                              As an update to this thread, I have now include codebook subcommand in asdocx. The codebook can be exported to MS Word, Excel or LaTeX. Details can be found here https://fintechprofessor.com/asdocx/...xcel-or-latex/
                              Regards
                              --------------------------------------------------
                              Attaullah Shah, PhD.
                              Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
                              FinTechProfessor.com
                              https://asdocx.com
                              Check out my asdoc program, which sends outputs to MS Word.
                              For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

                              Comment

                              Working...
                              X