Announcement

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

  • Creating a variable that satisfies 'Once a week' condition

    Hello,

    I am trying to see if there is an observation for a form entered 'Once a week' from x date and am wondering how to code this?
    For example, I want to see if there is a form entered once a week from Jan 01 2021 up to present week and create an indicator variable where it is =1 if there is a form entered weekly. I'm just not sure how to go about doing this and could use some help. Thank you!


  • #2
    What do you mean by "a week"?
    • The period from Sunday through the following Saturday?
    • Since January 1, 2021 was a Friday, the period from Friday through the following Thursday?
    • If a form was entered between January 1 and January 7, 2021, the period starting on that of the week and continuing through the next six days?
    • Something else?

    Comment


    • #3
      My apologies for not being clear.
      By once a week I meant in line with your 3rd point. So if a form was entered between Jan 1 and Jan 7 2021, then is there another form entered between Jan 8 and Jan 14, then again between Jan 15 and Jan 21, and so on.

      Comment


      • #4
        To increase your chances of obtaining a useful reply, you should provide a data example as recommended in FAQ Advice #12. Does once a week mean exactly once a week or at least once a week? The -ceiling()- and -floor()- functions can be helpful here.

        Code:
        help ceil()
        help floor()
        Here is an example:

        Code:
        clear
        input float(formid date)
        1 22281
        1 22289
        1 22297
        2 22281
        2 22283
        2 22290
        3 22281
        3 22291
        3 22299
        end
        format date %td
        
        gen weekid= ceil((date-`=td(31dec2020)')/7)
        *EXACTLY ONCE A WEEK
        isid form weekid
        bys form (weekid): gen wanted1= _N==(weekid[_N]- weekid[1] +1)
        *AT LEAST ONCE A WEEK
        egen tag= tag(form weekid)
        bys form weekid tag: gen wanted2= _N==(weekid[_N]- weekid[1] +1) if tag
        bys form weekid (tag): replace wanted2= wanted2[_N]
        Res.:

        Code:
        . drop tag
        
        . sort formid date
        
        . l, sepby(formid)
        
             +-------------------------------------------------+
             | formid        date   weekid   wanted1   wanted2 |
             |-------------------------------------------------|
          1. |      1   01jan2021        1         1         1 |
          2. |      1   09jan2021        2         1         1 |
          3. |      1   17jan2021        3         1         1 |
             |-------------------------------------------------|
          4. |      2   01jan2021        1         0         1 |
          5. |      2   03jan2021        1         0         1 |
          6. |      2   10jan2021        2         0         1 |
             |-------------------------------------------------|
          7. |      3   01jan2021        1         1         1 |
          8. |      3   11jan2021        2         1         1 |
          9. |      3   19jan2021        3         1         1 |
             +-------------------------------------------------+

        Comment


        • #5
          For "at least once a week" in #4, the week identifier should not be part of the grouping variables.

          Code:
          clear
          input float(formid date)
          1 22281
          1 22289
          1 22297
          2 22281
          2 22283
          2 22290
          3 22281
          3 22291
          3 22299
          end
          format date %td
          
          
          gen weekid= ceil((date-`=td(31dec2020)')/7)
          *AT LEAST ONCE A WEEK
          egen tag= tag(form weekid)
          bys form tag (weekid): gen wanted2= _N==(weekid[_N]- weekid[1] +1) if tag
          bys form weekid (tag): replace wanted2= wanted2[_N]
          Last edited by Andrew Musau; 30 Apr 2022, 09:58.

          Comment

          Working...
          X