Announcement

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

  • Counting times of query (but queries made in 30 days are regarded as 1)

    Hi.

    I am trying to count how many queries are made in a year,
    but queries made in 30 days are regarded as 1 time only.

    As shown in the following example, the last query is on 8/8,
    so queries made between 7/10-8/8(i.e., 7/21, 7/29, 8/2 and 8/8)
    are counted as only 1 time.

    Secondly, 4/3, 3/29 and 3/20 are counted as 1 time.

    Lastly, 2/19 is counted as 1 time.

    So the result is 3.

    How do I calculate such case by using -bysort-?

    query_date (from-to: 2013/1/1-2013/12/31)
    2013/2/19
    2013/3/20
    2013/3/29
    2013/4/3
    2013/7/21
    2013/7/29
    2013/8/2
    2013/8/8




  • #2
    Please note advice in http://www.statalist.org/forums/help#stata to use dataex (SSC) for data examples. Dates just presented as strings are awkward, as the engineering below indicates.

    panelthin (SSC) should help. If counting backwards is important, then negate the date and tsset in terms of a reversed date.

    Code:
    clear 
    input str9 s_query_date 
    "2013/2/19" 
    "2013/3/20" 
    "2013/3/29" 
    "2013/4/3"
    "2013/7/21"
    "2013/7/29"
    "2013/8/2"
    "2013/8/8"
    end 
    
    gen query_date = daily(s_query_date, "YMD") 
    format query_date %td 
    tsset query_date 
    
    * do this just once 
    ssc inst panelthin 
    
    panelthin, gen(wanted) min(30) 
    
    gen negdate = -query_date
    tsset negdate
    panelthin, min(30) gen(wanted2)
    
    l, sep(0)
    
         +----------------------------------------------------+
         | s_query~e   query_d~e   wanted   negdate   wanted2 |
         |----------------------------------------------------|
      1. |  2013/8/8   08aug2013        0    -19578         1 |
      2. |  2013/8/2   02aug2013        0    -19572         0 |
      3. | 2013/7/29   29jul2013        0    -19568         0 |
      4. | 2013/7/21   21jul2013        1    -19560         0 |
      5. |  2013/4/3   03apr2013        0    -19451         1 |
      6. | 2013/3/29   29mar2013        1    -19446         0 |
      7. | 2013/3/20   20mar2013        0    -19437         0 |
      8. | 2013/2/19   19feb2013        1    -19408         1 |
         +----------------------------------------------------+

    Comment

    Working...
    X