Dear Statalist, I need your help again.
Is there a way to iterate over variables that have an index in the name? Using _i or something similar?
I know foreach var but this is a slightly more complicated problem.
I have a list of variables S1 through S7, each indexed from 1 to 10 by the household member number. Eg: S1_01, S1_02...S1_10, S2_01 etc etc
S1* stands for the name of the respondent (John, Mary, etc.)
In another variable resp_name I have the name of the chosen respondent (John)
I want to create new variables S1_resp-S7_resp in which to copy the values of the S1-S10 responses for the chosen respondent.
Eg: gen S2_resp = S2_01 if S1_01=resp_name
But without copying this 70 times. There has to be a smarter way.
Foreach var iterates only over one variable, but I need to iterate over two at the same time (S1 and one of the others)
Any ideas?
Thank you very much.
Is there a way to iterate over variables that have an index in the name? Using _i or something similar?
I know foreach var but this is a slightly more complicated problem.
I have a list of variables S1 through S7, each indexed from 1 to 10 by the household member number. Eg: S1_01, S1_02...S1_10, S2_01 etc etc
S1* stands for the name of the respondent (John, Mary, etc.)
In another variable resp_name I have the name of the chosen respondent (John)
I want to create new variables S1_resp-S7_resp in which to copy the values of the S1-S10 responses for the chosen respondent.
Eg: gen S2_resp = S2_01 if S1_01=resp_name
But without copying this 70 times. There has to be a smarter way.
Foreach var iterates only over one variable, but I need to iterate over two at the same time (S1 and one of the others)
Any ideas?
Thank you very much.
Comment