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   Nick Cox <[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   Wed, 8 Aug 2012 17:10:15 +0100

There are other ways to do it. I showed you one. But  " " " " must
always be a problem because you don't know if it is

left left right right

or

left right left right

That's why compound double quotes became necessary. If you use them a
lot, you get used to them.

If a loop is executed many, many times, as if often is, getting an
echo of each command each time would be a nightmare. You are at the
other end of the scale and Stata's default is not what you want. My
guess us that not many people are in your position, but I could be
very wrong.

Nick

On Wed, Aug 8, 2012 at 2:13 PM, Xinjia Liu <[email protected]> wrote:
> It turned out when I assigned my command to a global cmd, and tried to do ...
> display `"$cmd"'
> $cmd
> I did not put `' in display "$cmd".
> While trying to fix the code without realizing where exactly the
> problem is, I introduced other bugs.
> The code that works in the end is...
>
> global cmd = `"table var1 var2 if var3 == "`val'", c(n var4)"'
> display `"$cmd"'
> $cmd
>
> It is too bad there is no other way to do this. I'll try to study more
> about nest quotations and use fewer loops.
> Thanks so much for all your help!
> Xinjia :)
>
> On Tue, Aug 7, 2012 at 7:21 PM, Nick Cox <[email protected]> wrote:
>> 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 <[email protected]> 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 <[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.
>> *
*
*   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