Announcement

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

  • Forvalues error: "i not found"

    Dear Statalist,

    I have encountered a seemly simple question when I was trying to "forvalues" in STATA 15.

    Before the forvalues, the codes were: (which works without any error)

    Code:
    generate new_var=old_var + 1 if inrange(_n,(1-1)*5760+1,5760*1)
    replace   new_var=old_var + 2 if inrange(_n,(2-1)*5760+1,5760*2)
    replace   new_var=old_var + 3 if inrange(_n,(3-1)*5760+1,5760*3)
    replace   new_var=old_var + 4 if inrange(_n,(4-1)*5760+1,5760*4)
    replace   new_var=old_var + 5 if inrange(_n,(5-1)*5760+1,5760*5)
    replace   new_var=old_var + 6 if inrange(_n,(6-1)*5760+1,5760*6)
    replace   new_var=old_var + 7 if inrange(_n,(7-1)*5760+1,5760*7)
    Then, I tried to use "forvalues" to simplify my codes:

    Code:
    forvalues i=1(1)7{
    gen neww_var=old_var + i if inrange(new_n,(i-1)*5760+1,5760*i)
    format loop_date  %tdNN/DD/CCYY
    }
    And STATA log showed that:
    i not found
    r(111);

    Anyone has a thought about that? Really don't know what is going wrong here...though it really seems a simple loop for me
    Any reply would be very much appreciated! Thanks in advance!

    Yingyi

  • #2
    You should take a look at the Stata Manual. In short, you’ll need to use ‘i’ (typed in my portable phone, hence not ‘exactly’, but I believe you got the idea.
    Last edited by Marcos Almeida; 10 Nov 2017, 18:13.
    Best regards,

    Marcos

    Comment


    • #3
      Originally posted by Marcos Almeida View Post
      ... you’ll need to use ‘i’ ...
      He means:

      Code:
      `i'
      Be aware that it can be very hard to answer a question without sample data. You can use the dataex command for this. Type help dataex at the command line.

      When presenting code or results, please use the code delimiters format them. Use the # button on the formatting toolbar, between the " (double quote) and <> buttons.

      Comment


      • #4
        Marcos and Weiwen are right. You must refer to a local macro that is a loop index with appropriate punctuation.

        Inside your loop, it's not illegal to refer to i but it can only mean a variable or scalar. Stata is telling you that there is no such variable or scalar.

        Even with that fixed your loop would fail as second time around you are trying to generate a variable which already exists.

        That is all assuming that you have variables newn and loop_date (not obviously relevant here).

        If you have loop_date, there is no point to assigning the same format seven times over. That statement belongs outside the loop.

        All that said, you don't need a loop for what you want. A single command will suffice.

        Code:
        . clear
        
        . set obs `=5760*7'
        number of observations (_N) was 0, now 40,320
        
        . gen oldvar = 42
        
        . gen newvar = oldvar + ceil(7 * _n/_N)
        
        . tab newvar
        
             newvar |      Freq.     Percent        Cum.
        ------------+-----------------------------------
                 43 |      5,760       14.29       14.29
                 44 |      5,760       14.29       28.57
                 45 |      5,760       14.29       42.86
                 46 |      5,760       14.29       57.14
                 47 |      5,760       14.29       71.43
                 48 |      5,760       14.29       85.71
                 49 |      5,760       14.29      100.00
        ------------+-----------------------------------
              Total |     40,320      100.00
        See help foreach and Statalist FAQ Advice #18

        Comment


        • #5
          Thanks so much for the reply guys! I ended up using Nick's codes and it works perfectly! Thanks again~

          Comment


          • #6
            Originally posted by Nick Cox View Post
            Marcos and Weiwen are right. You must refer to a local macro that is a loop index with appropriate punctuation.

            Inside your loop, it's not illegal to refer to i but it can only mean a variable or scalar. Stata is telling you that there is no such variable or scalar.

            Even with that fixed your loop would fail as second time around you are trying to generate a variable which already exists.

            That is all assuming that you have variables newn and loop_date (not obviously relevant here).

            If you have loop_date, there is no point to assigning the same format seven times over. That statement belongs outside the loop.

            All that said, you don't need a loop for what you want. A single command will suffice.

            Code:
            . clear
            
            . set obs `=5760*7'
            number of observations (_N) was 0, now 40,320
            
            . gen oldvar = 42
            
            . gen newvar = oldvar + ceil(7 * _n/_N)
            
            . tab newvar
            
            newvar | Freq. Percent Cum.
            ------------+-----------------------------------
            43 | 5,760 14.29 14.29
            44 | 5,760 14.29 28.57
            45 | 5,760 14.29 42.86
            46 | 5,760 14.29 57.14
            47 | 5,760 14.29 71.43
            48 | 5,760 14.29 85.71
            49 | 5,760 14.29 100.00
            ------------+-----------------------------------
            Total | 40,320 100.00
            See help foreach and Statalist FAQ Advice #18


            Hi Nick, you are so right that even after I use `i' in below , the codes did not work.

            Code:
            forvalues i=1(1)7{
            gen loop_new_var = old_var + `i' if inrange( _n ,(`i'-1)*5760+1,5760*`i')
            }
            STATA logs showed: variable loop_new_var already defined
            Last edited by Yingyi Lin; 28 Nov 2017, 14:07.

            Comment


            • #7
              Codes I ended up using:

              Code:
              gen loop_new_var
              forvalues i=1(1)7{
              replace loop_new_var = old_var + `i' if inrange( _n,(`i'-1)*5760+1,5760*`i')
              }
              OR

              Code:
              set obs `=5760*7'old_var
              *number of observations (_N) was 0, now 40,320
              gen new_var = old_var + ceil(7 * _n/_N)
              Thanks Marcos, Weiwen and Nick, again!

              Comment

              Working...
              X