Announcement

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

  • Multiple variable lists in for loop

    Hi everyone, thanks so much for taking the time to help me.

    Having some trouble using different variable lists in a for loop. I am using time series data measuring number of orders of mammograms on a given day. The celebrity variables (Phillips_60, etc.) label a 60 day window after a certain event. The goal is to generate two variables (popularity, awareness) that label the 60-day window with the corresponding popularity and awareness scores. I've cut a few variables and numbers out out for the sake of clarity.

    The main error message I get is "if not found" and I've been stuck on this problem for a while, and I would truly appreciate any help.

    Here' s the code I've got now.

    gen popularity = 0
    gen awareness = 0
    foreach var of varlist Phillips_60 Lee_60 Urich_60 Harris_60 Mizell_60 {
    local i = `i' + 1
    *local celeb: word `i' of `varlist1'
    local pop_values : word `i' of "2.095" "2.759" "2.726" "2.808" "1.879"
    replace popularity = `pop_values' if `var' == 1

    local aware_values : word `i' of "0.074" "0.470" "0.286" "0.343" "0.066"
    replace awareness = `aware_values' if `var' == 1
    }
    Last edited by Max Luo; 31 May 2015, 16:37.

  • #2
    I see several problems.

    1. This may or may not be your problem: The local macro var gets defined by the -foreach- statement, and is then used inside the loop in the -replace- statements. If you are running this line by line from the do-editor, rather than running the entire block of code at once, then after the -foreach- statement is done, the local macro var will go out of scope, and when you get to the -replace- statements, it will be interpreted as an empty string. So then Stata will think you are asking it to -replace popularity = `pop_values' if == 1-, which is definitely a syntax error (though not necessarily the syntax error you are getting).

    2. This is almost certainly a problem: At least in the code you show us, you never actually initialize the local macro i. The first mention of it is the self-referential -local i = `i' + 1-. Well, the first time through the loop, `i' is just an empty string. So `i'+1 will be just + 1. So local i will contain "+ 1". You then try to index some lists of constants using `i'. But -word `+ 1' of "2.095" "2.759"...- will be a syntax error. So then local pop_values will not be defined, but will just be an empty string. Then when you get to your first -replace- statement, Stata will think you are asking it to -replace popularity = if `var' == 1-, which I'm pretty sure will give you precisely the syntax error you're getting. So you need to initialize local macro i before the loop.

    3. And so is this: Because of your other syntax errors, you haven't gotten to this one yet, but it will bite you after you fix the others. You have -gen-erated poularity and awareness as numeric variables. But the values of `pop_values' and `aware_values' are going to be strings because of the way you have set up those macros. And Stata is not going to think to convert those strings to their numeric values. Instead it will give you yet another syntax error. So eliminate all the quotes in those lists of values so that `pop_values' and `aware_values' will be numbers.

    Comment


    • #3
      I note that you don't initialize the local variable i to zero at the start of your code. Suppose somewhere earlier in your do-file you used the local variable i for some other purpose, and it is something non-zero, say 42, at the start of your foreach loop. In that case
      Code:
      local pop_values : word `i' of "2.095" "2.759" "2.726" "2.808" "1.879"
      will expand to
      Code:
      local pop_values : word 43 of "2.095" "2.759" "2.726" "2.808" "1.879"
      so pop_values will be effectively blank, and then
      Code:
      replace popularity = `pop_values' if `var' == 1
      will expand to
      Code:
      replace popularity = if Phillips_60 == 1

      Comment


      • #4
        I have a related question, which is just to ask if the looping over multiple lists will ever work. Say you don't need any of the 'word' functionality or anything, you just want to loop over multiple lists. Say Phillips_60, Lee_60, etc. are themselves lists of 10 variables. Obviously you could make a new list with all 50 variables, but there must be a way to avoid that.

        Something like this, but this doesn't work:

        foreach var of varlist Phillips_60 Lee_60 Urich_60 Harris_60 Mizell_60 {
        summ `var'
        }

        Comment


        • #5
          This silly example points the way: your approach is just one set of `' away from working.
          Code:
          . local poem line1 line2
          
          . local line1 I think that I shall never see
          
          . local line2 A poem as lovely as a tree
          
          .
          . foreach line in `poem' {
            2. display "`line': " "``line''"
            3. }
          line1: I think that I shall never see
          line2: A poem as lovely as a tree
          
          .
          The trick is that with nested sets of `' the innermost set is evaluated first, then the result is substituted and the next set evaluated.

          So for your code, summ ``var'' is what you need..

          Comment

          Working...
          X