Announcement

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

  • Export variable labels and names into Excel or CSV file

    I am trying to export the variable names and variable labels from a Stata file to a csv or Excel file. I did a search on the question and found this. When I try to use this the Export command I get a 198 error. I am using Stata 12. The Stata file is here. My code follows:

    Code:
     use "\\dfs\wiwi\home\Perdue\Desktop\VAUD_73to79_varlist.dta", clear
    Code:
    export excel using "\\dfs\wiwi\home\Perdue\Desktop\var_labels.xls", firstrow(varlabels) replace
    Code:
    too many or no variables specified r(198)
    I also tried a variant:

    Code:
     . export excel commune year id var73_10 year var73_11 var73_12 var73_13 var73_14 var73_15 var73_16 using "\\dfs\wiwi\home\Perdue\Desktop\var_labels.xls", firstrow(varlabels) replace
    Code:
    too many or no observations specified r(198);
    Any help is appreciated!

    This question has been cross-posted to Stackoverflow here


    Last edited by John Michael Perdue; 09 Sep 2016, 09:08.

  • #2
    John,

    The first error has to do with the Excel .xls limitation of 256 columns or less. This can be solved by making your extension .xlsx instead of .xls.

    The second error has to with the fact that there are no observations in your data set. Since export excel is meant to export data, not just variable names, it cannot work with an empty data set. I see two possible solutions, perhaps there are more:

    1. Create a dummy record using set obs 1. This record can be deleted from the Excel sheet (if it even shows up at all) after export.
    2. Do describe, replace to create a data set containing the variable names and labels and export that data set to Excel.

    Regards,
    Joe

    Comment


    • #3
      Joe,

      Thanks a bunch! Naturally, your solution worked.However, when I ran describe, replace, I got an error; so, I simply exported two files, one with the variable labels and one with the variable names. After some cut and paste action, all is good! I'm one happy camper. Thanks again for the help!
      Last edited by John Michael Perdue; 09 Sep 2016, 10:47.

      Comment


      • #4
        Hi!
        I am trying to do something very similar to this. I need to export the information on all the variables, such as name of the variable, variable label, values for categorical variables and value labels in the dataset (the database has observations). I am running label list as it compiles the info I need. I would need to export it to excel, each variable in a row, with value and value labels. Any idea on how I can build this using STATA 15. I just started using this software and cannot find information on how to do this anywhere. Thanks

        Comment


        • #5
          Originally posted by Rita Neves View Post
          Hi!
          I am trying to do something very similar to this. I need to export the information on all the variables, such as name of the variable, variable label, values for categorical variables and value labels in the dataset (the database has observations). I am running label list as it compiles the info I need. I would need to export it to excel, each variable in a row, with value and value labels. Any idea on how I can build this using STATA 15. I just started using this software and cannot find information on how to do this anywhere. Thanks
          For finding this sort of information, start with using the -findit- command with your search terms (e.g. run: -findit export describe- , -findit export values-, etc). As mentioned above, you can use -describe- to export these elements, here is a example:


          Code:
          sysuse auto, clear
          
          preserve
              describe, replace
              list
              export excel using myfile.xlsx, replace first(var)
          restore
          Also, you mention also wanting to be able to export the values with value labels -- this would necessitate more rows than you describe since there are multiple labeled values per variable. Assuming you actually want what you describe, here's an example using -fre- from SSC to do this (there are many ways to do this, but this one is my favorite):

          Code:
          **install from ssc
          ssc install fre, replace
          
          **make more labeled vars:
          encode make, g(make2)
          
          keep make2 for
          fre using myvalues.xls, replace combine
          Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

          Comment


          • #6
            Hi all,

            I am trying something very similar with a couple of additions. I want the output to have the following columns:

            serial no. |var name | var label | value label |
            1. |a1 | gender | a1_lab 1=male 2=female

            I know `describe, replace` gives me variables, variable labels, and names of value labels, but could we possible tweak it to get the codebook too (as in 1=male/2=female in the example above)? Or is there any other command that can do this?

            Any help is appreciated. Thanks!

            Comment

            Working...
            X