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: creating a table of own design


From   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: creating a table of own design
Date   Mon, 9 Dec 2013 12:06:00 +0000

Some details folow in a small example. I built up the following little
example in
a do-file.

program mytest
di "{col 10}z-stat"
di "{hline 16}"
di "a" "{col 10}1.2345"
di "b" "{col 10}1.1234"
end

There is most of a strategy here for your own tables. More complicated
tables are just more complicated, not different in principle. Most
tables boil down to header, body and footer. In practice, the body
might be built up in a loop and you might need to format numeric
results explicitly.

The help for -display- and the help for -smcl- are your friends.

There is no problem in thinking that your table is really a matrix,
and wanting to build that and then show it, except that Stata's
matrices can only be displayed with a single numeric format and are
limited in what can be shown as row and column labels. Working out
line by line what you want and emitting it with -display- is a more
flexible method. The code needn't be lengthy, because as said the body
will often be created with a loop.

Here's what the program produces.

. mytest
         z-stat
----------------
a        1.2345
b        1.1234

The line looks better in Stata than will appear in your mailer.
Nick
njcoxstata@gmail.com


On 9 December 2013 09:05, Nick Cox <njcoxstata@gmail.com> wrote:
> You seem to be in some doubt about
>
>  mat A = ("a"\"b")
>
> but that will produce an error message of -type mismatch- and the
> reason in turn is simple: Stata matrices are for numbers only and may
> not include strings.
>
> Here's how Stata introduces matrices at [U] 14.1.1
>
> "To Stata, a matrix is a named entity containing an r x c (0 < r <=
> matsize, 0 < c <=   matsize) rectangular  array of double-precision
> numbers (including missing values) that is bordered by a row and a
> column of names."
>
> Learning Stata programming is tricky, but made less tricky by
> approaching it the right way. Here's my RST method for learning Stata
> programming.
>
> Read the documentation. There's really no alternative. [U] contained
> the first and in many ways still one of the best treatments. (Your
> problem, as above, is explained directly in [U].) Kit Baum's book
> http://www.stata.com/bookstore/stata-programming-introduction/ is also
> well paced and well explained.
>
> Slow but sure. Learners often slap down several lines of code and then
> say, "Why isn't my program working?" Focus on one line at a time and
> think about getting each one right. If you are learning, ten lines of
> code can be very long, but Stata will stop at the first error,
> regardless of whether later code is good.
>
> Test, test, test. Stata's debugging environment is not spectacular,
> but it's more than enough to help progress. -set trace on- often
> helps. Most of the tests one needs are a matter of looking at the
> data, macros, scalars, matrices, to see what they are (or whether
> Stata can see them).
>
> To build up a table as you intend, I recommend simply using -display-
> to produce a line at a
> time.http://www.stata-journal.com/article.html?article=pr0053 may
> help.
>
> On "local variable", see
>
> http://www.stata.com/statalist/archive/2008-08/msg01258.html
>
> Nick
> njcoxstata@gmail.com
>
> On 9 December 2013 05:00, Jason Park <loooooo.j8311@gmail.com> wrote:
>
>> Not being perfectly familiar with the Stata's programing language, I'm
>> struggling doing some easy stuff that could easily be done in other
>> languages. Please forgive me if I'm asking about too basic a thing
>> that most of you already know.
>>
>> I got to the point where I can generate some figures that I need in a
>> do file, and now I want to tabulate them.
>> Suppose the local variables those figures are saved in are a and b.
>> My intention is to present a table looking like below (not necessarily
>> with the broken lines though or the header):
>>           z-stat
>> ------------------
>> a       1.2345
>> b       1.1234
>> ------------------
>>
>> So far, I tried to create a matrix containing strings, A and B, and
>> another matrix with the figures, and join side by side, which didn't
>> work anyhow.
>> The faulty code I wrote is:
>>      mat A = ("a"\"b")
>>      mat B = (`a'\`b')
>>      mat C = A,B
>>      mat list C
>> It seems that the first line is returning an error. Neither does A =
>> ("a"\"b") work.
>>
>> Could someone give me some tips? It will be greatly appreciated if
>> someone suggests a better way of tabulation, but if not, it will also
>> be equally appreciated if someone helps me get my code to work.
*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index