Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[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 <[email protected]>
To   [email protected]
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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index