Hi everyone,

I'm having a long panel data set with 1250 individuals (identified by case) and information about their entrance into the labor market. The variable shown below "nummer_eintritt" shows, if it was the first, second, third, etc. entrance. I have a third variable "SES_" (I cannot display due to data privacy) that I now want to analyse.
For each individual, I want to generate a new variable that contains the value of SES_[_n-1] if nummer_eintritt[_n]==1, if the SES_[_n-1] is not missing. If it is missing, it should display the value of SES_ for _n-2 and so on.

I tried to get it by putting lots of conditions together, but it would end in a very high number of conditions because sometimes you have to go back 20 observations to find a non missing value.
Could you please help me on that?

Thank you and best regards
Marie

clear
input long case float nummer_eintritt
681 0
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 1
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
681 .
end
[/CODE]