Announcement

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

  • How to copy all variables and labels?

    Click image for larger version

Name:	QQ截图20161021090945.png
Views:	1
Size:	37.3 KB
ID:	1361110

    As the captured picture above, I want to copy names and labels to WORD or EXCEL, but in stata can only copy a name once a time , how should I do to copy all I want and paste ?
    Thanks~

  • #2
    I don't know about copy-and-pasting, but you can do something like that shown below to output the variable names and labels directly to an Excel workbook. Someone might have already written some command that does this (or something analogous) transparently, and you might want to use the search command to see whether anything turns up.
    Code:
    version 14.2
    
    clear *
    set more off
    
    quietly sysuse auto
    quietly describe
    
    // Make sure that you have saved the dataset before proceding, just in case . . .
    preserve
    if r(k) > r(N)  set obs `r(k)'
    
    quietly ds
    local varlist `r(varlist)'
    
    foreach new in newlist varname varlabel {
        quietly generate str `new' = ""
    }
    
    local k 1
    foreach var of varlist `varlist' {
        local varlabel : variable label `var'
        quietly replace varname = "`var'" in `k'
        quietly replace varlabel = "`varlabel'" in `k'
        local ++k
    }
    
    quietly export excel varname varlabel using Varnamesnlabels.xlsx, ///
        sheet("Names & Labels") firstrow(variables)
    
    exit

    Comment


    • #3
      Originally posted by Joseph Coveney View Post
      I don't know about copy-and-pasting, but you can do something like that shown below to output the variable names and labels directly to an Excel workbook. Someone might have already written some command that does this (or something analogous) transparently, and you might want to use the search command to see whether anything turns up.
      Code:
      version 14.2
      
      clear *
      set more off
      
      quietly sysuse auto
      quietly describe
      
      // Make sure that you have saved the dataset before proceding, just in case . . .
      preserve
      if r(k) > r(N) set obs `r(k)'
      
      quietly ds
      local varlist `r(varlist)'
      
      foreach new in newlist varname varlabel {
      quietly generate str `new' = ""
      }
      
      local k 1
      foreach var of varlist `varlist' {
      local varlabel : variable label `var'
      quietly replace varname = "`var'" in `k'
      quietly replace varlabel = "`varlabel'" in `k'
      local ++k
      }
      
      quietly export excel varname varlabel using Varnamesnlabels.xlsx, ///
      sheet("Names & Labels") firstrow(variables)
      
      exit
      Sorry, I can't understand the meaning of this code, and I tried but nothing comes out.
      I did search two kinds of methods:
      Code:
      1.
      Code:
       search fsum, all   
      fsum, stats(n) label
      2.
      Code:
      search svvarlbl, all  
      
         svvarlbl using varlabel.log
      But I'm not satisfied with those outcomes:
      Click image for larger version

Name:	QQ截图20161021132942.png
Views:	1
Size:	15.3 KB
ID:	1361119

      Click image for larger version

Name:	QQ截图20161021133044.png
Views:	1
Size:	9.6 KB
ID:	1361120

      That's not that clear. So I want some better method.

      Comment


      • #4
        Originally posted by Spacey Shi View Post
        Sorry, I can't understand the meaning of this code, and I tried but nothing comes out.
        That's strange. I copied and pasted into a do-file the code directly from the quotation in your post, I ran the do-file, and the attached Excel workbook came out.
        Attached Files

        Comment


        • #5
          Originally posted by Joseph Coveney View Post
          That's strange. I copied and pasted into a do-file the code directly from the quotation in your post, I ran the do-file, and the attached Excel workbook came out.
          Oh,I found it, it came out quietly, I didn't got it the first time,
          And the Excel version looks much better.
          varname varlabel
          o_hhid household id of related (other) individual
          o_line line number of related (other) individual
          hhid most current houshold id
          line most current household member id
          a5 relationship to head of household (recoded to 1993+ coding)
          a8 marital status
          wave survey year
          aa11 does person still live in hh?
          aa13 where person lives now
          a5e living in the home?
          surveyed 1=have some survey data for survey year
          family_id unique family/household id by survey year
          member unique member id within family id
          relation relationship code
          primary type of individual for primary id
          rel_type biological/step/foster relationship
          mp 1=maternal and 2=paternal grandparent when known
          gender gender
          moon_dob lunar date of birth, yyyymmdd
          west_dob western date of birth, yyyymmdd
          dod date of death, yyyymmdd
          changed 1=chabged households, multi id's in master file
          Thanks sooo much,

          Comment


          • #6
            Maybe it's a bit less elegant, but far shorter and simpler is to export excel once with firstrow(variables), once with firstrow(varlabels). You can even use if _n == 1 for big tables...

            Comment


            • #7
              Originally posted by Joseph Coveney View Post
              That's strange. I copied and pasted into a do-file the code directly from the quotation in your post, I ran the do-file, and the attached Excel workbook came out.
              I ran the do-file for another dataset, but it didn't work this time:

              invalid 'home'
              r(198);

              end of do-file

              r(198);

              I don't know where is the problem.

              Comment


              • #8
                Originally posted by Kazi Bacsi View Post
                Maybe it's a bit less elegant, but far shorter and simpler is to export excel once with firstrow(variables), once with firstrow(varlabels). You can even use if _n == 1 for big tables...
                firstrow(variables)
                command firstrow is unrecognized
                r(199);
                I didn't get it....

                Comment


                • #9
                  Code:
                  export excel using "The filename you like.xlsx" if _n == 1, sheet("The sheet you like") firstrow(variables) sheetreplace
                  export excel using "The filename you like.xlsx" if _n == 1, sheet("The sheet you like2") firstrow(varlabels) sheetreplace

                  Comment


                  • #10
                    Originally posted by Kazi Bacsi View Post
                    Code:
                    export excel using "The filename you like.xlsx" if _n == 1, sheet("The sheet you like") firstrow(variables) sheetreplace
                    export excel using "The filename you like.xlsx" if _n == 1, sheet("The sheet you like2") firstrow(varlabels) sheetreplace
                    Got it, thanks very much. And may I ask you how to open a sas7bdta data file in stata?

                    Comment


                    • #11
                      There was this thread answered by Nick: http://www.stata.com/statalist/archi.../msg00732.html
                      But I would obviously use proc export in SAS to save it into Excel or CSV, and then import somehow in Stata...

                      Comment


                      • #12
                        Note that Stata's describe command has an option to create a dataset with the information it generates:

                        Code:
                        sysuse auto, clear
                        describe, replace
                        export excel name varlab using test.xlsx, firstrow(variables) replace

                        Comment


                        • #13
                          Nice and neat solution, far better than mine!

                          Comment


                          • #14
                            I second that. Elegant solution! Thank you.

                            Comment


                            • #15
                              Beautiful Now, is there a way to do the reverse? I have a dataset that is free of all labels, and I would like to be able to import variable labels from excel into Stata, is this possible?

                              Comment

                              Working...
                              X