Announcement

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

  • combine strings to create path+file name

    I want to combine strings to create a path including the filename in one string, in order to use it in the use command.
    For example:
    Code:
    scalar pathname = "c:\data\stata\" 
    scalar filename = "test.dta" 
    use pathname+filename
    --> however, the latter code line does not work.

    Although this works to combine the strings:
    Code:
    scalar x = pathname+filename
    it does not work with
    Code:
    use x
    as I do not have quoots around it, so it opens x.dta, not c:\data\stata\text.dta

    The same problem arises when I want to create a text to use as an URL, such as
    Code:
    copy "https://maps.googleapis.com/maps/api/distancematrix/json?origins=Paris&destinations=Berlin" "test.txt", replace
    here I want to combine fixed text ("https://maps.googleapis.com/maps/api/distancematrix/json?origins=") with variable texts (the city names) in one string.

    Thanks
    Kees

  • #2
    You want Stata to do the calculation on the fly, but use doesn't offer in-line calculation. But there is a syntax for doing that.

    Code:
    . scalar au = "au"
    
    . scalar to = "to"
    
    . sysuse `=au + to'
    (1978 Automobile Data)
    What's happening that Stata has code for looking at your command line before it tries to execute it and within limits you can do some calculations of the fly so that the command in question sees the results.

    See help macro for a terse signal on the `= ' syntax.

    Almost always, I wouldn't use scalars here. I would use local macros (or global macros if really needed).

    Code:
    local pathname "c:\data\stata\"  
    local filename "test.dta"  
    use `pathname'`filename'
    Note that if you have a filename in a scalar then this is the only way to get the filename out of the scalar directly. The presence or absence of quotes (meaning "") has nothing to do with whether a scalar is evaluated. Given

    Code:
     
    use x
    will always look for a file with that name, and never evaluate any variable or scalar with the same name. (If that were so, there would be a long stream of posts from puzzled users).
    Last edited by Nick Cox; 27 Jan 2016, 04:39.

    Comment


    • #3
      Thank you. The code with scalar works, however, the second block with local doesn't work. What's the reason that you prefer local rather than scalar.

      Comment


      • #4
        Works for me:

        Code:
        . local au "au"
        
        . local to "to"
        
        . sysuse `au'`to', clear
        (1978 Automobile Data)
        I've got meetings for some hours. Someone else will probably answer your bigger question.

        Comment


        • #5
          when I do this
          Code:
          local pathfile  "C:\Users\kmaat_000\SURFdrive\McGill\"
          local infile    "McGill_data_prepared.dta"
          use `pathfile'`infile', clear
          I get
          Code:
          . local pathfile  "C:\Users\kmaat_000\SURFdrive\McGill\"
          
          . local infile    "McGill_data_prepared.dta"
          
          . 
          . use `pathfile'`infile', clear
          invalid '`' 
          r(198);
          
          end of do-file
          
          r(198);

          Comment


          • #6
            (Between meetings: you are probably executing these commands one by one from a do-file editor window. If so, locals are invisible to the last command. The commands *must* be issued as a block.)

            Comment


            • #7
              No, I did it as a block.

              Comment


              • #8
                Nick Cox (2008) has explained this problem a long time ago here.

                Also see c(dirsep) in

                Code:
                help creturn
                Best
                Daniel


                Cox, N.J. (2008). Stata tip 65: Beware the backstabbing backslash. The Stata Journal, 8(3), pp. 346-347.

                Comment


                • #9
                  Thanks! Forward slashes solved the problem.

                  I am still struggling with the second part of my question, about the url.

                  Comment


                  • #10
                    Here's some technique to help with the URL.
                    Code:
                    . local a "https://maps.googleapis.com/maps/api/distancematrix/json?origins="
                    
                    . local b "Paris"
                    
                    . local c "&destinations="
                    
                    . local d "Berlin"
                    
                    . 
                    . dir
                    
                    total 8
                    -rw-r--r--  1 lisowskiw  staff  183 Jan 27 13:13 demo.do
                    
                    . copy "`a'`b'`c'`d'" "test.txt", replace
                    
                    . dir
                    
                    total 16
                    -rw-r--r--  1 lisowskiw  staff  183 Jan 27 13:13 demo.do
                    -rw-r--r--  1 lisowskiw  staff  501 Jan 27 13:13 test.txt
                    
                    .

                    Comment


                    • #11
                      I tried the same (with different names), but got an invalid syntax r(198) error
                      Code:
                      local url_text1   "https://maps.googleapis.com/maps/api/distancematrix/json?origins="
                      local url_origin  "Paris"
                      local url_text2   "&destinations="
                      local url_dest    "Berlin"
                      copy "`url_text1'`url_origin'`url_text2'`url_dest'" "copytest.txt", replace

                      Comment


                      • #12
                        The code displayed works for me. If you used different code, we can't comment on the differences. Please just report whether you can reproduce the result, regardless of whether you care about the difference.

                        Comment


                        • #13
                          It works now, after I issued the code as a block.

                          Comment


                          • #14
                            Good.

                            Comment


                            • #15
                              Thanks for your help!

                              Comment

                              Working...
                              X