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: macro compound double quotes


From   Abhimanyu Arora <[email protected]>
To   [email protected]
Subject   Re: st: macro compound double quotes
Date   Wed, 11 Apr 2012 08:24:04 +0200

Thanks very much indeed for the insights, Matthew and Elan.
Cheers
Abhimanyu

On Tue, Apr 10, 2012 at 8:07 PM, Cohen, Elan <[email protected]> wrote:
> -mac list _inclcities- should shed some light on the issue.  The only true spaces that Stata is maintaining are those within quotes.
> -di `countcity'- results in 5 because of the 4 spaces within city names (hence creating 5 "words").
>
> HTH,
>
> - Elan
>
>
> -----Original Message-----
> From: [email protected] [mailto:[email protected]] On Behalf Of Matthew White
> Sent: Tuesday, April 10, 2012 13:56
> To: [email protected]
> Subject: Re: st: macro compound double quotes
>
> Hi Abhimanyu,
>
> -help extended_fcn- notes:
>
> word count string returns the number of words in string.  A token is a
> word (characters separated by spaces) or set of words enclosed in
> quotes.  Do not enclose string in double quotes because word count
> will return 1.
>
> Since in both cases the local was enclosed by double quotes, I'd
> expect a result of 1. Using Stata 11.2 SE, I ran the following:
>
> loc inclcities `"`"Accra"' `"Addis Ababa"' `"Almaty"' `"Antananarivo"'
> `"Bamako"' `"Bangkok"' `"Beijing"' `"Bishkek"' `"Calcutta"'
> `"Colombo"' `"Conakry"' `"Dakar"' `"Dar es Salaam"' `"Delhi"'
> `"Dhaka"' `"Dushanbe"' `"Hanoi"' `"Harare"' `"Islamabad"' `"Jakarta"'
> `"Johannesburg"' `"Kabul"' `"Kampala"' `"Karachi"' `"Kathmandu"'
> `"Khartoum"' `"Kinshasa"' `"Lagos"' `"Lhasa"' `"Lomé"' `"Luanda"'
> `"Lusaka"' `"Manila"' `"Maputo"' `"Mogadishu"' `"Mumbai"' `"Nairobi"'
> `"Niamey"' `"Phnom Penh"' `"Rangoon"' `"Saigon"' `"Ulan Bator"'
> `"Vientiane"' `"Yaounde"'"'
>
> loc countcity : word count `inclcities'
> di `countcity'
>
> loc countcity : word count `"`inclcities'"'
> di `countcity'
>
> loc countcity : word count "`inclcities"
> di `countcity'
>
> Resulting in:
>
> . loc inclcities `"`"Accra"' `"Addis Ababa"' `"Almaty"'
> `"Antananarivo"' `"Bamako"' `"Bangkok"' `"Beijing"' `"Bishkek"'
> `"Calcutta"' `"Colombo"' `"Conakry"' `"Dakar"' `"Dar es Salaam"'
>>  `"Delhi"' `"Dhaka"' `"Dushanbe"' `"Hanoi"' `"Harare"' `"Islamabad"' `"Jakarta"' `"Johannesburg"' `"Kabul"' `"Kampala"' `"Karachi"' `"Kathmandu"' `"Khartoum"' `"Kinshasa"' `"Lagos"'
>> `"Lhasa"' `"Lomé"' `"Luanda"' `"Lusaka"' `"Manila"' `"Maputo"' `"Mogadishu"' `"Mumbai"' `"Nairobi"' `"Niamey"' `"Phnom Penh"' `"Rangoon"' `"Saigon"' `"Ulan Bator"' `"Vientiane"' `"Ya
>> ounde"'"'
>
> .
> . loc countcity : word count `inclcities'
>
> . di `countcity'
> 44
>
> .
> . loc countcity : word count `"`inclcities'"'
>
> . di `countcity'
> 5
>
> .
> . loc countcity : word count "`inclcities"
>
> . di `countcity'
> 1
>
> The first and third results are expected, but I'm confused by the
> second. The following seems to show that Stata is coming up against
> some kind of limit:
>
> loc word1 : word 1 of `"`inclcities'"'
> di `"`word1'"'
>
> Another example. The following returns 4 when 2 is expected:
>
> mata: st_local("x", "x" * 600 + " " + "x" * 600)
> loc wc : word count `x'
> di `wc'
>
> All that said, none of this should be a problem for you as long as you
> don't enclose `inclcities' by double quotes.
>
> Best,
> Matt
>
> On Tue, Apr 10, 2012 at 1:12 PM, Abhimanyu Arora
> <[email protected]> wrote:
>> As a follow up I managed to find the right answer (by hit and trial)
>>
>> local temp1:list clean inclcities
>> local countcity:word count `temp1'
>> di `countcity'
>>
>> So it seems that unless the elements of a macro are minimally adorned,
>> stata doesn't parse the spaces between the tokens (even if they are
>> distinguished by compound double quotes)
>>
>> Cheers
>> Abhimanyu
>>
>>
>> On Tue, Apr 10, 2012 at 6:51 PM, Abhimanyu Arora
>> <[email protected]> wrote:
>>> Dear statalist
>>> I have been trying to understand this phenomenon
>>>
>>> http://screencast.com/t/m6fGA8g9lxfX
>>>
>>> From what I have read in the manuals, the compound double quotes help
>>> stata distinguish the tokens better when the macros themselves contain
>>> quotes. So isn't the first syntax more appropriate?
>>> But even then, the number returned when the second syntax is used
>>> doesn't seem to be correct.
>>>
>>> Many thanks and best regards
>>> Abhimanyu
>>> *
>>> *   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/
>
>
>
> --
> Matthew White
> Data Coordinator
> Innovations for Poverty Action
> 101 Whitney Avenue, New Haven, CT 06510 USA
> +1 434-305-9861
> www.poverty-action.org
>
> *
> *   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/

*
*   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