Announcement

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

  • Using decimal numlist in foreach loop


    Dear Statalisters,

    I want to build a table that records prevalence of a variable at a varying hypothetical cut-offs. I used Stata's auto data here. My variable of interest is Headroom. And I have designed the cut-offs in such a way that they address all possible values of Headroom. I want to see the results by all possible cases of rep78.

    My code looks like this:

    Code:
    sysuse auto, clear
    
     tempname relation
      postfile `relation' rep78 cutoff headroom_preval N using relation, replace
       forval rep = 1(1)5 {
        foreach h of numlist 1.5(0.5)5.1 {
         loc n = `h'*10 /*because decimals cannot be used as part of a variable name*/
         gen  preval`n' = 0
         replace preval`n' = 1 if head > `h'
         qui sum preval`n' if rep = `rep' /*for each rep case*/
         loc prevalbyrep = r(mean)
         loc Nbyrep = r(N)
        post `relation' (`rep') (`h') (`prevalbyrep') (`Nbyrep')
        }
       }
       postclose `relation'
    I keep on getting -invalid syntax- with error -r(198)- .

    Any suggestion on how to fix this are highly appreciated.

    Using Stata 15.1


  • #2
    Code:
     
     if rep = `rep'
    should be

    Code:
     
     if rep == `rep'
    See
    Code:
     help operators

    Comment


    • #3
      Thanks a lot. I have been pouring over this for the past four hours!

      Comment


      • #4
        Thanks very much for closure. I can teach you one small extra thing: https://en.oxforddictionaries.com/usage/pour-or-pore

        Comment


        • #5
          Lesson learned!
          You truly are a legend.
          https://twitter.com/hmmlowe/status/1033747491194392576

          Comment

          Working...
          X