Announcement

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

  • forvalue loop

    The goal is to identify individuals for each year from 2005 to 2022 who had at least 3 years of school and at least 60 weeks of attendance in the 5-year period leading up to that year. I have school year indicator and weeks of attendance by individual for each year. I tried forvalue loop but did not work me. Any ideas will be much appreciated.

  • #2
    Please post back with example data using the -dataex- command so that someone can try to develop a solution to this problem. It is likely that -rangestat-, by Robert Picard, Nick Cox, and Roberto Ferrer, available from SSC, will be the key to the solution, but your data may require some transformation to get it ready for that. Without having an example of your data in hand, it isn't possible to show you appropriate code.

    In the future, when asking for help with code, always show example data. There are occasions where it is not necessary, but, frankly, if you really know whether it is necessary, you probably already know how to solve your problem! So always show example data--no harm done if it isn't strictly needed. And always use -dataex- to do that. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Also, clarify whether "the 5-year period leading up to that year" means that year plus the four preceding it, or excludes that year and consists of the five that precede it.

    Comment


    • #3

      ***Data example
      id year school_ind school_wks
      1 2015 0 0
      2 2016 0 0
      3 2001 0 0
      3 2004 0 0
      3 2012 1 6
      3 2015 1 8
      4 2018 0 0
      5 2008 0 0
      5 2020 0 0
      6 2010 0 0
      6 2020 1 24
      6 2020 0 0
      7 2013 1 29
      7 2016 1 5
      7 2017 0 0
      7 2018 1 29
      7 2019 0 0
      7 2020 1 18
      7 2020 0 0
      8 2018 0 0
      8 2009 0 0
      8 2017 1 18
      8 2021 1 0
      9 2010 0 0
      9 2012 0 0
      9 2016 1 9
      9 2017 1 2
      9 2018 1 9
      9 2019 1 1
      9 2021 0
      9 2022 0 0
      10 2015 0 0
      10 2017 1 0
      10 2001 0 0
      10 2001 1 22
      10 2003 1 5
      10 2004 1 33
      10 2005 1 26
      11 2005 0 0
      12 2020 1 7
      13 2013 0 0
      13 2014 1 9
      13 2007 1 26
      14 2005 1 23
      14 2007 1 16
      14 2009 0 0
      15 2021 1 0
      16 2018 1 0
      16 2020 1 0
      **** my code *****
      **********************
      local start_year = 2005
      local end_year = 2022

      * a variable to store the final results
      gen frequent = 0

      * Loop through each year in the specified range

      forvalues year = `start_year'/`end_year' {


      * Flag observations within the last 5 years from the current year
      gen in_last_5_years = (year >= `year' - 5) & (year >= `year')

      keep if in_last_5_years

      * Summarize data by individual
      by id (in_last_5_years), sort: egen total_school_ind = total(cond(in_last_5_years, school_ind, .))
      by id (in_last_5_years), sort: egen total_weeks = total(cond(in_last_5_years, wks, .))

      * Flag individuals who meet the criteria
      by id: gen eligible = 1 if total_school_ind >= 3 & total_weeks >= 60

      * Update the qualified flag
      replace frequent = 1 if eligible ==1

      drop in_last_5_years eligible total_school_ind total_weeks

      }
      Last edited by Saeed Kamyana; 07 Sep 2024, 19:10.

      Comment


      • #4
        You didn't clarify what you mean by "in the last 5 years." And I cannot infer that meaning from your code, because
        * Flag observations within the last 5 years from the current year
        gen in_last_5_years = (year >= `year' - 5) & (year >= `year')
        actually identifies all years that are the year of the observation or any year in its future!

        In the following code, I assume that "in the last 5 years" means the year of the observation or any of the 4 preceding years. If you intend it to exclude the year of the observation and include the preceding 5 years, then change -4 0 below to -5 -1.
        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input byte id int year byte(school_ind school_wks)
        1 2015 0 0
        2 2016 0 0
        3 2001 0 0
        3 2004 0 0
        3 2012 1 6
        3 2015 1 8
        4 2018 0 0
        5 2008 0 0
        5 2020 0 0
        6 2010 0 0
        6 2020 1 24
        6 2020 0 0
        7 2013 1 29
        7 2016 1 5
        7 2017 0 0
        7 2018 1 29
        7 2019 0 0
        7 2020 1 18
        7 2020 0 0
        8 2018 0 0
        8 2009 0 0
        8 2017 1 18
        8 2021 1 0
        9 2010 0 0
        9 2012 0 0
        9 2016 1 9
        9 2017 1 2
        9 2018 1 9
        9 2019 1 1
        9 2021 . 0
        9 2022 0 0
        10 2015 0 0
        10 2017 1 0
        10 2001 0 0
        10 2001 1 22
        10 2003 1 5
        10 2004 1 33
        10 2005 1 26
        11 2005 0 0
        12 2020 1 7
        13 2013 0 0
        13 2014 1 9
        13 2007 1 26
        14 2005 1 23
        14 2007 1 16
        14 2009 0 0
        15 2021 1 0
        16 2018 1 0
        16 2020 1 0
        end
        
        rangestat (sum) school_ind school_wks, by(id) interval(year -4 0)
        gen byte wanted = (school_ind_sum >= 3) & (school_wks_sum >= 60)
        You also did not use -dataex- to show your example data. In this case, that proved not to be a problem--it was very easy to import your -list- output to Stata. But that is often not true, and to be considerate of those who want to help you, you should always use -dataex- to show example data. Please refer back to #2 for information about accessing and using the -dataex- command.

        Comment


        • #5
          great, it worked!!
          Thank you for your assistance, much appreciated!! I apologize for not using the dataex command initially.

          Comment

          Working...
          X