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   Nick Cox <njcoxstata@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   Wed, 8 Aug 2012 00:21:40 +0100

If you need to nest quotation marks there is no alternative to doing
it correctly.

Your first form looks quite right to me so I am puzzled on why it
doesn't work. I don't have your data, but the following code works for
me and should be testable by you.

The syntax looks odd at first sight but it is perfectly logical, being
on all fours with () [] and { }. The principle is that " " " " is not
an acceptable form because it is ambiguous, but `" " " "' is fine
because it implies nesting. Given that the outer quotation marks can
only match each other, the inner ones must match too.

sysuse auto, clear
decode foreign, gen(origin)
tab origin
foreach val in Domestic Foreign {
	display `" table rep78 head if origin == "`val'" "'
	table rep78 head if origin == "`val'"
}

foreach val in "Domestic" "Foreign" {
	display `" table rep78 head if origin == "`val'" "'
	table rep78 headroom if origin == "`val'"
}




On Tue, Aug 7, 2012 at 7:23 PM, Xinjia Liu <lxjliu@gmail.com> wrote:
> 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 <maartenlbuis@gmail.com> 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.
*
*   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