Announcement

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

  • Exporting variable label with putexcel

    Hello,

    I'm exporting descriptive statistics using putexcel in Stata 14 and have been struggling to export variable labels. Based on advice on this forum and elsewhere, I'm using the following code but get an error "(: expression must be enclosed in ()".

    Code:
    local row = 4
    foreach x of varlist $varlist {
    ...
    local varlabel : var label `x'
    putexcel A`row' = ("`varlabel'")
    ...
    }
    I've checked and the label is correctly stored in varlabel (display "`varlabel'") and the putexcel command works well when exporting a phrase (putexcel A`row'=("label of variable")). Do you know what the issue might be here?

    Many thanks,

    Ioana

  • #2
    Perhaps running
    Code:
    macro list varlist
    describe $varlist
    and posting the commands and their output here would allow us to see the values of your variable names and variable labels, which might suggest the problem.

    Better yet, create a reproducible example that leaves out all the stuff you represented as ... and then run that example and copy and paste the results here.
    Code:
    putexcel set filename
    local row = 4
    macro list varlist
    foreach x of varlist $varlist {
    describe `x'
    local varlabel : var label `x'
    putexcel A`row' = ("`varlabel'")
    local row = `row'+1
    }
    For some examples I came up with, this is how it runs.
    Code:
     putexcel set ~/Downloads/foo.xlsx, replace
    Note: file will be replaced when the first putexcel command is issued
    
    . local row = 4
    
    . macro list varlist
    varlist:        x1 x2 x3 x4
    
    . foreach x of varlist $varlist {
      2. describe `x'
      3. local varlabel : var label `x'
      4. putexcel A`row' = ("`varlabel'")
      5. local row = `row'+1
      6. }
    
                  storage   display    value
    variable name   type    format     label      variable label
    --------------------------------------------------------------------------------------------------
    x1              float   %9.0g                 This is a variable label
    file /Users/lisowskiw/Downloads/foo.xlsx saved
    
                  storage   display    value
    variable name   type    format     label      variable label
    --------------------------------------------------------------------------------------------------
    x2              float   %9.0g                 This is a variable's label
    file /Users/lisowskiw/Downloads/foo.xlsx saved
    
                  storage   display    value
    variable name   type    format     label      variable label
    --------------------------------------------------------------------------------------------------
    x3              float   %9.0g                 This is (a) variable label
    file /Users/lisowskiw/Downloads/foo.xlsx saved
    
                  storage   display    value
    variable name   type    format     label      variable label
    --------------------------------------------------------------------------------------------------
    x4              float   %9.0g                 This "is" a variable label
    file /Users/lisowskiw/Downloads/foo.xlsx saved
    
    .
    Section 12.1 of the Statalist FAQ is particularly pertinent here

    12.1 What to say about your commands and your problem

    Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!
    Last edited by William Lisowski; 04 Dec 2018, 16:20.

    Comment


    • #3
      Thank you, William! It turns out I was missing the describe `x' command before defining the local. I'll make sure to include all the relevant code and output in my next post.

      Comment

      Working...
      X