Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Looping over two tables

    Dear group,

    I have a question about looping over two tables. In my first table, I have a list of individuals for which I have the age, country of residence, gender and year of interview (I've inserted the first five observations below). In the second table, I have the official retirement ages for each year, country and gender. (I've inserted 5 retirement ages below)
    In the first table, I created a variable called official retirement age.
    What I have to do is to go through each individual in the first table and see if for the 4 variables (age, country, gender and year) there is a match with the second table. If yes, then I want to take the official retirement age from the second table and put it into the retirement age variable for that individual in the first table.
    I understand that I have to do 2 loops, one that loops over the individuals and another one inside this loop that loops over the variables and if they match in the two tables, take the retirement age from the second table to the first table. Unfortunately, I don't know how to write this in code.
    Could anyone help me with it?

    Thank you in advance,
    Rita Samudra
    Attached Files
    Last edited by Rita Samudra; 20 Mar 2022, 15:15.

  • #2
    You don't need loops, you need to merge the two datasets.
    Code:
    help merge
    For your merge to work, the variable names need to be the same in both datasets, and the variables need to be coded the same. For example, the gender variables should either both be "Gender" or both be "gender" and the the variables should be coded the same way. That is, if gender is a string variable, then either "male" or "Male" should be used in both datasets; if gender is a numeric variable with value labels, then male should have the same code in both datasets.

    Comment


    • #3
      Dear William Lisowski,

      You mean append the two datasets right?

      I appended the two datasets, and then run this code:

      replace retirementage=retirementage[_n] if age==age[_n] & interviewyear==interviewyear[_n] & female==female[_n] & country==country[_n]

      but there was no change in my retirementage varible, for the individuals it still gave a missing value. Do you know what did I do wrong here?

      Comment


      • #4
        You mean append the two datasets right?
        I suggested using the merge command to merge the two datasets. To append two datasets means using the append command, which I did not suggest.

        Comment

        Working...
        X