Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: RE: RE: RE: Writing the value of a variable in another variable in a selective way
From
Nick Cox <[email protected]>
To
[email protected]
Subject
Re: st: RE: RE: RE: Writing the value of a variable in another variable in a selective way
Date
Wed, 1 Jun 2011 20:27:18 +0100
My code loops over 20 subjects and within that loop over 60 periods.
As stated, my assumption was that partners are constant, so with that
assumption it suffices to look up partners within the data for period
1.
Nick
On Wed, Jun 1, 2011 at 6:49 PM, Mak, Timothy
<[email protected]> wrote:
> Or perhaps try this, if you've got your data in "long" format, i.e.:
>
> id period
> 1 1
> 1 2
> . .
> . .
> 1 60
> 2 1
> 2 2
> . .
> . .
> 20 60
>
> sort id
> gen originalsort = _n
> * This gives you the original sort
> sort partnerid
> gen partnerid_sort = _n
> sort id
> * This basically makes a new id that goes from 1,2,3,4, etc... And supposing your partner doesn't change over the period...
>
> forval i=1/`=c(N)' {
> local partnersort = partnerid_sort[`i']
> replace partnerprofit = profit[`partnersort'] in `i'
> }
>
> * which is the same code as before
>
> I haven't tried this out, so I don't know if it works, but I don't really understand Nick's code there, since it apparently only loops over 20 lines, which would be valid if it is in wide format, but the rest of the code doesn't seem to imply a wide format...
>
> Like Nick said, if you actually provide what the data look like, it'd be easier to help.
>
> Tim
>
> -----Original Message-----
> From: [email protected] [mailto:[email protected]] On Behalf Of Nick Cox
> Sent: 01 June 2011 12:53
> To: '[email protected]'
> Subject: RE: st: RE: RE: RE: Writing the value of a variable in another variable in a selective way
>
> In your code local macros are evaluated for particular observation numbers, which will be at best insufficient and at worst wrong for a panel dataset.
>
> You didn't explain your full problem in the previous email, and you just got advice for what you did explain. Here the problem report is just "doesn't work". Imagine that you write code for me and I report back "it doesn't work". How are you going to advise me? How much does my report tell you?
>
> I can guess what your data look like, but I'd rather see some real data, say the first 100 observations for -subject- -partner- -profit-.
>
> However, guessing at times 1...60 and subjects 1...20 and that subjects have the same partners throughout [sounds like a theorist's fantasy] I imagine code like
>
> sort period subject
> gen partnerprofit = .
>
> forval s = 1/20 {
> local partner = partner[`s']
> forval p = 1/60 {
> su profit if period == `p' & subject == `partner', meanonly
> replace partnerprofit = r(min) if period == `p' & subject == `s'
> }
> }
>
> As said, you may need to tell us much more about the data if that's not helpful.
>
> Nick
> [email protected]
>
> [email protected]
>
> After 2 hours of unsuccesful trying on my own let me ask again please,
> this time more precisely:
>
> I have n==20 "subjects", each with entries for t==60 periods. Each
> subject has a unique "partner". I would like to stata to write the value
> of the variable "profit" of subject i's partner into subject i's
> variable "partnerprofit", for each period.
>
> I tried the code below, but it doesn't work.
>
> forval subject =1/20 {
>
> forval period =1/60 {
> local partner = partner[`subject']
> replace partnerprofit = profit[`partner'] in `period'
> }
> }
> }
>
>
> Thank you very much again!
> Ozgur
>
>
>
> Nick Cox schrieb:
>> Tim is correct; thanks!
>>
>> Nick
>> [email protected]
>>
>> Mak, Timothy
>>
>> I think Nick meant:
>>
>> gen partnerprofit = profit[5] in 3
>>
>> But following this logic, and if your dataset is not large, you could do it in a loop, I think:
>>
>> gen partnerprofit = .
>> forval i=1/`=c(N)' {
>>
>> local partnerid = partnerid[`i']
>> replace partnerprofit = profit[`partnerid'] in `i'
>>
>> }
>>
>> where I assume you have a partnerid variable which gives you the observation number of the partner.
>>
>> If your id's do not go in 1,2,3,...,n, however, you may need to do a bit more programming... and probably -merge- is the easiest.
>>
>> Nick Cox
>>
>> If the problem is just this, the solution would be
>>
>> gen partnerprofit = profit[5] in 13
>>
>> In general, think about a -merge-.
>>
>> On Behalf Of [email protected]
>>
>> How can I write the value of the variable called profit for e.g.,
>> subject i as the value into another variable called "partnerprofit" of
>> another subject j?
>>
>> For example: I am subject 3, my partner is subject 5; his profit is 13,
>> now I would like to stata write in my "partnerprofit" the value 13.
>>
>
*
* 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/