I would check whether -statsby- can be used
for this purpose.
In your code, your call to -egen- is illegal, and
indeed would be unnecessary if it were legal.
That is probably where your code fails first.
(In reporting a problem, it never hurts to say
precisely where code fails, instead of saying it
"doesn't seem to do the trick".)
On the law: The syntax of -egen- always
includes a call to an -egen- function to
the right of the assignment sign =.
On necessity: You are trying to create
a variable to hold a constant. Outside graphics,
when this can be a way to solve a problem,
this is almost never necessary in Stata.
I would tweak your code in the following
directions. But, as said, check out -statsby- too.
use "dataset"
gen year = 1994 + _n in 1/10
gen arscore = .
qui forvalues i = 1995/2004 {
roctab default1 rating if cohort==d(01jan`i'), lorenz summary
replace arscore = r(gini) if year == `i'
}
l year arscore
summarize arscore
Nick
[email protected]
David T. Hamilton
> I have a data set that consists of cohorts of firms, their
> ratings, and
> their default status (so there are three fields: cohort, default1,
> rating). I am trying to loop over each cohort and take the average of
> the cohort gini scores using roctab rather than pooling the cohort
> data. The problem is that commands that make looping easy are not
> available with roctab (e.g. by, if).
>
> The code I've written doesn't seem to do the trick. I would appreciate
> any pointers the Stata community could provide. Many thanks.
> Here's my code:
>
> use "dataset"
> preserve
>
> mat A=J(10,1,.)
> forvalues i=1/10 {
> egen j=`i'+1994
> keep if cohort==d(01jan`j')
> quietly roctab default1 rating, lorenz summary
> mat A[`i',1]=r(gini)
> restore, preserve
> }
>
> svmat A, name(arscore)
>
> summarize arscore
*
* 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/