Announcement

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

  • Embedded quotes question

    Hello,

    I'm trying to auto-generate do-files by copy/pasting from datasets created by the do-file this question pertains to.

    I want this as a variable value in the dataset in row 8:
    Code:
    cap mkdir "`projectfolder'/`projectname' QC"
    I tried to do this with this line of code:
    Code:
    replace preface = `"cap mkdir "`projectfolder'/`projectname' QC""'  if _n ==8
    However, as you might guess, I get this value:
    Code:
    cap mkdir "/ QC"
    How do I fix this?

    Thank you, brilliant Statalist!

    v.14.2

  • #2
    There is nothing obviously wrong with the code you show. Most likely it is a question of where and when you are running these and where and when you are defining local macros projectfolder and projectname.

    The key point is that if you define a local macro in a do file, its existence continues only through that do file. If you go to another do file, all local macro definitions are lost. Also, even within a do file, a local macro defined inside a program is undefined outside that program and vice versa. Finally, if you are running a do file in pieces rather than all at one time, a local macro defined in one piece ceases to be defined as soon as you reach the end of the segment you are running. If you then try to run the next segment in the do file, macro definitions from the previous segment are lost.

    Comment


    • #3
      Hi Clyde,

      Thanks for the reply. I get what you're saying about defining locals in do-files, but I guess I'm specifically curious if there's a way to replace a variable value with the text that I'd use to define a macro, without it functioning as a macro in the do-file (only as the text)?

      Thanks,
      Reese

      Comment


      • #4
        There's nothing wrong with the code you have. It works for me.

        Code:
        . clear*
        
        . local projectfolder My project folder
        
        . local projectname project1
        
        . set obs 1
        number of observations (_N) was 0, now 1
        
        . gen preface = `"cap mkdir "`projectfolder'/`projectname' QC""'
        
        . list
        
             +-------------------------------------------+
             |                                   preface |
             |-------------------------------------------|
          1. | cap mkdir "My project folder/project1 QC" |
             +-------------------------------------------+
        
        . 
        end of do-file
        (I am using Stata version 15.1, but I am quite confident that nothing involved in this code has changed from version 14.2.)

        Comment


        • #5
          Oh, sorry, I must not have communicated well. I don't want the resulting value to be the value of the local, but I want it to be the text required to call the local. So I want it to read `projectname', not project1.

          Sorry, I suspect that this is somewhat of an unusual thing to do and it's making it confusing. And that I'm not communicating well either doesn't help.

          Comment


          • #6
            A small modification to Clyde's code does the trick. The backslash escapes the special meaning of the following character, so it is not interpreted as the start of a macro expansion.
            Code:
            . gen preface = `"cap mkdir "`projectfolder'/\`projectname' QC""'
            
            . list
            
                 +------------------------------------------------+
                 |                                        preface |
                 |------------------------------------------------|
              1. | cap mkdir "My project folder/`projectname' QC" |
                 +------------------------------------------------+

            Comment


            • #7
              Perfect William, thank you so much!

              Comment

              Working...
              X