Hi there,

I have only recently started using Stata to edit data, instead of using the default datasets, and am struggling to use for loops and macros.

My specific issue here is that I am trying to analyse individual and household data, but have some missing individuals in households (I have both individual survey data and household survey data), and I need to see if there is a pattern to whose data is missing, such as age.

I have information on all of my individuals age, even the missing ones, from the household data, however it is not stored within the observation for the individual, but within the household overall (I will explain this more soon).

On top of this, each individual in a household has a respondent number rcode_indiv, which is unique within a household. What I need to do is assign respondent 2's age to their observation under the variable 'age', whereas at the moment it is everyone's observation under age_r2.

The format of my dataset is presented below, however I have hundreds of households and many many more variables.
rcode_indiv age personal_income age_r1 age_r2 age_r3 age_r4
1 41 3000 41 15 30 25
2 . . 41 15 30 25
3 . . 41 15 30 25
4 25 5000 41 15 30 25

The code I was trying to run to do that was the following.

gen age2 = 0
forvalues i= 1/`N'{
local x = rcode_ind[i]
replace age2[i] = age_r`x'[i]
}

However I keep on getting r198 invalid syntax, would anyone be able to point me in the right direction?

Thanks in advance.