Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: how to have commands in a loop (over strings) shown in log files, without using nested quotes


From   Maarten Buis <maartenlbuis@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: how to have commands in a loop (over strings) shown in log files, without using nested quotes
Date   Tue, 7 Aug 2012 18:57:57 +0200

On Tue, Aug 7, 2012 at 6:03 PM, Xinjia Liu wrote:
> I am trying to run a command with several different "if" conditions,
> and have both the commands and the results shown in a log file.
<snip>
> If I use a loop to avoid typing the command repeatedly...
>
> foreach val in "a" "b" "" {
> table var1 var2 if var3 == "`val'", c(n var4)
> }
>
> The problem with the loop is, only the resulting table would show in
> the log file, not the actual command.
<snip>
> If I try to do
>
> log using...
> foreach ... {
> display "cmd..."
> cmd...
> }
> log close
>
> I would need to somehow (e.g. using `"') include double quotes in the
> cmd string, which could very easily introduce bugs that are hard to
> fix.

The way to do that is to use compound quotes, as you suggest. So, type
-display `"cmd..."'- instead of -display "cmd..."-. I do not see how
you think that that would be buggy. The difficulty with nesting
strings and regular double quotes is that it becomes ambiguous which
quote is a closing quote and which quote is an opening quote. That is
solved by using compound quotes, so that is that.

-- Maarten

---------------------------------
Maarten L. Buis
WZB
Reichpietschufer 50
10785 Berlin
Germany

http://www.maartenbuis.nl
---------------------------------
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index