Announcement

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

  • Events in Intervals

    Hi,
    I am attempting to count admissions that have occurred for an ID variable (IDV) that occur (ADate) between two dates ; DDate and M.
    M is a date that is created as occurring 365 days before DDate. I have used the following syntax after reading Stata tip 51 but continue to receive error - invalid syntax r(198) -- please help

    gen n_admit_12m = .
    local N =_N
    quietly forval i = 1/‘N’ {
    count if IDV == IDV[‘i’]& ///
    inrange(ADate[‘i’] – M, 1, DDate)
    replace n_admit_12m = r(N) in 'i'
    }
    Sample data is:
    IDV ADATE DDATE M
    2 5/6/2017 7/6/2017 5/6/2016
    2 8/8/2016 12/8/2016 8/8/2015
    2 10/10/2016 24/10/2016 10/10/2015
    3 24/12/2018 05/01/2019 24/12/2017
    With thanks
    Tags: None

  • #2
    Code:
    gen n_admit_12m = .
    local N =_N
    quietly forval i = 1/‘N’ {
        count if IDV == IDV[‘i’] & inrange(ADate[‘i’] – M, 1, DDate)
        replace n_admit_12m = r(N) in 'i'
    }
    The syntax error may be which single quotes you are using around macro references. The opening single quote on keyboards I know about is near the Esc key. The quotes in the replace statement look wrong.

    Note that rangestat (SSC) in several ways supersedes that Tip 51 at https://www.stata-journal.com/sjpdf....iclenum=pr0033
    Last edited by Nick Cox; 09 Jun 2020, 03:54.

    Comment


    • #3
      Copied and pasted from my local list of frequent answers, to elaborate on Nick's answer.

      To have Stata substitute the value of a local macro
      Code:
       shortcut
      you need to type on your keyboard
      Code:
       `shortcut' 
      where the first character is (on an American English keyboard) the "left single quote" beneath the tilde (~) character below the ESC key, and the final character is the usual "single quote" ("apostrophe") just to the left of the RETURN key. Neither of these are "smart quotes" as understood in programs like Microsoft Word, and in particular the first character is correctly titled the "grave accent".

      Note that the Stata PDF documentation unfortunately displays local macro references incorrectly using smart single quotes as
      Code:
       ‘shortcut’ 
      which leads to no end of confusion, especially because such code will not work if copied and pasted into Stata. On the other hand, it does look elegant in the printed documentation.

      Comment


      • #4
        Thank you for the assistance. I can see the correct keyboard key and have rewritten the code to be:

        gen n_admit_12m = .
        local N =_N
        quietly forval i = 1/`N’ {
        count if IDV == IDV[`i’] & inrange(ADate [`i’] – M, 1, DDate)
        replace n_admit_12m = r(N) in `i’
        }

        it continues to report an invalid syntax --

        Comment


        • #5
          The right single quotes remain incorrect.

          Comment


          • #6

            Thank you William. I am using the key under ESC and the key to the left of the Enter key:
            gen n_admit_12m = . local N =_N quietly forval i = 1/`N'{ count if IDV == IDV[`i'] & inrange(ADate [`i']– M, 1, DDate) replace n_admit_12m = r(N) in `i' }

            Comment


            • #7
              Note that the code is puzzling in any case.

              Code:
              inrange(ADate[‘i’] – M, 1, DDate)
              The first argument is a difference between dates. So by intention is the second, but the third is a date, in practice an integer about 20000.

              Comment


              • #8
                Apologies. I am still learning and very junior in the use of stata.
                The aim of the syntax is to count where the IDV matches and where the ADate is between the M and DDate.

                Comment


                • #9
                  No need to apologise. We're all still learning.

                  Please see FAQ Advice #12 on giving data examples. https://www.statalist.org/forums/help#stata As explained there (12.2) this is especially important for date variables. As I understand #8 the calculation is direct:

                  Code:
                  clear
                  input IDV  str42(ADATE     DDATE     M)
                  2     "5/6/2017" "7/6/2017" "5/6/2016"
                  2     "8/8/2016" "12/8/2016" "8/8/2015"
                  2     "10/10/2016" "24/10/2016" "10/10/2015"
                  3     "24/12/2018" "05/01/2019" "24/12/2017"
                  end
                  
                  foreach v in ADATE DDATE M {
                      gen work = daily(`v', "DMY")
                      drop `v'
                      rename work `v'
                      format `v' %td
                  }
                  
                  egen count = total(inrange(ADATE, M, DDATE)), by(IDV)
                  
                  list, sepby(IDV)
                  
                       +-------------------------------------------------+
                       | IDV       ADATE       DDATE           M   count |
                       |-------------------------------------------------|
                    1. |   2   05jun2017   07jun2017   05jun2016       3 |
                    2. |   2   08aug2016   12aug2016   08aug2015       3 |
                    3. |   2   10oct2016   24oct2016   10oct2015       3 |
                       |-------------------------------------------------|
                    4. |   3   24dec2018   05jan2019   24dec2017       1 |
                       +-------------------------------------------------+
                  Last edited by Nick Cox; 10 Jun 2020, 01:34.

                  Comment


                  • #10
                    Thank you Nick.
                    Please find see dataex below:

                    dataex IDV ADate DDate M, count (10)

                    ----------------------- copy starting from the next line -----------------------
                    Code:
                    * Example generated by -dataex-. To install: ssc install dataex
                    clear
                    input str8 IDV int(ADate DDate M)
                    "A7735871" 22069 22069 21704
                    "F3291117" 22068 22069 21704
                    "B7609262" 22068 22069 21704
                    "D7207148" 22068 22069 21704
                    "B0323731" 22068 22069 21704
                    "B0493648" 22067 22069 21704
                    "E9366438" 22067 22068 21703
                    "C8763867" 22067 22069 21704
                    "C7361730" 22067 22067 21702
                    "C8763867" 22066 22067 21702
                    end
                    format %tdnn/dd/CCYY ADate
                    format %tdnn/dd/CCYY DDate
                    format %tdnn/dd/CCYY M
                    ------------------ copy up to and including the previous line ------------------

                    Listed 10 out of 41642 observations

                    Comment

                    Working...
                    X