Announcement

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

  • Problem making a loop to count the number of dates

    Hi,

    I am working on a project examining the association between Covid-19 and a number of pregnancy outcomes.

    I have merged test data to a pregnancy dataset. The test data merged on the pregnancy data is in wide format and covers positive tests taken during the entire pandemic.
    I want to make a loop counting the number of dates of a registered positive test (some of the observations in the dataset have more than one registered positive tests) during an interval from the start of pregnancy to the end of pregnancy.

    I have tried out the code below, but it will not run, and I get the message 'varlist not allowed'.

    Code:

    forvalues i=1/11 {
    count PRDATE`i' if PRDATE`i'>=date_conception & PRDATE`i'<=preg_outcome
    } I hope you can help me out to get the code right. Thanks in advance.

    Best,
    Trine.

  • #2
    The help for count shows that the syntax is

    Code:
    count [if] [in]
    so, as cited, a variable list (one or more variable names) is not allowed. What I guess will work is

    Code:
    forvalues i=1/11 {
    count if PRDATE`i'>=date_conception & PRDATE`i'<=preg_outcome
    }
    I would create a little table. There are several ways to do this. Here is one, but naturally I can't test the code against your data.

    Code:
    matrix results = J(11, 2, .) 
    
    forval j = 1/11 { 
         quietly count if inrange(PRDATE`j', date_conception, pregnancy_outcome) 
         matrix results[`j', 1] = `j' 
         matrix results[`j', 2] = r(N)
    }
    
    matrix li results

    Comment


    • #3
      Hi Nick,

      Thanks a lot for your suggestions. I can make the code run, but I would like to generate a count variable, that counts how many test dates that lie within the interval of date of conception and the date of the pregnancy outcome. I need this count variable to be able to generate a new variable including the date of the first positive Covid-19 test during pregnancy. I hope it makes sense:-).

      Kind regards,
      Trine.

      Comment


      • #4
        That sounds to be a completely different problem from what your code was doing. My guess is that you want something like


        Code:
        gen wanted = 0
        gen  first_date = .  
        forval j = 1/11 {      
              replace wanted = wanted + inrange(PRDATE`j', date_conception, pregnancy_outcome)      
              replace first_date = PRDATE`j' if inrange(PRDATE`j', date_conception, pregnancy_outcome)  & missing(first_date)  
        }
        (I am shortly travelling, so my contributions here are likely to be limited and delayed for some days.)

        A general comment is that long layout is preferable here.
        Last edited by Nick Cox; 06 Sep 2023, 03:35.

        Comment


        • #5
          Hi Nick,

          I really appreciate your help. Your last suggestion solved my problem:-). Thanks a lot, and safe travels.

          Kind regards,
          Trine.

          Comment


          • #6
            To be clear, count counts across observations, not variables. I guess that you were imagining it to be a good starting point, but as your problem is counting across variables, it can't help.

            Comment


            • #7
              Hi Nick,

              Your code below works, and has solved my problem:-). However, I am not sure I completely understand how the last part of the code (marked in bold) picks the first date among the number of positive tests registered during pregnancy (inrange the date of conception and the date of pregnancy outcome)? Can you please explain this?

              gen wanted = 0 gen first_date = . forval j = 1/11 { replace wanted = wanted + inrange(PRDATE`j', date_conception, pregnancy_outcome) replace first_date = PRDATE`j' if inrange(PRDATE`j', date_conception, pregnancy_outcome) & missing(first_date) }
              In the same dataset, I have generated a variable for whether there within each pregnancy is a registered positive Covid-19 test prior to pregnancy, so prior to the date of conception.

              I have used this code (which works out fine):

              gen test_prior_pos=0

              forvalues i = 1/N {
              replace test_prior_pos=1 if (RESULT`i'==1 & PRDATE`i'<date_concep & preg_outcome!=.)
              }

              However, I would additionally like to generate a new variable including the date of the latest positive Covid-19 test before the date of conception, so among the number of tests registered prior to pregnancy, I want to pick the test date closest to the date of conception. I guess the solution is somewhat similar to the code above, but I can't seem to get i right. Do you have a suggestion?

              Best regards,

              Trine.

              Comment


              • #8
                I am assuming that your dates 1 to 9 are in strict order. So date 1 is the first, and so on.

                In the loop you cite

                1. You initialize the first date as missing,

                2. Then -- within the loop -- you find the first date by seeing which first satisfies the conditions.

                3. Once you replace it with a non-missing date, it's no longer missing and so will not be changed in later iterations.

                To get the last date you just remove the constraint that the date must be missing just before you replace it.


                Code:
                gen last_date = . 
                
                forval j = 1/11 { 
                      replace last_date = PRDATE`j' if inrange(PRDATE`j', date_conception, pregnancy_outcome)
                }
                You may find https://www.stata-journal.com/articl...article=pr0046 helpful. https://www.stata-journal.com/articl...ticle=pr0046_1 was an update

                Comment


                • #9
                  Thanks a lot Nick, it worked:-). I really appreciate all your help.

                  Best regards,
                  Trine Damsted.

                  Comment

                  Working...
                  X