Announcement

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

  • Foreach loop with putexcel

    I am trying to create a separate excel sheet for each id of a dataset with information specific to the patient. For example, I would like to put the language that the patient has indicated they speak into the excel sheet. I am able to specify this for an individual patient but would like to create a loop for each patient. I am not able to use an if statement for putexcel, so I have been trying a few other things but have not been successful. Any ideas of how I could specify that stata should put id specific information into the Excel sheet for that id?

    levelsof clinic_id_confirm, local(id)
    foreach val of local id {
    putexcel set consentdocumentation_`val'.xlsx , replace
    putexcel D4 = "Study Consent Documentation Form"
    putexcel A6 = "Title: XXXXX"
    putexcel A8 = "What language(s) do you understand?"
    if clinic_id_confirm == `val' {
    putexcel A9 = language_prefer_string
    }
    }

  • #2
    I think this will be easier to accomplish if you loop across observations in your dataset rather than values of clinic_id_confirm.
    Code:
    forvalues i=1/`c(N)’ {
    local val = clinic_id_confirm[`i’]
    putexcel set consentdocumentation_`val'.xlsx , replace
    putexcel D4 = "Study Consent Documentation Form"
    putexcel A6 = "Title: XXXXX"
    putexcel A8 = "What language(s) do you understand?"
    putexcel A9 = language_prefer_string[`i’]
    }

    Comment


    • #3
      Worked like a charm, thank you so much!!

      Comment


      • #4
        For those who may find this post at a later date, I see now that the editor I used to create the code in post #2 "smartened" the quotation marks around the local macros I typed, creating three typographical errors. The code below corrects these errors.
        Code:
        forvalues i=1/`c(N)' {
        local val = clinic_id_confirm[`i']
        putexcel set consentdocumentation_`val'.xlsx , replace
        putexcel D4 = "Study Consent Documentation Form"
        putexcel A6 = "Title: XXXXX"
        putexcel A8 = "What language(s) do you understand?"
        putexcel A9 = language_prefer_string[`i']
        }

        Comment


        • #5
          If I needed to create two loops as above but slightly different depending on whether the variable minor is 1 or 0 how would I be able to do this? How do you cycle through all observations and apply the loop to those that have minor as 0 or minor as 1?

          Comment


          • #6
            Since for the purposes of creating your separate Excel worksheets for each observation you are processing one observation at a time, two loops may not be necessary - consider the following code.
            Code:
            forvalues i=1/`c(N)' {
            local m = minor[`i']
            if `m'==1 {
                // code for when minor is 1
            }
            if `m'==0 {
                // code for when minor is 0
            }
            }

            Comment

            Working...
            X