Announcement

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

  • Foreach Loops

    Got an error while using foreach loop

    foreach varlist in "assignN" "assignM" "assignC"{
    gen hw`varlist' = .
    replace hw`varlist'= "varlist" in scall-1
    }

    what I wanted to do with this loop is to generate hwassignN, hwassignM, hwassignC variables which will take the assignN, assignM and assignC variables when when it is scall -1. "scall" is numeric variable. Thank you so much for helping me out

  • #2
    Code:
    foreach var in assignN assignM assignC {
    gen hw`var' = `var' if scall == -1
    }

    Comment


    • #3
      Thank you so much Snilsberg.... The loop ran without any error but the loop was not able to retrieve the values of assignN, assignM and assignC that were in previous scall (s-1)

      Comment


      • #4
        I see, I misunderstood your inquiry - please provide a data example by typing -dataex- in Stata and copying the output in the results window

        Comment


        • #5
          . dataex NameoftheStudent scall rcall hwroundnno assignN assignM assignC didN didM didC in 1/10

          ----------------------- copy starting from the next line -----------------------
          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input str20 NameoftheStudent float(scall rcall hwroundnno) str3(assignN assignM assignC) str9(didN didM didC)
          "Aadarsa b.k"       2 3 1 "Yes" "Yes" "No"  "Yes" "Yes" "No" 
          "Aadarsa b.k"       2 3 1 "Yes" "Yes" ""    "No"  "No"  ""   
          "Aaishma Chaudhary" 2 2 1 "Yes" "No"  "Yes" "Yes" "Yes" "Yes"
          "Aaishma Chaudhary" 3 3 2 "Yes" "Yes" "No"  "Yes" "Yes" "Yes"
          "Aaishma Chaudhary" 4 4 3 "Yes" "Yes" "No"  "Yes" "Yes" "Yes"
          "Aaishma Chaudhary" 1 1 0 "Yes" "No"  "Yes" "No"  "No"  "No" 
          "Aakash Pariyar"    1 1 0 "Yes" "Yes" "No"  "No"  "No"  "No" 
          "Aakash Pariyar"    2 2 1 "Yes" "Yes" ""    "Yes" "Yes" ""   
          "Aakash Pariyar"    3 3 2 "Yes" "Yes" ""    "Yes" "Yes" ""   
          "Aauyush Gharti"    2 2 1 "Yes" "Yes" ""    "Yes" "Yes" ""   
          end
          ------------------ copy up to and including the previous line ------------------

          Listed 10 out of 253 observations

          Comment


          • #6
            This is how I created variables listed above

            hwroundnno = SuccessfulPhoneCallMade - 1
            assignN = DidyouassignanyhomeworkinN
            assignM = DidyouassignanyhomeworkinM
            assignC = DidyouassignanyhomeworkinC
            scall = SuccessfulPhoneCallMade
            rcall = PhoneCallRoundNoshouldbe
            didN = DidthestudentdohisherNepal
            didM = DidthestudentdohisherMathe
            didC = DidthestudentdohisherConce

            Comment


            • #7
              thank you for the data example, does this give you the desired result,
              Code:
              foreach v of var assign* {
              by NameoftheStudent (scall), sort: gen hw`v' = cond(_n>1,`v'[_n-1],`v')
              }
              ?

              Comment


              • #8
                Yes it works perfectly.... thank you so much Mr. Snilsberg

                Comment

                Working...
                X