Announcement

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

  • Is it possible to "export delimited" with variable labels instead of variable names?

    Dear Statalisters,

    I'm quite new to Stata and I've run into what seems like a very "basic" problem. I would like to export my dataset from Stata to a csv file, but instead of having variable names on the first row, I need to have variable labels. I know that this is possible for exporting to excel with the code below, but for creating text files this option doesn't seem to exist. Am I missing something here? Is there any easy way to "go around" this? Of course the excel file can then be saved as a csv file, but I'm trying to avoid any additional steps here.

    export excel using "test.xlsx", firstrow(varlabels)

    Thank you in advance!

    Best regards,
    Elina

  • #2
    If you have fewer observations than variables, then you can save your variable labels to an extra string variable and then use them in your new environment -- with a loop there to put them where desired. There could be -- should be -- better work-arounds, so this is just one idea.


    Code:
    gen varlabel = "" 
    
    local i = 1 
    foreach v of var * { 
          replace varlabel = "`: var label `v''" in `i' 
          local ++I 
    }

    Comment


    • #3
      Should be

      Code:
      local ++i
      in #2.

      Comment


      • #4
        Hi Nick,

        Thank you for this suggestion! For now this should work as I don't have as many observations as variables, but this may change in the future as the dataset in question is a register with new data potentially coming in. So, if there are any other work-arounds, I'd be happy to hear!

        Best regards,
        Elina

        Comment


        • #5
          This is not the sort of thing I typically want to do, but I am surprised to infer that there isn't an easy work-around. Assuming access to MS Excel, or a program that can read its spreadsheet file formats, export excel following by saving as .csv might be another one.

          Comment


          • #6
            Here is a trick that exploits the capability of export excel

            Code:
            sysuse auto, clear
            export excel myfile,  firstrow(varlabels) replace
            import excel myfile.xls, clear
            export delimited myfile, novarnames replace

            Comment


            • #7
              Super, a big thank you both Nick and Andrew for your help!

              Comment

              Working...
              X