Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: two questions about matrix operations


From   "Katsuhide Isa" <[email protected]>
To   [email protected]
Subject   st: Re: two questions about matrix operations
Date   Fri, 07 Jan 2005 22:03:39 +0900

Dear David and Ulrich,

Thank you for your advice.

You're definitely right.
I'm sorry I didn't include relevant statements, and I'm 
impressed by your insight at the same time.

The original ones are: 

foreach file in mat1982 mat1983 mat1984{
	use `file'
	foreach var of varlist A-D{
		count if `var' == 0
		local i = r(N)
		local sum = `sum' + `i'
	}
	di `sum'
}

Things went well after modifying the 2nd line as 
-use `file', clear-
Actually, I tried including -clear- in the loop, but at the 
wrong place: just after -di `sum'-. 
And that provoked another error message :-).


And the solution suggested by Ulrich,

>local sum 0
>foreach var of varlist A-D {
>	count if `var' == 0
>	local sum = `sum' + r(N)
>}
>di `sum'

is surely nicer than mine.
My solution, while ending without issuing no error messages,
produces some unwanted results.
That is, the cumulative sum for all the files are iteratively 
calulated and shown, while Ulrich's return sums for each file 
separately, and that's what I wanted.

I guess this is because `sum' is not initialized in each round 
of the loop in mine, while it is in Ulrich's . 


I learned a lot from this experience.
Thank you again.


K.I.
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index