Announcement

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

  • Run a loop by survey wave

    Hello, I need to run the loop below separately for each wave of my survey as identified by the interview_nb variable. Since -foreach- does not admit the use of by, my super basic idea was to run each command preceded by -bysort interview_nb- but it does not seem the most efficient way to do that.
    Code:
    ds, has(char deflate)
    
    foreach x in `r(varlist)' {
    g `x'_99pct=`x'
    g `x'_95pct=`x'
    g `x'd_99=`x'_d
    g `x'd_95=`x'_d
    qui su `x',d
    replace `x'_99pct=. if `x'>=r(p99)&`x'!=.
    replace `x'_95pct=. if `x'>=r(p95)&`x'!=.
    replace `x'd_99=. if `x'>=r(p99)&`x'!=.
    replace `x'd_95=. if `x'>=r(p95)&`x'!=.
    }
    Is there a way to run this loop by survey way in a more concise way? Thanks



  • #2
    Well, the following is more concise:

    Code:
    foreach x of varlist `r(varlist)' {
        quietly summ `x', detail
        foreach n of numlist 95 99 {
            gen `x'_`n'pct = cond(inrange(`x', r(p`n'), .), ., `x')
            gen `x'd_`n' = cond(inrange(`x', r(p`n'), .), ., `x'_d)
        }
    }
    Note: Not tested -- beware of typos.

    That said, I'm not sure that the conciseness gained is a good thing--it's less transparent than the original code. I think the real gain here is replacing separate -generate- and -replace- commands with a single -generate- command using the -cond()- function. But the looping over 95 99 I think is not so advisable. A matter of taste, I suppose.

    Comment


    • #3
      Small detail:

      In the code in #1 it is unnecessary to exclude the missing values with the condition

      Code:
       
       `x' != .
      They are missing already and you don't want to change that.

      Comment

      Working...
      X