Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Macro and Loop


From   "Austin Nichols" <[email protected]>
To   [email protected]
Subject   Re: st: Macro and Loop
Date   Mon, 8 Sep 2008 13:42:01 -0400

Ying Huang <[email protected]>:
There are a number of ways of doing this, as I hinted unsuccessfully.
Here are two:

clear
set obs 1000
g byte month=1+mod(_n,12)
forv i=1/24 {
 g v`i'=uniform()<.2
 }
g m=.
forv i=1/12 {
 egen next12_`i'=rowmax(v`=`i'+1'-v`=`i'+12')
 qui replace m=next12_`i' if month==`i'
 drop next12_`i'
 }
g i=_n
qui reshape long v, i(i)
egen m2=max(v==1 & inrange(_j,month+1,month+12)), by(i)
qui reshape wide v, i(i)
tab m2 m

In the "long" format of data, using by: is often faster than -egen-
but you may have to -reshape- twice to use it.

On Mon, Sep 8, 2008 at 1:13 PM, Ying Huang <[email protected]> wrote:
> Maarten buis wrote:
>>
>> --- Ying Huang <[email protected]> wrote:
>>
>>>
>>> I have 24 indicator variables (1/0) to indicate insurance coverage in
>>> 24 monthes. For example, if the survey is in 1997, then the survey
>>> month refer to Jan to Dec of 1997, and those 24 variables show
>>> insurance coverage for each month in 97 and 98.
>>>
>>> The variable I want to create is again a 1/0 variable, say call it
>>> "insurance". Starting from the survey month, if any of the following
>>> 12 insurance coverage indicators = yes, then insurance =1; otherwise
>>> insurance =0.
>>>
>>
>> say that the indicotor variables are called insured1 till insured24,
>> then I would type:
>> egen insured = rowtotal(insured1-insured12)
>> replace insured = insured >0 if insured < .
>>
>> You may need to think a bit on how you treat missing values (-rowtotal-
>> treats a missing value by default as 0)
>>
>> Hope this helps,
>>
>> Maarten
>>
>>
>
> Maarten,
> Thanks a lot for your reply, but what I want to know is not if someone had
> coverage for any of all those 24 months. What I want to know is if someone
> had coverage from Aug 97 to Aug 98 if he was surveyed in Aug 97. For another
> person, the period may go from Nov 97 to Nov 98. The 12-month period varies
> from person to person.
*
*   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/



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