Announcement

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

  • how to create a loop for a macro?

    I have a very large dataset but to cut it short I demonstrated the data with the following example:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(patid death dateofdeath)
     1 0     .
     2 0     .
     3 0     .
     4 0     .
     5 1 15007
     6 0     .
     7 0     .
     8 1 15526
     9 0     .
    10 0     .
    end
    format %d dateofdeath
    I am trying to sample for a case-control study based on date of death. At this stage, I need to first create a variable with each date of death repeated for all the participants (hence we end up with a dataset with 20 participants) and a "pairid" equivalent to the patient id (patid) of the corresponding case.

    I created a macro for one case (which works) but finding it difficult to have it repeated for all cases (where death==1) in a loop.

    The successful macro is as follows:
    Code:
    local i "5" //patient id who died
              
                gen pairid= `i'
                gen matchedindexdate = dateofdeath
                replace matchedindexdate=0 if pairid != patid
                gsort matchedindexdate
                replace matchedindexdate= matchedindexdate[_N]
                save temp`i'

    and the loop I attempted is:

    Code:
    forval j = 1/10 (min and max patid id) {
    count if patid == `j' & death==1 //to specify that I want the loop run for cases only.
    if r(N)=1 {
                gen pairid= `j'
                gen matchedindexdate = dateofdeath
                replace matchedindexdate=0 if pairid != patid
                gsort matchedindexdate
                replace matchedindexdate= matchedindexdate[_N]
                save temp/matched`j'
    
    }
    
    }
    
    use temp/matched1, clear
    forval i=2/10 {
        capture append using temp/matched`i'
        save matched, replace
    }
    but I get
    Code:
    invalid syntax


    How can I do the loop?

    Your help is highly appreciated.


  • #2
    Cross-posted at https://stackoverflow.com/questions/...macro-in-stata


    Code:
     
     if r(N)=1 {
    should be

    Code:
     
     if r(N)==1 {

    Comment


    • #3
      Nick Cox
      Thank you for your reply.
      I changed the command as recommended to

      if r(N)==1 {
      but I keep getting
      invalid syntax

      Comment


      • #4
        You also need to comment out or remove the line that says (min and max patid id)

        Comment


        • #5
          Ali Atia

          yes thank you, it did work out, but not completely!!
          My commands are now are:

          forval j = 1/10 {
          count if patid == `j' & death==1
          if r(N)==1 {
          gen pairid= `j'
          gen matchedindexdate = dateofdeath
          replace matchedindexdate=0 if pairid != patid
          gsort matchedindexdate
          replace matchedindexdate= matchedindexdate[_N]
          save temp/matched`j'

          }

          }

          use temp/matched1, clear
          forval i=2/10 {
          capture append using temp/matched`i'
          save matched, replace
          }

          However, I am getting

          variable pairid already defined

          I do get the file matched5 afterwards with what I want but only for one case (for patid 5) but not for the other case (patid 8).

          Comment


          • #6
            I got it solved!!! Many thanks for all those who helped.

            I had to use the dataset again within the loop so this is how the final commands look like:

            forval j = 1/10 {
            use miniset,clear //this is the dataset example I am using.
            count if patid == `j' & death==1
            if r(N)==1 {
            gen pairid= `j'
            gen matchedindexdate = dateofdeath
            replace matchedindexdate=0 if pairid != patid
            gsort matchedindexdate
            replace matchedindexdate= matchedindexdate[_N]
            save temp/matched`j'



            }

            }


            use temp/matched5, clear
            forval i=6/10 {
            capture append using temp/matched`i'
            save matched, replace
            }

            Thank you again for all the help.

            Comment

            Working...
            X