Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: Assigning values to a number of rows of a matrix

 From Nick Cox To "statalist@hsphsun2.harvard.edu" Subject Re: st: Assigning values to a number of rows of a matrix Date Mon, 24 Feb 2014 09:22:06 +0000

```internet exchanges and so is best avoided.

1. A loop over just two variables is somewhat over the top.

2. You cannot easily store an entire vector in a local macro. As it
happens, putting a vector into a macro only to take it out again is
quite unnecessary.

3. -xi:- is outdated unless you are using an old version of Stata (in
which case, as advised in the FAQ, you should be telling us that).

4. Unique values are those occurring precisely once. I suspect that
you are interested in the number of distinct values. -search distinct-
in Stata to get guidance on that. (StataCorp's use of terminology
could be improved here, in my view.)

5. But you don't need to know the number of rows or columns you are
adding to a matrix, which makes this kind of technique possible

sysuse auto, clear
xi: mean mpg i.rep78
mat b = e(b)
xi: mean mpg i.foreign
mat b = b , e(b)
mat li b

I don't know anything about .rtf.
Nick
njcoxstata@gmail.com

On 24 February 2014 05:14, Eilya Torshizian <e.torshizian@auckland.ac.nz> wrote:
> Dear Statalisters,
>
> In a loop, I would like to add values derived from a command (such as -mean-) to a matrix. Let's assume GENDER and MARITAL are categorical variables with 2 categories, which brings the total categories to 4 (CAT=4). I need to create a matrix with the size of the number of categories. However, I could not derive the total number of unique values by using -inspect-.
>
> *HOW TO DERIVE THE TOTAL UNIQUE VALUES FOR A VARLIST: global VARS GENDER MARITAL?
> matrix M = J(`CAT',2,0)
> local i = 1
> foreach VAR of varlist GENDER MARITAL {
> xi: mean i.`VAR'
> local MEAN = e(b)
> *HERE I DO NOT KNOW HOW TO SAY: MATRIX M[1:2,1] = MEAN[1:2,1]:
> matrix M[`i',1] = MEAN
> local i = `i' + `THE NUMBER OF CATEGORIES OF `VAR'' - 1
> }
>
> In the end, I would like to save the M matrix as an 'rtf' document.
>
> Thanks,
> Eilya.
>
>
> *
> *   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/

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