Q:

Generate a new variable from row conditions in Stata

I am using Stata13 on Windows 7. I have a dataset with repeated observations of age and educ in a row for each id. i.e. variables q9p1educ and q9p1age is the education and age for person1 respectively , q9p2educ and q9p2age is the education and age for person2 respectively etc. I want to extract the education level of the person with the highest age. I have managed to extract the maximum age maxage using egen maxage = rowmax(q9p1age - q9p9age) How can I get the education of the person with the maximum age? 
The sample data is here

A:

I would start by reshaping your data into long format
reshape long q9@educ q9@age, i(id maxage) j(pid) string

Then the answer depends on What do you want to do if the maxage is not unique. Perhaps you could do something like average them?
bysort id (age): gen temp=q9educ if age==maxage
bysort id: egen educmaxage=mean(temp)
drop temp

Then if you want it wide again, you could simply reshape wide.
reshape wide q9@educ q9@age, i(id maxage educmaxage) j(pid) string

