Announcement

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

  • Using local macros of directories works with commands such as import/use but not cd.

    !!!
    I've solved the issue myself. Of course, no shenanigans involved on the side of Stata. Just the fact that, for some reason, I need to use both double quotes and `' for the cd command to work. Maybe someone could explain why this is? I thought you could access local macros without any single or double quotes?
    !!!


    Hello,

    I've an incredibly weird problem that I cannot wrap my head around. I am new to Stata, so could be a stupid error, but I can't find anything of help online, on the cd documentation, nor with AIs.

    Anyways. I've defined local macros of directories to cut down on typing and increase readability. These macros work with commands such as import delimited and save. However, they do not work with cd. In that case, I get the error r(170).

    Here's the code of relevance:
    Code:
    clear
    cd
    
    local deso_populationdata_directory "/Users/theodorselimovic/Library/CloudStorage/OneDrive-Personal/Sciences Po/M2 Spring Courses/Advanced Panel Econometrics/Project/DeSo befolkning"
    
    
    foreach file of local mycsvfilelist {
        import delimited "`deso_populationdata_directory'/`file'", varnames(1) clear
        local outfile = subinstr("`file'",".csv","",.)
        save "`deso_populationdata_directory'/`outfile'", replace
    }
    
    cd deso_populationdata_directory

    Why does cd send out r(170) when deso_populationdata_directory clearly exists, is not protected, and is a directory? Could it be due to the fact that I'm working in a cloud environment?

    Thank you!
    Last edited by Theodor Selimovic; 17 Mar 2025, 04:00.

  • #2
    I think you just need
    Code:
    cd "`deso_populationdata_directory’"

    Comment


    • #3
      I thought you could access local macros without any single or double quotes?
      Not quite true. If you want to do local macro substitution, i.e. have Stata substitute the contents of the macro for its name (as here), you need to enclose the local macro name in single quotes. The need for additional double quotes here arises because you want the folder name to be surrounded in double quotes (since it contains spaces etc) without which the cd command would give an error.

      If you want to "access" the macro but not specifically do macro substitution, then you don't need the single quotes. For instance you might do something like
      Code:
      foreach item of local myitems {
      ...
      }
      to iterate through the words in a local macro called myitems.

      You might want to take a look at sections 18.3.1 and 18.3.5 of the manual entry on macros.

      Comment


      • #4
        To sum it up:

        1. A local macro in Stata may contain spaces. Some arcane or more advanced exceptions aside, it should be referenced using single quotation marks ` '

        2. When referring in Stata to directory, folder or filenames including spaces, you must use double quotation marks " ".

        3. #1 and #2 are separate principles, but in practice you may need to pay attention to both.

        Comment

        Working...
        X