Announcement

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

  • Basic putexcel not outputting to excel

    Hi all, I'm trying to output very basic entries to an excel sheet but nothing is being written into excel. I am using Stata version 14.1 and it is fully updated. I don't get any error codes but still nothing outputs to the excel sheet. My code looks like this:

    [putexcel set "Category.xlsx", replace sheet("Description", replace)
    putexcel B2 = "ID"]

    I've read all of the posts here as well as the help - putexcel file. This seems like it should be very basic, but I can't figure out what the problem is.

  • #2
    The following works for me.
    Code:
    . putexcel set "Category.xlsx", replace sheet("Description", replace)
    Note: file will be replaced when the first putexcel command is issued
    
    . putexcel B2 = "ID"
    file Category.xlsx saved
    
    . putexcel close
    Click image for larger version

Name:	result.png
Views:	1
Size:	32.6 KB
ID:	1449158

    With that said, please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. See especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], and to use the dataex command to provide sample data, as described in section 12 of the FAQ.

    Added in edit: I'm using Stata 15.1, and a careful look at help export excel reveals that
    Code:
    ... sheet("Description", replace)
    is not correct syntax; the correct syntax would apparently be
    Code:
    ... sheet("Description") sheetreplace
    although since you specify the replace option for the workbook, the sheet by definition will not already exist. Perhaps your use of incorrect syntax in your actual code caused Stata to do something incorrect.


    Last edited by William Lisowski; 15 Jun 2018, 07:34.

    Comment


    • #3
      Thanks so much for the quick reply. I did read the Statalist FAQ as well as the Advice on Posting link. My apologies if I neglected something there other than including the output. I understand this seems like it should be a simple problem to solve. Even when I remove the sheetreplace, I get the same output from the commands in Stata as above, but the spreadsheet remains blank.

      Code:
      [
      . putexcel set "Category.xlsx", replace sheet("Description")
      Note: file will be replaced when the first putexcel command is issued
      
      . putexcel B2 = "ID"
      file Category.xlsx saved
      ]
      Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	40.8 KB
ID:	1449173


      I'm not sure what other information I can include. Is it possible I need to specify the complete filepath for the excel sheet somewhere?
      Last edited by Jenn OKeeffe; 15 Jun 2018, 08:37.

      Comment


      • #4
        Hi Jenn,

        you might try it this way:
        Code:
        putexcel B2 = ("ID")

        Comment


        • #5
          Hi Carlos,

          Thanks so much. I've tried it with and without brackets and there's no difference.

          Comment


          • #6
            I note that unlike my code, your code does not include putexcel close. Regarding the file path, the file will be written to Stata's current working directory, as reported by pwd.

            Try the following code to see if the file you are writing is indeed the file you are opening in Excel.
            Code:
            putexcel set "Category.xlsx", replace sheet("Description")
            putexcel B2 = "ID"
            putexcel close
            display "`c(current_date)' `c(current_time)'"
            pwd
            dir "`c(pwd)'/Category.xlsx"
            Code:
            . putexcel set "Category.xlsx", replace sheet("Description")
            Note: file will be replaced when the first putexcel command is issued
            
            . putexcel B2 = "ID"
            file Category.xlsx saved
            
            . putexcel close
            
            . display "`c(current_date)' `c(current_time)'"
            15 Jun 2018 11:06:31
            
            . pwd
            /Users/lisowskiw/Downloads/gnxl
            
            . dir "`c(pwd)'/Category.xlsx"
            
            -rw-r--r--  1 lisowskiw  staff  2342 Jun 15 11:06 /Users/lisowskiw/Downloads/gnxl/Category.xlsx
            Added in edit: a slightly improved test.
            Code:
            putexcel set "Category.xlsx", replace sheet("Description")
            putexcel B2 = "ID"
            putexcel close
            display "`c(current_date)' `c(current_time)'"
            pwd
            dir "`c(pwd)'/Category.xlsx"
            import excel "Category.xlsx", clear
            list
            Code:
            . list
            
                 +----+
                 |  B |
                 |----|
              1. | ID |
                 +----+
            Note that Stata ignores the first, unused row in the worksheet, which is why this appears in the first observation rather than the second, as you might expect.
            Last edited by William Lisowski; 15 Jun 2018, 09:20.

            Comment


            • #7
              Hi William,

              That worked. I had a small error in the working directory code that was sending the changes to a different folder. Thanks so much for the help! Just a note that I think putexcel close is only available in Stata 15? I get the following error when I try to use it.

              Code:
              . putexcel set "Category.xlsx", modify sheet("Opendes")
              
              . putexcel A3="ID"
              file Category.xlsx saved
              
              . putexcel close
              CLOSE: invalid cell name

              Comment

              Working...
              X