Announcement

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

  • Show the working directory and save it in a macro

    Hi~
    I can use command cd or pwd to show the working directory. But how can I save the returned results in a macro.
    If I can do this, it will be much easier when I share my codes with others. They will not need to update and reset the working director manually before running my codes.

    Any advice?

    Thanks~


  • #2
    Originally posted by Xinya Hao View Post
    I can use command cd or pwd to show the working directory. But how can I save the returned results in a macro.
    There is a whole lot of things accessible as a creturned value, see help creturn . One of these is the current working directory in c(pwd) . So you don't need any command before that, it is always available.

    Originally posted by Xinya Hao View Post
    If I can do this, it will be much easier when I share my codes with others. They will not need to update and reset the working director manually before running my codes.
    I find that for these things I most often work with relative paths. The simplest form is to just to only mention the filename and not mention the path at all. That means it looks for that file in the current working directory. So in that case you typically don't need c(pwd) .

    Say we have a directory structure like below and we are in D:\projects\fancy_project, then we can refer to raw_data.csv as posted/data/raw_data.csv, again we don't need to specify the current directory and we don't need c(pwd). Say we are in D:\projects\fancy_project\work, then we can we can go "back" a directory by saying ..\ So we could access raw_data.csv as ..\posted\data\raw-data.csv.

    Code:
    D/
    ├─ projects/
    │  ├─ fancy_project/
    │  │  ├─ docu/
    │  │  │  ├─ fancy_researchlog.txt
    │  │  ├─ posted/
    │  │  │  ├─ data/
    │  │  │  │  ├─ raw_data.csv
    │  │  ├─ work/
    │  │  │  ├─ fancy_main.do
    │  │  │  ├─ fancy_dta01.do
    │  │  │  ├─ fancy_ana01.do
    │  │  │  ├─ fancy01.dta
    My workflow consists of one .do file, in this example fancy_main.do, that calls all other .do files in my project (fancy_dta01.do and fancy_ana01.do). This fancy_main.do is the only place that sets the working directory. Everywhere else I only use relative paths. So I can copy the folder fancy_project to any computer with Stata, change the first line of fancy_main.do, and everything should run. To read more on this see: J. Scott Long (2009) The Workflow of Data Analysis Using Stata. College Station, TX: Stata Press. Also see ssc desc mkproject



    Last edited by Maarten Buis; 25 Oct 2022, 04:48.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Thank you very much for your detailed reply.
      I will try the workflow you suggested.

      Comment

      Working...
      X