Announcement

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

  • space in global macros

    Dear Stata listers,

    I am pretty sure this will brand me as a complete lightweight, but I guess my threshold for shame is very high

    I have a problem with global macros. This has been caused by a recent update to dropbox (all my data resides there) when the folder was renamed "Dropbox (Personal)" (with a space) from simply "Dropbox".

    In my profile.do, I had the following:

    global F7 B:/Dropbox/

    and I had countless statements of the following kind in my do files:

    use ${F7}CGAF4/CGAF_2007_012.dta, clear

    On my office computer, the drive that has all my data is D:, so my profile.do would read:

    global F7 D:/Dropbox/

    Everything was well in the world.

    Now dropbox has decided to rename my folder "Dropbox (Personal)" with a space (gasp.) I am furious about this, but I cannot get them to change it back.

    I have tried to trick my profile.do with this:

    global F7 `"'B:/Dropbox (Personal)/

    and then, in my do file:

    use ${F7}CGAF4/CGAF_2007_012.dta", clear

    (note the quote before the comma). But that does not seem to work.

    Can you think of a workaround?

    Regards

    Pierre

  • #2
    Now that the Dropbox part of the full path includes a space, you have to enclose such files references with double quotes. Try

    Code:
    global F7 "B:/Dropbox (Personal)/"
    use "${F7}CGAF4/CGAF_2007_012.dta", clear

    Comment


    • #3
      Robert is right. But I think Pierre is concerned about a deeper problem. I'm guessing he has a lot of code lying around that has relied on the use ${F7}[whatever] syntax--and all of that code is now broken.

      Going forward, it is always safer to enclose filenames in quotes, even if you "know" they don't contain spaces. Because, as this episode points up, you never really "know."

      You might try
      Code:
      global F7 `""B:/Dropbox (Personal)/"'  // NOTE CLOSED COMPOUND DOUBLE QUOTE AT END
      I think that will cause Stata to store the open single quote in $F7 and it might solve your problem.

      All of that said, this action by Dropbox is really outrageous: you are probably not the only person who is being severely inconvenienced, or worse, by this. If it were me, I would ditch Dropbox and find another cloud storage service if they continue to refuse to fix it.

      Comment


      • #4
        All the old file references are broken because they do not enclose the full path in double quotes. Pierre's post is ambiguous in that there are two versions of

        Code:
        use ${F7}CGAF4/CGAF_2007_012.dta, clear
        use ${F7}CGAF4/CGAF_2007_012.dta", clear
        the first one without any double quotes and the second with. I do not see why Pierre's code would be peppered with malformed file references (without an initial double quote but terminated with a double quote). To achieve a full path enclosed in double quotes by splitting the double quotes between a global macro and a do-file like this is just making things complicated for no good reason. Perhaps Pierre did not understand that you can reference a global within a string enclosed by double quotes.

        The larger issue is that using full paths in any do-file makes the code not portable. If the code is to be shared with others or eventually published, all paths will have to be adjusted before the code can run. You can reduce the quantity of changes needed by defining globals or using include to define locals but I prefer solutions that rely on relative paths. You set Stata's current directory and reference all files relative to that.

        Personally, I use project (ssc des project) and never use full paths. project goes one step further and changes Stata's current working directory to the one that contains the currently running do-file (and does this automatically). Since I put relevant files within the same directory as the do-file, most of my file references use simply the file name with no path at all.

        Comment


        • #5
          Thanks to you both. Robert, your workaround gets the job done, though it means that I have to edit all my old files whenever I run them. My hope is that it won't be too bad because dropbox will realize they screwed up and will release an update in the not-so-distant future. And I will look into project.

          Comment


          • #6
            I'm a Mac user so I can't give you an exact example that would work for you but using my favorite editor (BBEdit), I can do multiple file search and replace using grep. The following search pattern seems to work:

            Code:
             search for (note there is a leading and trailing space)
            
             (\$\{F7\}.+),
            
            and replace with (again, with a leading and trailing space)
            
             "\1",
            to target all file references that start with " ${F7}" followed by an arbitrary number of characters until ", " is found. The "\1" in the replacement pattern is what has matched (the part that's within the parentheses).

            This single search and replace can be applied at once to all do-files in any directory and sub-directory within. I would make sure to practice on a small subset of files but this could save you a lot of manual changes. Perhaps someone handy with PC's can suggest an equivalent.

            Comment

            Working...
            X