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   Xinjia Liu <[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 14:23:43 -0400

thanks for your reply.
I tried using...
display `"table var1 var2 if var3 == "`val'", c(n var4)"'

and

display `"table var1 var2 if var3 == ""`val'"", c(n var4)"'

the first one gave me an error message, and the second one gave me...

table var1 var2 if var3 == a, c(n var4)

inside the log, without the double quotes for "a", and the resulting
table is not quite right either.
It is very easy for me to make a mistake here, and it also takes a
long time for me to figure out what that is and how to fix it. So I
was just wondering if there is a way to simply avoid using nesting
strings.

Please advise.
Thanks,
Xinjia

On Tue, Aug 7, 2012 at 12:57 PM, Maarten Buis <[email protected]> wrote:
> 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/
*
*   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