Hi,
I am new in stata, so I am doing long commands for a simple variable generating. I want to shorten the below commands. As you see only the numbers after "p" are descending rtr7p(8,7,6,5,4,3,2,1)e1by and rtr6p(8,7,6,5,4,3,2,1). I have to take values from that specific variable below exactly in the same order, not from the beginning but from the last one up to the first one. Do you have any suggestions for shortening these kinds of repetition? Thank you in advance

generate yearcp=rtr7p8e1by if rtr6p8==1
replace yearcp=rtr7p7e1by if rtr6p7==1
replace yearcp=rtr7p6e1by if rtr6p6==1
replace yearcp=rtr7p5e1by if rtr6p5==1
replace yearcp=rtr7p4e1by if rtr6p4==1
replace yearcp=rtr7p3e1by if rtr6p3==1
replace yearcp=rtr7p2e1by if rtr6p2==1
replace yearcp=rtr7p1e1by if rtr6p1==1