Hello there,
I have two variables, income of individuals from year 1968 to 2017 and their ages for the same years. I want to create a variable which indicates income for specific age. Like income of each individual at the age of 40. I wrote the following loop but it assigned 2017's income for all ages.
generate labory40=.
foreach x of varlist labory1968-labory2017{
foreach y of varlist age1968-age2017{
replace labory40=`x' if `y'==40
}
}
generate labory41=.
foreach x of varlist labory1968-labory2017{
foreach y of varlist age1968-age2017{
replace labory41=`x' if `y'==41
}
}
.
.
.
Any help is highly appreciated,
Related Posts with foreach loop
How to remove the invisible space in the tail of a string in Stata?Hello, I have a small dataset below, clear input str80 No str90 Project "F-000508" "Hermann Park " "…
cummulative changeArray Dear listers, I would like to apologize if my question is too naive. I am using Stata 15. I w…
Condition command stataI want to create a new variable (ageT) with the -cond- programming function. Is there any way I effi…
How do I fill in missing values using the highest/last observation-year?Hello everyone, I would like to replace the missing values (leftceo) by the last given observation-…
How to generate a new variable based on conditions for values of two other variablesHello! I have the following dataset and I'm trying to create a new var X which should have the same …
Subscribe to:
Post Comments (Atom)
0 Response to foreach loop
Post a Comment