Announcement

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

  • How to concatenate a local variable with a string in Stata

    I have a very large DO file that I need to control for whether the code is run in Linux or Windows.

    To do this, I thought I would add this chunk of code at the top of the file:

    Code:
    // Set OS variable for filesystem/directory control: values are: {linux, win}
        local os = "linux"
    And then whenever I had to select the correct filesystem's directory output I would have:

    Code:
    if "`os'" == "linux" {
        use "/mnt/DataResearch/DataStageData/CV_PATIENT_LABS.dta"
    }
    else {
        use "\\mrts-400-netapp\DataResearch\DataStageData\CV_PATIENT_LABS.dta"
    }
    The problem is that there are a LOT of `use`, `save` and `merge` statements in the code with hardcoded directories in them, so that putting this type of control into the DO file would not only be tedious, it would not be the most elegant of solutions.

    In python, I would just define a variable `dir_out` as such:

    Code:
    if os == 'linux':
        dir_out = '/mnt/DataResearch/DataStageData/'
    elif os == 'win':
        dir_out = '\\mrts-400-netapp\DataResearch\DataStageData\'
    else:
        pass
    and then throughout the DO file, just concatenate dir_test to the file name like:

    Code:
    use = dir_out + "CV_PATIENT_LABS.dta"
    However, I have not for the life of me figured out how to do that in Stata-ease.

    A colleague suggested using the built-in Python interpreter to do this, but I can't see how that would be any better than having a ton of the `if-then-else` control sequences interspersed throughout the code.

    Any suggestions would be most welcome.
    Last edited by Greg Silverman; 08 Aug 2020, 18:19.

  • #2
    Cross-posted and answered on Stack Overflow https://stackoverflow.com/questions/...tring-in-stata

    Please our policy on cross-posting, which is a request that you tell us about it: https://www.statalist.org/forums/help#crossposting

    Comment


    • #3
      Ended up doing something incredibly ugly, but it works:

      [CODE]
      local os = "linux"

      if "`os'" == "linux"
      {
      local dir_main "/mnt/DataResearch/DataStageData/"
      local dir_temp "/mnt/DataResearch/DataStageData/temp/"
      }
      else {
      local dir_main "\\mrts-400-netapp\DataResearch\DataStageData\"
      local dir_temp "\\mrts-400-netapp\DataResearch\DataStageData\temp\"
      }

      // files here:
      local file_main_CV_PATIENT_LABS "`dir_main'CV_PATIENT_LABS.dta"
      local file_temp_CV_PATIENT_CURR_MED "`dir_temp'CV_PATIENT_CURR_MEDS.dta"

      etc.
      ...

      Comment


      • #4
        The reasoning in the original post is very nonlinear, so I might have misunderstood what problem the original poster is trying to solve. Nick explained how one concatenates macros in Stata.

        But to achieve what (I think) the original poster is trying to achieve I do not concatenate macros manually. I simply set a working directory with -[D] cd -- Change directory-, and then everything starts to happen in this directory, so I do not need to refer to the full names of the files loading, merging, or saving them.

        Comment


        • #5
          Code:
          local project_root_win = "//mrts-400-netapp"
          local project_root_lin = "/mnt/"
          
          local project_root = cond( c(os)=="Windows", "`project_root_win'", "`project_root_lin'")
          
          cd "`project_root'"

          Comment


          • #6
            As I have commented on Stack Overflow I don't see what is ugly here behind filepaths that can't be blamed on Stata. Bjarte Aagnes tacitly underlines a point I made there, which is that forward slashes are fine in Stata under Windows.

            Comment

            Working...
            X