Quantcast
Channel: Statalist
Viewing all articles
Browse latest Browse all 73268

Using display to reproduce code without Stata interpretation of code

$
0
0
Hey Statalist Community,

Apologies for all the code snippets, but I figured the best way for me to explain the issue and my desired outcome is to provide a reproducible example. I appreciate your help.

Version 13.1
OS: Windows 7 Enterprise

To reproduce the issue, we do not need to execute this first snippet of code. This code, which I shall call Code 01, is only necessary for reference purposes. Consider the following code saved to the working directory as duties.do:
Code:
    // ----- Example 01 ----- //
    foreach x of global duties {
        replace DutyType`x' = "A" if NAV1`x' != ""
        replace AV1`x' = NAV1`x' if DutyType`x' == "A"
        replace NAV1`x' = "" if DutyType`x' == "A"
    }

    // ----- Example 02 ----- //
    foreach x of local vars {
        replace `x' = subinstr(`x',"'","",.)
    }
We do need to execute this snippet of code, which I shall refer to as Code 02. Consider the following code:
Code:
file open duties using duties.do, read
file read duties line

while r(eof) == 0 {
    display `"`line'"'
    file read duties line
    }

file close duties
Upon execution of Code 02, we receive Result 01:
Code:
. file open duties using duties.do, read

. file read duties line

. 
. while r(eof) == 0 {
  2.         display `"`line'"'
  3.         file read duties line
  4.         }
        // ----- Example 01 ----- //
        foreach x of global duties {
                replace DutyType = "A" if NAV1 != ""
                replace AV1 = NAV1 if DutyType == "A"
                replace NAV1 = "" if DutyType == "A"
        }

        // ----- Example 02 ----- //
        foreach x of local vars {
                replace  = subinstr(,,,.)' invalid name
r(198);

end of do-file

r(198);

.
There are two issues:
  1. In no case do we have `x' reproduced in the display. I realize that Stata is treating `x' as a local macro and substituting the undefined value into the displayed value, but I do not want Stata to do that.
  2. In Example 02 of Code Result 01, the subinstr(`x',"'","",.) portion is also being interpreted by Stata. I realize that I can fix this issue by using compound-compound double quotes in Code 02, but that results in most lines beginning with `" and ending with "', which I also do not want.
With those examples in mind, my question is this: How can I get Stata to ignore any interpretation of
Code:
 display `"`line'"'
and simply reproduce the line as it is written?

Thank you

Viewing all articles
Browse latest Browse all 73268

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>