Good day to all,
I am relatively new to Stata and need to write a long code of if-conditions for my research. My goal is to create a new variable (PLBTt3 for example) and populate it with values that are within the group, but outside of the explicit observation.
I'll share my code with you first, then hopefully it will be more understandable:
bysort NAME: gen PLBTt3 = 0
bysort NAME: gen PLBTt2 = 0
bysort NAME: gen PLBTt1 = 0
sort NAME year
by NAME: replace PLBTt3= PLBT[year==1]+1000000 if (PLBT[year==1]<-1000000)&(PLBT[year==2]>1000000)
by NAME: replace PLBTt2= PLBT[year==2]-1000000 if (PLBT[year==1]<-1000000)&(PLBT[year==2]>1000000)
.
.
.
I want to tell STATA which observation within the group I want to reference with {year==1}(e.g.). However, Stata does not recognize this "{year==1}" command correctly because, I do not get an error message, but data is "Replaced" even though the conditions are not met.
Now to my question: Did I miss something? Is {year==1} not the "correct" command to tell STATA the correct row?
PS. I have been looking for a solution in the forum for a long time, however I had trouble making my search command fit. I also tried to torture ChatGPT for a long time, but it was no use...
Many thanks in advance,
Lenny
I am relatively new to Stata and need to write a long code of if-conditions for my research. My goal is to create a new variable (PLBTt3 for example) and populate it with values that are within the group, but outside of the explicit observation.
I'll share my code with you first, then hopefully it will be more understandable:
bysort NAME: gen PLBTt3 = 0
bysort NAME: gen PLBTt2 = 0
bysort NAME: gen PLBTt1 = 0
sort NAME year
by NAME: replace PLBTt3= PLBT[year==1]+1000000 if (PLBT[year==1]<-1000000)&(PLBT[year==2]>1000000)
by NAME: replace PLBTt2= PLBT[year==2]-1000000 if (PLBT[year==1]<-1000000)&(PLBT[year==2]>1000000)
.
.
.
I want to tell STATA which observation within the group I want to reference with {year==1}(e.g.). However, Stata does not recognize this "{year==1}" command correctly because, I do not get an error message, but data is "Replaced" even though the conditions are not met.
Now to my question: Did I miss something? Is {year==1} not the "correct" command to tell STATA the correct row?
PS. I have been looking for a solution in the forum for a long time, however I had trouble making my search command fit. I also tried to torture ChatGPT for a long time, but it was no use...
Many thanks in advance,
Lenny
Comment