Announcement

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

  • Set a specific folder to save regression outputs and graphs

    Hello!

    I need to set a specific folder (directory), different from the current working directory, to save the output of my regressions and my graphs. I don't know how to do it. I looked for a solution everywhere, but I couldn't find anything. This is because the idea is to create a new directory with mkdir and after that I would like to set that new directory as the one to save the work generated by the codes in the DO file. So, if I send that do file to somebody else, that person would only need to change the working directory to the one with the necessary files to run the codes and get tables, graphs and so on in a different folder, previously created in the code with mkdir.

    Thank you in advance.

  • #2
    Hi Santiago, you can do it by setting globals as in the example below.
    Code:
    global path "C:/Users/..."
    global tables "$path/Tables"
    mkdir $tables
    global graphs "$path/Graphs"
    mkdir $graphs
    cd $path
    For your output you can then specify the graphs or tables folder, for example:
    Code:
    reg y x
    outreg2 using $tables/regyx.doc

    Comment


    • #3
      Hello Wouter. Thank you very much for yor answer. I'm trying to set up the configutarion I want, but I think I am missing something. Lets say I put a working directory with "cd" and a different directory to save graphs, tables and so on. This is what I am writing:

      PHP Code:
      mkdir C:\Users\Usuario\Desktop\Folder1, public

      global 
      path "C:\Users\Usuario\Desktop\Folder1"
      global tables "$path"
      global graphs "$path"
      cd "C:\Users\Usuario\Google Drive\Folder2" 

      I want to use files to work wich are in Folder2, but I want to save outputs, like tables and graphs, in Folder1, wich I previously created with mkdir. When I run my codes with putexcel to create excel tables, Stata is still saving the results in the cd folder, that is, Folder2, when I want to save it in Folder1.

      Comment


      • #4
        Did you specify your alternative folder in putexcel set? Otherwise it will be automatically saved in your working directory.
        Code:
        putexcel set $path/results

        Comment


        • #5
          Wouter Wakker I am facing a similar issue, but my following code does not work in allocating a different output directory:

          Code:
          global inputdir "/Users/saunok/Library/CloudStorage/Dropbox/Projects/RA Work/HistoricTrade/raw/schedules/originals/withclassno"
          cd "${inputdir}"
          global outputdir "/Users/saunok/Library/CloudStorage/Dropbox/Projects/RA Work/HistoricTrade/BoxFiles/grand_merge/imports_to_tariffs/tariffs_to_sectors"
          mkdir "${outputdir}"
          The first one works while the second doesn't. I am getting the error message:
          "could not create directory /Users/saunok/Library/CloudStorage/Dropbox/Projects/RA Work/HistoricTrade/BoxFiles/grand_merge/imports_
          > to_tariffs/tariffs_to_sectors
          r(693);"

          The path names are long, but they are within the max length of 4096 characters permitted on macos (I'm using a Mac). I'm using StataNow/SE 18.5


          Regards,
          Saunok

          Comment


          • #6
            You could get that error if the folder already exists. Have you confirmed this is not the case?

            If your code sometimes runs with this folder already on disk and sometimes not, you might want to do

            Code:
            capture mkdir "${outputdir}"
            Last edited by Hemanshu Kumar; 24 Jun 2024, 03:09.

            Comment


            • #7
              Hemanshu Kumar that worked! Thanks. I don't know why it did, though -- I thought capture just prevented an error from stopping the execution of the rest of the program. Here, "capture mkdir" is creating the global macro even though I get an error in creating the macro?

              Comment


              • #8
                mkdir does not make a global macro, it creates the directory. If that directory already exists, then that would result in an error. However you don't care about that error, as the directory already existing is not a problem in your case. So you suppres that error with capture. The global macro $outputdir was created in the line
                global outputdir "/Users/saunok/Library/CloudStorage/Dropbox/Projects/RA Work/HistoricTrade/BoxFiles/grand_merge/imports_to_tariffs/tariffs_to_sectors"
                ---------------------------------
                Maarten L. Buis
                University of Konstanz
                Department of history and sociology
                box 40
                78457 Konstanz
                Germany
                http://www.maartenbuis.nl
                ---------------------------------

                Comment


                • #9
                  Maarten Buis I understand now, thanks!

                  Comment

                  Working...
                  X