Announcement

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

  • Working with dates: a loop to identify a time period from "YYYY" to "YYYY-MM-DD"

    Dear Statalist users,

    I have a panel dataset of students who participate in various activities as part of a university outreach programme. The timing that each activity takes place is reported in the following format: YYYY-MM-DD.

    The students are also asked to report their intentions to apply at university at the beginning of each academic year. I want to look at the impact of activity participation on changes in students' intentions, considering the activities that happened between the first and last time each student reported their intentions.

    To do this I created a variable which identifies the year each student reported their intentions for the first time (first_time) and another variable identifying the year they reported for the last time (last_time).

    To identify activities that happened before a specific date I could use the following code:

    Code:
    g ddate = daily(startdate, "YMD")
    format %td ddate
    
    g before = cond(ddate < td(01sep2021), 1, 0) if !missing(ddate)
    What I would like to ask your help for is, if possible, to write a loop that would use the years in "first_time" and "last_time" and identify the activities that happened in the period in between.
    For example, consider a student whose first_time=2017 and last_time=2020, I want to select the activities that happened between 01sep2017 and 01sep2020.

    I hope that my explanation is clear and I honestly apologise if it's not. Any help would be beyond appreciated, thank you!

  • #2
    No data example, but you probably do not need a loop for whatever you want to do. Create two date variables and then use the -inrange()- function and the -by- prefix, see

    Code:
    help inrange
    help by
    Code:
    g startdate= mdy(9, 1, first_time)
    g enddate= mdy(9, 1, last_time)
    format startdate enddate %td
    then, e.g., list observations within start and end dates

    Code:
    sort id date
    by id: list if inrange(date, startdate, enddate), sepby(id)

    Comment


    • #3
      This is exactly what I needed and much simpler than what I had in mind, thank you!

      Comment

      Working...
      X