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 at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: matcell option in tab command within a loop inside postfile


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: matcell option in tab command within a loop inside postfile
Date   Sun, 24 Feb 2013 11:00:54 +0000

As a demonstration

sysuse auto

foreach v of var * {
      qui tab `v'
      di "`v'{col 20}"   r(r)
}

make               74
price              74
mpg                21
rep78              5
headroom           8
trunk              18
weight             64
length             47
turn               18
displacement       31
gear_ratio         36
foreign            2

. distinct

              |        Observations
              |      total   distinct
--------------+----------------------
         make |         74         74
        price |         74         74
          mpg |         74         21
        rep78 |         69          5
     headroom |         74          8
        trunk |         74         18
       weight |         74         64
       length |         74         47
         turn |         74         18
 displacement |         74         31
   gear_ratio |         74         36
      foreign |         74          2

Neither approach exports the results to a separate data file, however.
Internally, -distinct- does not use -tabulate- given the -limits- on
the number of rows that can be accommodated.

For more discussion of this problem, including other ways to do it, see

SJ-8-4  dm0042  . . . . . . . . . . . .  Speaking Stata: Distinct observations
        (help distinct if installed)  . . . . . .  N. J. Cox and G. M. Longton
        Q4/08   SJ 8(4):557--568
        shows how to answer questions about distinct observations
        from first principles; provides a convenience command

.pdf accessible via http://www.stata-journal.com/sjpdf.html?articlenum=dm0042

On Sat, Feb 23, 2013 at 9:57 AM, Nick Cox <njcoxstata@gmail.com> wrote:
> The number of categories is stored in r(r) after -tabulate, so even
> with that approach you need do nothing with matrices.
>
> However, none of this is needed. -distinct- (SJ) gives you this output
> directly. -search distinct- to get a location from which to download.
>
> I haven't tried finding what is wrong with your code, given other ways
> to do it.
>
> The correct spelling is "Stata". Please do read the FAQ carefully
> before posting, as requested.
>
> Nick
>
> On Sat, Feb 23, 2013 at 4:31 AM, Jenny Yu <jennyyu@upei.ca> wrote:
>
>> I have the following STATA code for extracting the maximum number of the
>> categories for each variable in my varlist:
>>
>>
>> use "h:\mydata.dta", clear
>>
>>
>> tempname memhold
>> postfile `memhold' str20 yvar maxm1  using temp, replace
>>
>>
>> foreach yvar of varlist * {
>>
>>
>> capture tab `yvar', matcell(m)
>> svmat m
>> egen maxm1=max(m1)
>>
>>
>> post `memhold' ("`yvar'")  (maxm1)
>> drop m1 maxm1
>> }
>>
>>
>> postclose `memhold'
>>
>>
>>
>> If I open stata and run this do file. It does not run and error message
>> is: "matrix m not found" "r(111);"
>> However, after this error warning, if I type(notes: var1 is any of the
>> variables in my varlist):
>>  tab var1, matcell(m)
>>  svmat m
>>
>>
>> Not only these two lines are fine, but also this time, the above whole
>> postfile works perfectly.  But as soon as I close stata and open stata
>> again, run the do file, everything above repeats.
>>
>>
>> Does anyone know why I have to use "tab var1, matcell(m); svmat m;"
>> first, and then the whole postfile works just fine? Or there are some
>> other tricks here?
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index