Announcement

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

  • Count how many calls within a time period

    Hi team,

    I have a question regarding counting how many calls within my data are within a time period (say, 3months). I have the dates of calls stored correctly in datecall, but what I want to do is count, for any datecall, how many datecalls are within 90 days of it. I have my code below. You can see that I put datecall* which does not work--is there another way to signal this to stata? Thanks!

    Code:
    gen numref_3m=0
    gen numref_12m=0
    foreach x of numlist 1/53 {
    replace numref_3m = numref_3m+1 if datecall`x'<= datecall* & datecall`x' - datecall* < abs(90)   & datecall`x'~=.   
    
    replace numref_12m = numref_12m+1 if datecall`x'<= datecall* & datecall`x' - datecall* < abs(365)   & datecall`x'~=. 
     
    }

  • #2
    Would this wrok??
    Code:
    foreach x of numlist 1/53 {
        local i = `x' +1
    replace numref_3m = numref_3m+1 if datecall`x'<= datecall`i' & datecall`x' - datecall`i' < abs(90)   & datecall`x'~=.   
    
    replace numref_12m = numref_12m+1 if datecall`x'<= datecall`i' & datecall`x' - datecall`i' < abs(365)   & datecall`x'~=. 
     local i = `i' +1
    }

    Comment


    • #3
      You will go nowhere slowly trying to do this with the data in wide layout. As you do not provide example data, I have mocked up a data set that I imagine resembles yours. Adapt this code as necessary
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float(id datecall1 datecall2 datecall3 datecall4 datecall5 datecall6 datecall7 datecall8 datecall9 datecall10)
       1 21968 22463 22347 22041 22360 22384 22238 22020 22315 22278
       2 22105 22324 22636 22365 21936 22425 22010 22379 22369 22052
       3 22006 22280 21988 22383 22009 21958 21942 22112 22361 22348
       4 22029 22508 22397 22330 22557 22276 22016 22474 22273 22096
       5 22407 22146 21975 22025 22594 22344 22036 21955 22564 22263
       6 22043 22250 22326 22092 22103 22030 21983 22266 22062 22637
       7 21951 22274 22199 22071 22211 22328 22441 22373 21992 22336
       8 21986 22247 22121 22612 22476 22637 22315 21984 22426 22136
       9 22130 21968 22222 22247 22516 22501 22296 22033 22449 22553
      10 22592 22634 22244 22524 22161 22016 22537 22124 22466 21919
      11 22120 22633 21921 22285 22144 22294 21915 22441 22343 22155
      12 22220 21932 22557 22219 22490 22542 22304 22245 21939 22209
      13 21998 22077 21924 22090 22476 22488 22043 22520 22208 22409
      14 22591 22165 22320 22248 22233 22396 22614 22295 22480 22464
      15 22558 22228 22479 22600 22175 21943 21918 22182 22318 21985
      16 21935 22021 22414 22196 22624 22353 22486 21994 22467 22322
      17 22126 22126 22616 22348 22025 22601 22623 22015 22489 22534
      18 21930 22516 22255 22062 22303 22409 22052 22560 22480 22065
      19 22503 22512 22564 22459 22239 22176 22398 22289 22023 21936
      20 22585 22580 22329 22121 22059 22363 22362 22100 22396 22100
      end
      format %td datecall1
      format %td datecall2
      format %td datecall3
      format %td datecall4
      format %td datecall5
      format %td datecall6
      format %td datecall7
      format %td datecall8
      format %td datecall9
      format %td datecall10
      
      reshape long datecall, i(id)
      rangestat (count) numref_90d = datecall, by(id) interval(datecall 0 90)
      -rangestat- is written by Robert Picard, Nick Cox, and Roberto Ferrer. It is available from SSC.

      Note that your request for number of calls "within 90 days" of each call is ambiguous. I have interpreted it to mean how many of the calls occur between 0 and 90 days after. But you could just have well meant between 0 and 90 days before. Or maybe, same day shouldn't count. Adapt the code accordingly.

      In the future, when asking for help with code, always show example data, and when showing data examples, please use the -dataex- command to do so. If you are running version 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.



      When asking for help with code, always show example data. When showing example data, always use -dataex-.

      Comment


      • #4
        Thank you for your help! I apologize but the data is very private so I don't think I am allowed to post even an example of it. Maybe I could modify it or something? Thank yuo!

        Comment


        • #5
          Do you know how to make rangestat work so that it only picks up a call between 0 to 90 days after (not before)?

          Comment


          • #6
            The code shown in #3 does it exactly that way.

            Comment


            • #7
              Oh wait, I see. I was counting the call itself. I changed it to (1 90) and it fixed this. Thank you!

              Comment

              Working...
              X