Announcement

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

  • Exporting a .png file into an excel sheeet

    Hello I am having trouble trying to export a coefplot I have made. The graphs show up in stata but Im failing to save them into a folder so I can place them into an excel sheet. My question is, what would the code be to both save the graph, and then export into my excel file

    graph export Output/logistic_Results/`state'.png
    putexcel set Output/logistic_Results, modify sheet("`StateLabel'")
    putexcel B4= "`StateLabel'.png"

  • #2
    Could you say more about the contents of the local macros you are using, namely

    Code:
    state 
    StateLabel
    ?

    Comment


    • #3
      The loop seems fine but for some reason I cant get it to export and save onto my excel sheet


      *Define state names and their FIPS codes
      global independent_variables i.AGECAT i.GENDER i.RACECAT i.EMPLOY ///
      i.EDUCAT i.PSYCH DRUGFLG i.NOPRIOR

      * Loop through each state
      levelsof STFIPS, local(statelist)
      foreach state in `statelist' {
      local StateLabel : label (STFIPS) `state'

      *Run a logistic regression
      quietly logistic HEROIN $independent_variables if STFIPS == `state' ///
      & !missing(HEROIN)

      *Format the coeffplot
      coefplot, drop(_cons) ///
      baselevels omitted ///
      eform ///
      title("Odds of Heroin vs. Other Drug Admission in `StateLabel'", size(small)) ///
      headings ///
      (1.AGECAT = "{bf: Age}" ///
      1.GENDER = "{bf: Gender}" ///
      1.RACECAT = "{bf: Race}" ///
      1.EMPLOY = "{bf: Employment}" ///
      1.EDUCAT = "{bf: Education}" ///
      0.PSYCH= "{bf: Psych Diagnosis}" ///
      DRUGFLG = "{bf: Other Drugs}" ///
      1.NOPRIOR = "{bf: Prior treatment}") ///
      ciopts(lcolor(blue)) ///
      grid(none) ///
      xlabel(, labsize(small)) ///
      xscale(log range (1 4)) ///
      xtitle(Odds ratio) ///
      xsize (5) ///
      ylabel(, labsize(small)) ///
      yscale (alt) ///
      ysize (9)

      graph export Output/logistic_Results/`state'.png
      putexcel set Output/logistic_Results, modify sheet("`StateLabel'")
      putexcel E1= "`StateLabel'.png"
      }

      Comment


      • #4
        Could you show us exactly what error crops up when the code tries to export the graph?

        Also, for a start, two things:
        • I would add the option replace to the graph export command.
        • I believe your command to put the graph into excel should be
          Code:
          putexcel E1 = image(Output/logistic_Results/`state'.png)
        Last edited by Hemanshu Kumar; 05 May 2025, 11:37.

        Comment


        • #5
          file Output/logistic_Results/9.png could not be opened
          r(603);

          I mustve changed something because before I was able to view all 6 graphs

          Comment


          • #6
            And you are certain you have permissions to write to (i.e. create files in) that folder?

            I mustve changed something because before I was able to view all 6 graphs
            If you name each graph differently (e.g. with the option name(gr_`state', replace) , you'll be able to see them all in Stata.

            Comment


            • #7
              Ok sorry guys i was able to figure it out

              Comment


              • #8
                That's good, but the explanation would still be interesting. Someone else is likely to have the same problem, and your solution could then be of interest.

                Comment


                • #9
                  Could you tell us how you resolved this? It would be helpful for anyone who comes across this thread in the future.

                  Comment


                  • #10
                    Apologies but my real name is Ravi P.

                    Comment

                    Working...
                    X