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: 2 questions on function wordcount


From   "Impavido, Gregorio" <GImpavido@imf.org>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   RE: st: 2 questions on function wordcount
Date   Thu, 3 May 2012 13:07:42 -0400

Thank you Nick. Exhaustive!

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox
Sent: Thursday, May 03, 2012 1:00 PM
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: 2 questions on function wordcount

To spell this out (the number 6 here is just the number of variables
in a currently open Stata)

. unab vlist : *

. di `: word count `vlist''
6

. di c(k)
6

i.e. note also that the number of variables in memory is directly
accessible in c(k). So, this is a one way to do it in this case

. di "`: word `c(k)' of `vlist''"
group

More generally

. local k `: word count `vlist''

. di "`: word `k' of `vlist''"

The last could be telescoped into one line, with some cost in readability.

Note that the limit that bites here is not on macro length, but on the
length of string in a string expression.

Nick

On Thu, May 3, 2012 at 5:39 PM, Austin Nichols <austinnichols@gmail.com> wrote:
> Gregorio <GImpavido@imf.org>:
> wordcount() will truncate the string at 255 characters. Instead use
> -local- tricks described at
> help extended_fcn

 On Thu, May 3, 2012 at 12:34 PM, Impavido, Gregorio <GImpavido@imf.org> wrote:

>> The following two examples should give the same result but they don't.
>>
>> /*-------begin 1 ---------*/
>> unab vlist : *
>> di wordcount(`"`vlist'"')
>> /*-------end 1 ---------*/
>>
>> /*-------begin 2 ---------*/
>> unab vlist : *
>> local i 0
>> foreach var of local vlist {
>>        local i = `i' + 1
>> }
>> di `i'
>> /*-------end 2 ---------*/
>>
>> In my case, wordcount gives me a different result depending on how I rename the variables in the dataset while the second gives me always 170 (which happens to be the number of variables in the dataset.
>>
>> Question 1. Is this related to the length limitation of the local macro? But if Yes, why is the macro vlist not truncated?
>>
>> Question 2. Is there a quicker way to do display the last word in vlist (i.e., -di word((`"`vlist'"',wordcount(`"`vlist'"'))-) than using example 2 above to extract the number of the last variable?

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