Announcement

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

  • while error

    clear
    set obs 10
    gen randnum = .
    local i = 1
    while `i' < 11 {
    local r = ceil(runiform()*100)
    if !inlist(r, randnum) {
    replace randnum = r in `i'
    local i = `i' + 1
    }
    else{
    local i = `i'
    }
    }



    . do "C:\Users\good\AppData\Local\Temp\STD668c_000000.t mp"

    . while `i' < 11 {
    2. local r = ceil(runiform()*100)
    3. if !inlist(r, randnum) {
    4. replace randnum = r in `i'
    5. local i = `i' + 1
    6. }
    7. else{
    8. local i = `i'
    9. }
    10. }
    <11 invalid name
    r(198);

    The error invalid name?Why?I even check it by the example in help.


  • #2
    Because I ain't defined for the next do file. It appears like you define it above, in what I can only imagine is the script before your lower code runs, but i is undefined there so Stata doesn't register it. Oh, and please, write your code in
    Code:
    Code blocks like this,  //
    so everyone can read it nicely.

    Comment


    • #3
      This is my do file.

      clear
      set obs 10
      gen randnum = .
      local i = 1
      while `i' < 11 {
      local r = ceil(runiform()*100)
      if !inlist(r, randnum) {
      replace randnum = r in `i'
      local i = `i' + 1
      }
      else{
      local i = `i'
      }
      }

      Comment


      • #4
        Once defined you need to refer to local macros using punctuation like this

        Code:
        `r'
        There may be other bugs. I see one: the inlist() call won't look at all the values in the variable named.

        The problem addressed is wanting 10 random integers between 1 and 100 that are different.

        Here is a way to do that.

        Code:
        . clear
        
        . set obs 100
        number of observations (_N) was 0, now 100
        
        . gen wanted = _n
        
        . set seed 314159265
        
        . gen double foo = runiform()
        
        . sort foo
        
        . list wanted in 1/10
        
             +--------+
             | wanted |
             |--------|
          1. |     82 |
          2. |      3 |
          3. |     57 |
          4. |     30 |
          5. |     92 |
             |--------|
          6. |     67 |
          7. |     10 |
          8. |     87 |
          9. |     19 |
         10. |     36 |
             +--------+

        Comment


        • #5
          You did respond to Jared Greathouse but did not follow his advice to write your code in code blocks (nor did you ask how to do this): Copy your code (or do-file) into a post, mark the commands just copied, and click on the hash (#) symbol (in the top of your "write" window) to wrap the tags around your selected text. Hence, read the full advice you get and ask if you don't understand it.

          Comment


          • #6
            Here is another way to do it with Mata.

            Code:
            . mata
            ------------------------------------------------- mata (type end to exit) ---------------------------
            : rseed(31459265)
            
            : wanted = jumble(1::100)
            
            : wanted[1::10,]
                     1
                 +------+
               1 |  48  |
               2 |  20  |
               3 |  92  |
               4 |  95  |
               5 |  68  |
               6 |  86  |
               7 |  44  |
               8 |  87  |
               9 |  40  |
              10 |  47  |
                 +------+

            Comment

            Working...
            X