Announcement

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

  • loop

    I need help with generating a series from an existing series that assigns a value of minus one of the series I am using. I have area codes data. I want to recode the area code from 5 to 109. I want 5 to be 4 up to 109 to be 108. Each area code has over 1000 observations.

    Here is a sample of the data.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long(Date Date_td) byte(Time Day Area)
    130823 130823 18 1  5
    130823 130823 18 1  5
    130823 130823 18 1  5
    130823 130823 18 1  5
    130823 130823 18 1  5
    130823 130823 18 1  5
    130823 130823 18 1  5
    130823 130823 18 1  5
    130823 130823 18 1  5
    130823 130823 18 1  5
    130823 130823 18 1  5
    130823 130823 18 1  5
    130823 130823 18 1  5
    130823 130823 18 1  5
    130823 130823 18 1  5
    130823 130823 18 1  5
    130823 130823 18 1  5
    130823 130823 18 1  5
    130823 130823 18 1  5
    130823 130823 18 1  5
    130823 130823 18 1  5
    130823 130823 18 1  5
    130823 130823 18 1  5
    140823 140823 15 2  6
    140823 140823 15 2  6
    140823 140823 15 2  6
    140823 140823 15 2  6
    140823 140823 15 2  6
    140823 140823 15 2  6
    140823 140823 15 2  6
    140823 140823 15 2  6
    140823 140823 15 2  6
    140823 140823 15 2  6
    140823 140823 15 2  6
    140823 140823 15 2  6
    140823 140823 15 2  6
    140823 140823 15 2  6
    140823 140823 15 2  6
    140823 140823 15 2  6
    140823 140823 15 2  6
    140823 140823 15 2  6
    140823 140823 15 2  6
    140823 140823 15 2  6
    140823 140823 15 2  6
    140823 140823 15 2  6
    140823 140823 15 2  6
    140823 140823 15 2  6
    140823 140823 15 2  8
    140823 140823 15 2  8
    140823 140823 15 2  8
    140823 140823 15 2  8
    140823 140823 15 2  8
    140823 140823 15 2  8
    140823 140823 15 2  8
    140823 140823 15 2  8
    140823 140823 15 2  8
    140823 140823 15 2  8
    140823 140823 15 2  8
    140823 140823 15 2  8
    140823 140823 15 2  8
    140823 140823 15 2  8
    140823 140823 15 2  8
    140823 140823 15 2  8
    140823 140823 15 2  8
    140823 140823 15 2  8
    140823 140823 15 2  8
    140823 140823 15 2  8
    140823 140823 15 2  8
    140823 140823 15 2  8
    140823 140823 15 2  8
    140823 140823 15 2  8
    140823 140823 15 2  8
    140823 140823 18 2 10
    140823 140823 18 2 10
    140823 140823 18 2 10
    140823 140823 18 2 10
    140823 140823 18 2 10
    140823 140823 18 2 10
    140823 140823 18 2 10
    140823 140823 18 2 10
    140823 140823 18 2 10
    140823 140823 18 2 10
    140823 140823 18 2 10
    140823 140823 18 2 10
    140823 140823 18 2 10
    140823 140823 18 2 10
    140823 140823 18 2 10
    140823 140823 18 2 10
    140823 140823 18 2 10
    140823 140823 18 2 10
    140823 140823 18 2 10
    140823 140823 18 2 10
    140823 140823 18 2 10
    140823 140823 18 2 10
    140823 140823 18 2 10
    140823 140823 18 2 10
    140823 140823 18 2 10
    140823 140823 18 2 10
    140823 140823 18 2 10
    end

  • #2
    figured it out. thanks

    gen area2=.
    forvalues i= 8/108 {
    display `i'

    replace area2=area-1 if area==`i'

    }

    Comment


    • #3
      You don't need a loop for this. Or, equivalently: Stata will do this loop without explicit loop syntax.

      Code:
      gen area2 = area - 1  if inrange(area, 8, 108)
      is equivalent to your code in #2 while

      Code:
      gen area2 = area - 1  if inrange(area, 5, 109)
      seems implied by the question in #1.

      Incidentally, this block of Statalist is set aside for Mata questions: your question falls under General. That is not a problem except that many people interested in Stata but not Mata may rarely if ever read questions here.

      Comment

      Working...
      X