Announcement

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

  • Trouble embedding quotes within a program.

    What is wrong with the green font line of code in the program below? It throws a 198 error: invalid '1'. However when I change the line to save project`2', replace after setting a directory for the do file, it runs perfectly. But I need to add another path when saving within the program. I've tried double quotes but it does not work. Thank you.

    capture program drop closed2020
    program define closed2020
    clear
    use "C:\Documents\Stata Code Output\dataset_a"
    encode projectid, gen(`1')
    keep if `1'==`2'
    set obs `=_N+1'
    foreach var in a b c d e {
    replace `var' = `var'[_n-1] if `1'==.
    }
    save "C:\Documents\project`"`2'"", replace
    end

    closed2020 pid2 1
    closed2020 pid2 2
    closed2020 pid2 3
    closed2020 pid2 4
    closed2020 pid2 5
    closed2020 pid2 6
    closed2020 pid2 7
    closed2020 pid2 8
    closed2020 pid2 9
    closed2020 pid2 10

  • #2
    The outer quotes must be compound quotes. Try

    Code:
    save `"C:\Documents\project"`2'""' , replace
    I cannot comment much on what the code does and you did not ask for it, but I will note that hard-coding a dataset to be loaded and repeatedly load the entire dataset does not strike me as the most obvious thing to do. Probably the goals that you are trying to achieve with your program are better achieved in another way.

    Comment


    • #3
      Thanks, Daniel. It still does not work. The code won't read the `2' as part of the loop. The program creates separate datasets that another program edits and then appends together. I'm sure there could be a more efficient way to achieve this but so far this approach worked.

      Comment


      • #4
        Originally posted by Santiago Ramirez View Post
        The code won't read the `2' as part of the loop.
        Sorry, I do not know what you mean. The line of code is not executed in a loop. Also, there is nothing wrong with the code. Windows, which you seem to be using, does not allow quotes in filenames. That has nothing to do with Stata's interpretation of locals, loops, or the specific line of code. You might be looking for

        Code:
        save "C:\Documents\project`2'" , replace

        Comment


        • #5
          I don't see where `2' is defined in your code which leads me to believe that you are showing too liitte of your code for an adequate response

          Comment


          • #6
            Re #5: When you enter a program, Stata parses the line that called it into tokens and those tokens, within the program, can be accessed as `1', `2', `3', etc.
            Code:
            . clear*
            
            . capture program drop my_program
            
            . program define my_program
              1.     forvalues i= 1/3 {
              2.         display `"``i''"'
              3.     }
              4.     exit
              5. end
            
            .
            . my_program a b c d e
            a
            b
            c
            
            .
            end of do-file

            Comment


            • #7
              Worked perfectly, thank you.

              Comment


              • #8

                "C:\Documents\project`2'" , replace, worked, thanks Daniel. Clyde, the code you shared is very useful reference since I tend to work with programs.

                Comment


                • #9
                  Hi all. It looks like I am having the same problem in another program. I've tried several options for embedding quotes but the file p2 won't open (the green line in the program code). I used Daniel's suggestion which worked before and have tried the structure shown in Clyde's code, but with no success. Thank you again for your help.

                  capture program drop aproj
                  program define aproj
                  clear
                  use "C:\Documents\Stata Code Output\`1'"
                  save "C:\Documents\Stata Code Output\`1'", replace
                  append using "C:\Documents\Stata Code Output\`2'"
                  save "C:\Documents\Stata Code Output\`3'", replace
                  end

                  aproj p2 project3 p3
                  aproj p3 project4 p4
                  aproj p4 project5 p5
                  aproj p5 project6 p6
                  aproj p6 project7 p7
                  aproj p7 project8 p8
                  aproj p8 project9 p9
                  aproj p9 project10 p10
                  ...

                  Comment


                  • #10
                    The problem is because you are using backslash (\) as your path separator. When Stata sees \`, it understands that as a literal ` character, not as \ followed by the opening of a local macro. Use / as your path separator. Even if you are running on Windows, this will work from within Stata. When you use / as the path separator, Stata will properly understand ` to mark the start of a local macro, will correctly parse your commands, and will then, behind the scenes, replace the / path separators by \ just before passing your I/O commands on to Windows.
                    Code:
                    capture program drop aproj
                    program define aproj
                        clear
                        use "C:/Documents/Stata Code Output/`1'"
                        save "C:/Documents/Stata Code Output/`1'", replace
                        append using "C:/Documents/Stata Code Output/`2'"
                        save "C:/Documents/Stata Code Output/`3'", replace
                    end
                    
                    aproj p2 project3 p3
                    aproj p3 project4 p4
                    aproj p4 project5 p5
                    aproj p5 project6 p6
                    aproj p6 project7 p7
                    aproj p7 project8 p8
                    aproj p8 project9 p9
                    aproj p9 project10 p10
                    should do it.

                    Added: Within your program the -save "C:/Documents/Stata Code Output/`1'", replace- command serves no purpose. It is just writing the data, which has not been changed, back into the same file. It wastes time and accomplishes nothing.
                    Last edited by Clyde Schechter; 22 Jun 2022, 16:05.

                    Comment


                    • #11
                      Thank you, Clyde. The problem is solved when using / as the path separator. And indeed, the -save line of code serves no purpose, I have removed it.

                      Comment

                      Working...
                      X