Announcement

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

  • best way to "set obs" when the number of obs is not clearly known?

    Hi Fellow Listers,

    I am trying to generate a sequence of values based on a numlist from 1 to X in a non-integer increment such as 1(0.0001)10.

    The code below does this okay, but I am not sure if there is a better approach to setting the observations (here I arbitrarily set obs to 100000).

    Any help would be much appreciated!


    qui {
    set obs 100000
    local cnt = 1
    gen test =.
    forval i = 1(0.0001)10 {
    replace test = `i' in `cnt'
    local cnt = `cnt' + 1
    }
    }

    Ariel

  • #2
    I believe this is the general case of what you want:
    Code:
    clear
    local start = 1
    local stop = 10
    local inc = 0.0001
    local obs = 1 + ceil((`stop'- `start')/`inc') // ceil() in case result is not an integer
    set obs `obs'
    gen test = `start' + (_n -1) *`inc'
    drop if (test > `stop') // possible extras

    Comment


    • #3
      Thanks, Mike!

      While I was waiting for a reply, I came up with this version, although yours is more eloquent!

      set obs 1
      local cnt = 1
      gen double test =.
      forval i = 1(0.0001)10 {
      replace test = `i' in `cnt'
      local cnt = `cnt' + 1
      if test < 10 set obs `=_N + 1'
      }


      Thanks!

      Ariel

      Comment

      Working...
      X