Announcement

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

  • Include quotation marks when writing a file

    I know this is Stata 101 but I'm not finding the answer. With this line,

    Code:
     file write `out' "semdata <- read_dta(`outputfilename')" _n
    I want the outputfilename enclosed in quotation marks, e.g. I want a line written like

    semdata <- read_dta("C:\Dropbox\Testprogs/ltry.dta")

    but instead I get

    semdata <- read_dta(C:\Dropbox\Testprogs/ltry.dta)

    There must be some easy way to do this, but I don't know what it is.
    -------------------------------------------
    Richard Williams, Notre Dame Dept of Sociology
    Stata Version: 17.0 MP (2 processor)

    EMAIL: [email protected]
    WWW: https://www3.nd.edu/~rwilliam

  • #2
    Hi Richard,

    I think this is the exact scenario compound double quotes have been introduced for; try this:
    Code:
    file write `out' `"semdata <- read_dta("`outputfilename'")"' _n
    Regards
    Bela

    Comment


    • #3
      Thanks! Worked like a charm. I figured it was something like that but I wasn't keeping all the quote marks straight.

      I still had a problem though, because the program that was reading the file written out doesn't like \ (forward slashes). My solution was to add this line first:

      Code:
      local outputfilename = subinstr("`outputfilename'", "\", "/", 999)
      The line then written out was

      Code:
      semdata <- read_dta("C:/Dropbox/Testprogs/ltry.dta")
      There may be something more elegant but it works
      -------------------------------------------
      Richard Williams, Notre Dame Dept of Sociology
      Stata Version: 17.0 MP (2 processor)

      EMAIL: [email protected]
      WWW: https://www3.nd.edu/~rwilliam

      Comment


      • #4
        Your fix is about as elegant as I can think of, although
        Code:
        local outputfilename = subinstr("`outputfilename'", "\", "/", .)
        (using a missing value rather than a large number for the fourth argument) will be definition replace all values, although I don't think I've ever seen a filesystem 999 levels deep in subdirectories.

        Comment


        • #5
          William Lisowski I tend to run my file systems deep, with relatively few files at each level. Lots of hierarchy. I've never really come close to 999 levels. I can tell you that in Windows and in those varieties of Unix that I have used (not many), you can't get to 999 depth even if you want to because long before that you run up against a limit on the length of a full pathname.

          That said, using missing value for the final argument in -subinstr()- does make a lot of sense, and is more likely to be what is actually intended than some large specific number.

          Comment


          • #6
            Thanks for pointing out I could use a period instead of 999. My code is now two characters shorter. ;-)

            Thanks to everybody -- these are pretty easy fixes but it would have probably taken hours to figure them out by myself.
            -------------------------------------------
            Richard Williams, Notre Dame Dept of Sociology
            Stata Version: 17.0 MP (2 processor)

            EMAIL: [email protected]
            WWW: https://www3.nd.edu/~rwilliam

            Comment

            Working...
            X