Announcement

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

  • xtable gives "invalid syntax" error

    I want to save a table frequency of operations and length of stay (=vtid) over a number of hospitals(=sjhstrkod) to an xlsx-file. Hospitals is a string variable. vtid is continuous number of days.

    "table sjhstrkod ar5kat if opnummer==1&ar5kat>1975, cont(freq mean vtid)" works OK, but

    "xtable sjhstrkod ar5kat if opnummer==1&ar5kat>1975, cont(freq mean vtid) filename("vtid5arsjh.xlsx") sheet("Blad1", replace) replace" gives an error code "invalid syntax"

    Changing "Blad1" to sheet1 or Blad1 does not help

    What is wrong?

    I try noput but is gives same error

  • #2
    Using Stata 16, I was not able to reproduce your problem. The user-written xtable command (from SSC) requires Stata 13.1 or later, so that could be one source of your problem. Another possibility is that you do not have the latest version of xtable, running the ado update xtable command will solve that problem. A third possibility is that the xtable command you show is not the xtable command you actually ran. That's one of the reasons the Statalist FAQ linked to from the top of the page recommends that you copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE].
    Code:
    . clear
    
    . set obs 1000
    number of observations (_N) was 0, now 1,000
    
    . set seed 666
    
    . generate sjhstrkod = runiformint(1,3)
    
    . generate opnummer = runiformint(0,1)
    
    . generate ar5kat = runiformint(1971,1980)
    
    . generate vtid = rnormal(10,1)
    
    . 
    . table sjhstrkod ar5kat if opnummer==1&ar5kat>1975, cont(freq mean vtid)
    
    ------------------------------------------------------------
              |                      ar5kat                     
    sjhstrkod |     1976      1977      1978      1979      1980
    ----------+-------------------------------------------------
            1 |       19        16        15        16        17
              | 10.05676  9.858599  9.833144  10.16844  9.942553
              | 
            2 |       17        16        22        21        10
              | 10.28688  9.951308   10.3295  10.04701  10.15012
              | 
            3 |       14        19        16        27        16
              | 9.564639  9.931817  9.734007    10.033  10.06809
    ------------------------------------------------------------
    
    . xtable sjhstrkod ar5kat if opnummer==1&ar5kat>1975, cont(freq mean vtid) filename("vtid5arsjh.
    > xlsx") sheet("Blad1", replace) replace
    
    ------------------------------------------------------------
              |                      ar5kat                     
    sjhstrkod |     1976      1977      1978      1979      1980
    ----------+-------------------------------------------------
            1 |       19        16        15        16        17
              | 10.05676  9.858599  9.833144  10.16844  9.942553
              | 
            2 |       17        16        22        21        10
              | 10.28688  9.951308   10.3295  10.04701  10.15012
              | 
            3 |       14        19        16        27        16
              | 9.564639  9.931817  9.734007    10.033  10.06809
    ------------------------------------------------------------
    Output written to vtid5arsjh.xlsx

    Comment


    • #3
      Hi rollanders. I wrote xtable and was able to reproduce your error here by modifying William Lisowski's code to create "sjhstrkod" as a string variable. The error occurs while handling labels for exporting the table. I'll fix it and submit an update to ssc when I have the time.

      Please note that the matrix holding the results should have been created anyway, but without row and column names and not as a stored result. You can check this with matlist xtable.

      So, as a workaround, you can export the matrix xtable (important: not r(xtable)) with putexcel after calling the command.
      Last edited by Weverthon Machado; 10 Dec 2019, 07:49.
      Weverthon Machado
      weverthonmachado.github.io

      Comment


      • #4
        rollanders -

        If the problem is indeed that sjhstrkod is a string variable in your dataset, I expect you can also resolve your problem by converting sjhstrkod to a numeric variable before running xtable, which you likely will need to do for your analyses in any event.

        Weverthon Machado -

        I did what I thought you described and xtable still worked without an "invalid syntax" error. Can you tell us how you created sjhstrkod as a string variable?
        Code:
        clear
        set obs 1000
        set seed 666
        generate str8 sjhstrkod = strofreal(runiformint(1,3))
        generate opnummer = runiformint(0,1)
        generate ar5kat = runiformint(1971,1980)
        generate vtid = rnormal(10,1)
        table sjhstrkod ar5kat if opnummer==1&ar5kat>1975, cont(freq mean vtid)
        xtable sjhstrkod ar5kat if opnummer==1&ar5kat>1975, cont(freq mean vtid) filename("vtid5arsjh.xlsx") sheet("Blad1", replace) replace

        Comment


        • #5
          The command works fine when the values are exclusively numbers, even if stored as strings. So I tried including letters. A variable like this will cause the reported error:

          Code:
          gen sjhstrkod = char(runiformint(65,67))


          Weverthon Machado
          weverthonmachado.github.io

          Comment


          • #6
            rollanders

            The lesson here is that, as I suggested to you on an earlier posting here, giving example data using the dataex command is crucial to ensuring that you get a helpful answer in a timely fashion, and that those whose help you seek don't have to guess about your data.

            Comment


            • #7
              For future reference: xtable has been updated on ssc and now handles string variables correctly.
              Weverthon Machado
              weverthonmachado.github.io

              Comment

              Working...
              X