See also the generic
Is there a way to tell Stata to try all values of a particular variable
in a foreach statement without specifying them?
http://www.stata.com/support/faqs/data/foreach.html
Nick
[email protected]
Martin Weiss
You do not have to -set obs- in any way, as _you_ have the data that you
want to use. I do not, so I had to make them up to test ideas for you.
Anyway, the -macro length- is also exceeded on my machine when I try to
work
with 128040 individuals (via -set obs 128040- at the top of the do
file).
But there is an easier solution: Omit the -levelsof- line which causes
the
error. Set up the loop as
*******
qui forvalues id=1/`=_N'{
...
drop `touse'
}
*******
which lets it run through all the rows of your dataset, one after
another.
This is of course a time intensive procedure, but it is a way to
circumvent
the limitations imposed by the macro length. It only works if the
-personid-
runs from 1 to 128040, i.e. no gaps in there. -levelsof- would have put
the
levels of the variable into the macro, that is why I went for it in the
first place...
Vincenzo Carrieri
I have tried to run the loop one statalister suggested me to assign
the mean of income calculated to the person with
same education, same region, same activity and falling in an age
interval. The loop suggested me is the following:
clear*
set obs 1000
gen personid=_n
gen byte region= irecode(_n,0,1000,5000,10000)
gen income= rnormal(0,100)+1500
gen byte education=9+int(8*runiform())
gen byte activity=int(10*runiform())
gen age=20+int(10*runiform())
//generate var to be filled later on
gen meanincome=.
//also count the number of "peers"
gen count=.
qui levelsof personid
qui foreach id of numlist `r(levels)'{
tempvar touse
gen byte `touse'=region==region[`id'] & /*
*/ education==education[`id'] & /*
*/ activity==activity[`id'] & /*
*/ inrange(age, age[`id']-5, age[`id']+5)
su income if `touse', mean
replace meanincome=r(mean) if `touse'
replace count=r(N) if `touse'
}
The problem is that I have 128040 individuals and I cannot set obs
1000 as the statalister suggested me ( stata gives me this error:
obs must be between 128040 and 1060356). then If I skip to set obs and
I work with all my 128040 individuals, stata says me: macro length
exceeded
r(1000);
Ho can I solve this problem? is it because my data-set is too big? I
have set mem 800m and matsize 90, is it not enough?
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/