I have a dataset that has repeated values of a variable measured at different times as indicated by a variable 'seqsij' which takes the values of 1,2,3...N for each new value of a variable sij_sp within a given ID. Each value of seqsij and sij_sp constitues a new row. I want to subtract the value of the LAST measure of sij_sp from the FIRST measure of sij_sp.
I tried the codes suggested by Nick Cox in response to a query by Roman Mostazir on 02Mar 2015; i.e.
bysort id (pre) : gen change = kimsob[_n] - kimsob[1]
but my code is
#
Code:
When I do this I get all missing values.
I also tried the suggestion of Klaudia Erhardt in response to the same query where Klaudia used
bysort id: gen change = cond(kinsob != kinsob[_n-1], kinsob - kinsob[_n-1],.)
but my code was
#
and this worked, but it does not give the FIRST - LAST value; it just provides the value of the difference between the current value and the immediately preceding one.
If I try
#
I get all missing values.
Where is my error?
Many thanks.
Don
I tried the codes suggested by Nick Cox in response to a query by Roman Mostazir on 02Mar 2015; i.e.
bysort id (pre) : gen change = kimsob[_n] - kimsob[1]
but my code is
#
Code:
bysort id (seqsij) :gen sijdif = sij_sp[_N] - sij_sp[1] #
When I do this I get all missing values.
I also tried the suggestion of Klaudia Erhardt in response to the same query where Klaudia used
bysort id: gen change = cond(kinsob != kinsob[_n-1], kinsob - kinsob[_n-1],.)
but my code was
#
Code:
bysort id (seqsij): gen sijdif =cond(sij_sp != sij_sp[_n-1], sij_sp - sij_sp[_n-1],.) #
If I try
#
Code:
bysort id (seqsij sij_sp): gen sijdif2 = cond(sij_sp[1]!= sij_sp[_N],sij_sp[1] - sij_sp[_N],.) #
Where is my error?
Many thanks.
Don
Comment