Announcement

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

  • Creating a foreach loop

    I've been the code (see below) - but I need to copy and paste this for 20 times for different variables(see bold) eg Dvt, MI, heartfailure etc.


    bys hospitalid2 date: egen new_var = max(dvt == 1)
    by hospitalid2 date: egen all_missing = min(missing(dvt))
    by hospitalid2 date: keep if _n == 1
    replace new_var = . if all_missing == 1
    drop dvt all_missing

    rename new_var dvt


    I have created a loop:

    foreach i of dvt mi heartfailure {
    bys hospitalid2 date: egen new_var = max(`i' == 1)
    by hospitalid2 date: egen all_missing = min(missing(`i'))
    by hospitalid2 date: keep if _n == 1
    replace new_var = . if all_missing == 1
    drop `i' all_missing
    }

    however stata says invalid syntax...

    I am perhaps not writing it correctly, can anyone help?

  • #2
    The immediate problem is that you need

    Code:
    foreach i in dvt mi heartfailure
    but your code will fail second time around the loop, as then new_var already exists.

    I am not clear enough what you're trying to do to suggest better code It looks dangerous to me to drop observations each time around the loop.

    Please use CODE delimiters as here. Code is much more readable if presented as code.

    Comment


    • #3
      Nick Cox thanks for this.

      yes you’re right the loop won’t work as new_var is aleady speicfied and won’t work for the remaining 19 times.

      if there is no other solution i’ll stick with copy and paste


      however this what i’m trying to do
      https://www.statalist.org/forums/for...e-observations
      - Combing rows which have a uniqueid and date .

      For all variables - observations are binary 1 or 0. and if one of the rows (having the same id and admission date) has a 0 and another row as a 1 for that specified variable .the 1 will take precedence and drop the row with 0

      this will repeated for all remaining variables : mi/ heart failure / dvt


      Apart from one which is LEngth of stay and wont be included.

      Comment


      • #4
        Well, I never really understood what that thread was first about. The OP had observations with the same identifier and date -- which I understood -- but with different values on other variables, which I understood too -- but I was never clear what they wanted to select and what to ignore.

        In the same way, I have to guess that you're stabbing in the dark with code you don't fully understand, which is always dangerous.

        If you want to keep just observations with 1 on all indicators, you need to calculate their row sum and keep if the row sum is the number of variables concerned. Otherwise I doubt that your rules are complete. How do you choose between keeping 1 0 1 and 1 1 0 and 0 1 1?

        Comment


        • #5
          Copy-paste will not help since you are attempting to create a variable with same name multiple times, so it will produce an error from the second time onwards. Also, you are dropping observations that you need in later iterations. Here is what you might want to do instead:

          Code:
          local varlist dvt mi heartfailure // add other variables here
          sort hospitalid2 date
          
          foreach v of local varlist {
              by hospitalid2 date: egen new_`v' = max(`v' == 1)
              by hospitalid2 date: egen all_missing = min(missing(`v'))
              replace new_`v' = . if all_missing == 1
              drop `v' all_missing
          }
          
          * if you want to retain just one observation per hospitalid2 and date, now is the time to do this (i.e. after the loop)
          by hospitalid2 date: keep if _n == 1
          You could also make the loop above shorter and perhaps simpler by replacing it with:
          Code:
          foreach v of local varlist {
              by hospitalid2 date: egen new_`v' = max(cond(missing(`v'),.,`v' == 1))
              drop `v'     
          }
          Last edited by Hemanshu Kumar; 07 Dec 2022, 06:33.

          Comment


          • #6
            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input float mi long hospitalid2 float(date dvt) str3 var5
            1 1 21915 0 "1,0"
            1 1 21915 0 ""  
            1 2 21916 0 "1,1"
            1 2 21916 1 ""  
            0 2 21916 0 ""  
            0 3 21976 0 "1,1"
            1 3 21976 1 ""  
            1 3 21976 1 ""  
            0 4 22069 0 "0,1"
            0 4 22069 1 ""  
            end
            format %td date
            label values hospitalid2 hospitalid2
            label def hospitalid2 1 "12A", modify
            label def hospitalid2 2 "13A", modify
            label def hospitalid2 3 "14A", modify
            label def hospitalid2 4 "15A", modify

            Hi , there thanks for helping me out here.
            However, I came across an unsual problem.

            I've tried the above code with the data above. Please not var 5 is what I expect to see.
            Yet stata just reads the code and doesn't action it. What am I doing wrong?
            I've highlighted all the code

            (see screenshot)
            Click image for larger version

Name:	pic1.png
Views:	1
Size:	128.1 KB
ID:	1692717


            I once had a similar problem before, some people had said that it was due to copy and paste. I have opened a new do file, reloaded the file and still same issue.

            Click image for larger version

Name:	pic2.png
Views:	1
Size:	64.6 KB
ID:	1692718


            Last edited by Denise Vella; 09 Dec 2022, 04:58.

            Comment


            • #7
              see screenshots



              Comment


              • #8
                You have written a loop over

                Code:
                local varist
                Stata sees a reference to a macro that doesn't exist, or equivalently is empty. That kind of reference is perfectly legal (and in other contexts may be very useful).

                It is fine to ask Stata to do nothing, which is exactly what you want done in many cases.

                Here for you, it's a typo. You need a reference to varlist

                Comment

                Working...
                X