Announcement

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

  • Put value labels into excel

    Hi, I'm looking for a way to access the value labels for a categorical variable, to export them into Excel.

    This is my code:
    Code:
    putexcel set agegroups.xlsx, modify
    putexcel A1 = ("Age group")
    putexcel B1 = ("Coefficient")
    putexcel C1 = ("se")
    putexcel D1 = ("l95ci")
    putexcel E1 = ("u95ci")
    stcox i.trial##i.age10, nolog
    scalar counter = 1
    forvalues i = 1/4 {
        lincom 1.trial + 1.trial#`=counter'.age
        putexcel A`=counter+1' = (?????)
        putexcel B`=counter+1' = (`=r(estimate)')
        putexcel C`=counter+1' = (`=r(se)')
        putexcel D`=counter+1' = ((`=r(estimate)') - 1.96 * (`=r(se)'))
        putexcel E`=counter+1' = ((`=r(estimate)') + 1.96 * (`=r(se)'))
        scalar counter = `=counter+1'
    }
    What I'd like is for the question marks to refer to the value labels for age10 (which are 40-49, 50-59 etc), so I can then use that excel file to make a graph.

    Come to think of it, is there away to do that excel stuff (import excel, twoway …) in the middle of my do file and then return to the previous state of my original dataset, i.e. without having to re-create all my variables and everything? My solution at the moment is to just have the code for my graph at the bottom of my do file, but it feels like there should be a better way.

    I've only just started using Stata for anything more advanced than simple regression commands, and I'm figuring things out as I go along, so I'd welcome any other remarks, like if there's a simpler/better way to do my code.

  • #2
    Your question mark should be replaced by something like this
    Code:
    `"`:label age10 `i''"'
    as for your second question, the preserve / restore commands will allow you to change the data, create the graph, and then restore it back again to an earlier point.

    Comment


    • #3
      Oded Mcdossi, close and correct if the value label is named age10. If you are trying to get the value label and only know the variable name then you would put age10 in parentheses.

      Comment


      • #4
        Indeed, good point!

        Comment


        • #5
          Since Oliver also asked for any tips on improving his coding, it appears that in his loop, counter is always equal to i . So there's no need to have both. In fact, i itself seems to play no role: it is never referred to inside the loop. Also, instead of re-evaluating counter+1 (or i+1 below) in each command, it makes sense to evaluate it just once. So I would code this as:
          Code:
          forvalues i = 1/4 {
              lincom 1.trial + 1.trial#``i'.age
              local j = `i' + 1
              putexcel A`j' = `"`label (age10) `i''"'
              putexcel B`j' = (`=r(estimate)')
              putexcel C`j' = (`=r(se)')
              putexcel D`j' = ((`=r(estimate)') - 1.96 * (`=r(se)'))
              putexcel E`j' = ((`=r(estimate)') + 1.96 * (`=r(se)'))
          }

          Comment


          • #6
            Many thanks to all three, everything's working just as I want it and my code looks better.

            Comment

            Working...
            X