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

st: RE: Generate new variable "by" question


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Generate new variable "by" question
Date   Mon, 22 Jul 2002 13:15:17 +0100

Dale Steele
> 
> minus the value of response when resistnc is 0.  Seems like 
> I should use the
> "by" command, but I'm having a hard time creating working 
> code. My efforts
> thus far ...
> 
>  by idnum: generate cresponse = response - (the value for 
> response given
> resistnc = 0 for each idnum)
> 
> . list idnum resistnc response
> 
>          idnum   resistnc   response
>   1.       401          0        .14
>   2.       401        2.5       2.42
>   3.       401          5       3.76
>   4.       401         10       4.74
>   5.       401         20       7.88
>   6.       401         30       8.52
>   7.       402          0         .4
>   8.       402        2.5       1.46
>   9.       402          5       1.58
>  10.       402         10       4.08
>  11.       402         20          6
>  12.       402         30       8.94
> 

What reached me seemed to have lost at least one line, 
but from this sample it seems that 

bysort idnum (resistnc) : gen cresponse = response - response[1] 

is sufficient to get what you what you want, as that puts 
-resistnc- in order within -idnum-, and so -response[1]- 
is then the response when -resistnc- is 0.  

If -resistnc- were ever negative, you could do something 
like this: 

gen iszero = resistnc == 0 
bysort idnum (iszero) : gen cresponse = response - response[_N] 


Nick 
n.j.cox@durham.ac.uk 

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index