Announcement

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

  • generating a variable which is a count if of other variables

    Dear Statalisters,

    I am trying to count the number of children falling in certain age categories.
    The age is provided by the year of birth of each child: i.e q7210y_1 for child 1 , q7210y_2 for child 2 until child 17.

    It was easy to do it for children younger three years of age or younger as below.

    Code:
    *Number of children three years old or younger 
    gen nochild3age=0  
    foreach x of varlist q7210y* {
    replace nochild3age =1  if `x'==2012  | `x'==2011  | `x'==2010 
    replace nochild3age =3  if `x'==2012  & `x'==2011  & `x'==2010
    replace nochild3age =2  if `x'==2012  & `x'==2011 
    replace nochild3age =2  if `x'==2011  & `x'==2010  
    }
    lab var nochild3age "number of children aged three years old or younger. Year of birth of children is 2012,2011 or 2010"
    yet, it would be harder when trying to identify the number of children older than 6 years old. So, I tried to automate it as below but something went wrong:

    Code:
    *No of children older than 3 and less than 7 
    gen nochild4to6age=0
    foreach x of varlist q7210y* {
    bysort indid: count if `x'>2006 & `x'<2010 
    local counting=r(N)
    replace nochild4to6age = `counting'
    }
    Could you please guide me to a solution to this issue?

    Best regards,
    Maye

  • #2
    I think we need to see an example of your data (FAQ Advice #12 applies as always).

    You imply that your first block of code worked, but it's mostly legal but unlikely to be what you want. In particular, you have three lines like this

    Code:
     replace nochild3age =3  if `x'==2012  & `x'==2011  & `x'==2010
    So that replaces a variable with 3 if it's true that some other variable is at once 2011 and 2012 and 2013. That's never going to be true any more than the current year can be 2018 and also 2017 and also 2019 at the same instant.

    Now, let's guess that & is a typo for | there and you meant something more like this:

    Code:
    gen nochild3age=0  
    foreach x of varlist q7210y* {
        replace nochild3age =1  if `x'==2012  | `x'==2011  | `x'==2010
        replace nochild3age =3  if `x'==2012  | `x'==2011  | `x'==2010
        replace nochild3age =2  if `x'==2012  | `x'==2011
        replace nochild3age =2  if `x'==2011  | `x'==2010  
    }
    Again, that's legal but still unlikely to be what you want. That collapses to

    Code:
    gen nochild3age=0  
    foreach x of varlist q7210y* {
        replace nochild3age =1  if `x'==2012  | `x'==2011  | `x'==2010
        replace nochild3age =3  if `x'==2012  | `x'==2011  | `x'==2010
        replace nochild3age =2  if `x'==2012  | `x'==2011  | `x'==2010
    }
    and then we notice that you're just overwriting values and might as well write

    Code:
    gen nochild3age=0  
    foreach x of varlist q7210y* {
         replace nochild3age =2  if `x'==2012  | `x'==2011  | `x' ==2010
    }
    but that's still unclear because the loop over variables doesn't make obvious sense (at least to me).

    There are serious problems with your second block of code too, but I will stop there. I don't think you'll get good advice unless you give an example of your data.

    Short version: Your code is seriously confused.








    Comment


    • #3
      Same as Nick, I find your description and code are unclear. Anyhow, with a wild guess, I expect the below code would give you a hint.
      Code:
      egen nochild1to3age=anycount(q7210y_*), v(2010 2011 2012)
      egen nochild4to6age=anycount(q7210y_*), v(2007 2008 2009)
      Given the fact that you have had 47 posts so far, please kindly notice to use -dataex- to give small example for future posts.

      Comment

      Working...
      X