Announcement

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

  • loop for multiple variables

    Hi everyone,

    I hope you are all doing well and safe. I have a question about entering my command in a loop for 2 different variable.
    I have a data set that investigates parents and children. Parents are the respondents and they are giving info about their children. So parents are located in rows and children info located in column section. I will demonstrate the data structure below:

    Parents w6_ch_occupation_1 w6_ch_occupation_2 w6_ch_occupation_3 ...... this variable indicates the occupation of child number one to child number 20
    1 5 2 1
    2 1 3 .
    3 2 4 3

    My question is for each child variable: I have to recode occupation variable as employed and not employed and I would like to drop children who are born in 1999 or later because these children are not relevant for my research. My coding is as follows:

    foreach var of varlist w6_ch_occupation_1- w6_ch_occupation_20 {
    replace `var' = . if `var'==-2 | `var'==-1 | `var'==97
    replace `var' = . if `w6_ch_yrbirth_1'>=1999
    replace `var' = 1 if `var'==1 | `var'==2 | `var'==3
    replace `var' = 0 if `var'==-9 | `var'==4 | `var'==5 | `var'==6 | `var'==7 | `var'==8 | `var'==9
    }

    however, Stata gives error for replace `var' = . if `w6_ch_yrbirth_1'>=1999. Do any of you know how to implement these 2 commands in a loop. Or is there an easy way to drop all children who are born in 1999 or older? Please keep in mind that i need to do this for all children related variables (e.g. marital status, education, gender etc.)

    Thanks a lot for your help.
    Kind regards,
    Martin








  • #2
    You don't tell us the exact error message, but the code you highlight suggests a possible source of the error. Instead of
    Code:
     replace `var' = . if `w6_ch_yrbirth_1'>=1999
    you probably meant
    Code:
    replace `var' = . if w6_ch_yrbirth_1>=1999
    unless you have defined somewhere else a local macro with the name w6_ch_yrbirth_1.
    For dropping all the children, have you tried
    Code:
    drop if w6_ch_yrbirth_1>=1999

    Comment


    • #3
      I tried out both codes
      replace `var' = . if `w6_ch_yrbirth_1'>=1999
      and
      replace `var' = . if w6_ch_yrbirth_1>=1999 in case one of them works. What I want to do is that I would like to make all children related variable (gender, occupation, marital status etc.) missing if children were born in 1999 or later. Do you know a code for this?

      Comment


      • #4
        What I want to do is that I would like to make all children related variable (gender, occupation, marital status etc.) missing if children were born in 1999 or later. Do you know a code for this?
        I don't understand your question. If I understand your code correctly, then you are already achieving what you want to do with your existing code. Or are you asking for code which achieves your goal in one line?
        You could provide a data example, so I can understand your problem bettter.

        Comment


        • #5
          Hi Sven,

          I attached how my data look. So I have information about respondents and their children. Respondents can have max. 20 children (they usually have less). Therefore, I have 20 variables for gender of children (gender of child#1, gender of child#2 .... gender of child#20 or occupation of child#1 to occupation of child#20).

          I am interested in children who were born 1998 or earlier so I would like either to drop all children who were born in 1999 or later or recode them as missing. The problem is, I also would like to have children related variables to be missing for those who were born in 1999 or later. Is it a bit clear?

          Thanks in advance for your answer
          Attached Files

          Comment


          • #6
            I think that it is clear to me what you want, but I don't understand what your problem with your code is. Your code already sets every variable to missing if the birth year is greater or equal to 1999.
            You could run the dataex-command before and after using your code, post the resulting datasets here and then point out what exactly your existing code does not do what you want it to do.

            Comment

            Working...
            X