Announcement

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

  • Replace a certain repeated value

    Hello,
    I have a panel and I have an event (turnover). The thing is sometimes i have this event twice and i need only to consider the first event. So when turnover=1 twice for the same id, y want to replace the second turnover to zero. In the example below, you can see turnover happens twice on id=2, once in 1930 and once in 1933. I want to change the 1933 turnover dummy to zero. This for a long panel data, so replace turnover=0 if id==2 & year==1933 would take me days. Is there a simpler way?
    Code:
    id year turnover
     2 1926 .
     2 1927 .
     2 1928 0
     2 1929 0
     2 1930 1
     2 1931 0
     2 1932 0
     2 1933 1
     2 1934 0
     2 1935 .
     3 1926 .
     3 1927 .
     3 1928 0
     3 1929 0
     3 1930 0
     3 1931 0
     3 1932 0
     3 1933 0
     3 1934 0
     3 1935 .
     4 1926 .
     4 1927 .
     4 1928 0
     4 1929 0
     4 1930 1
     4 1931 0
     4 1932 0
     4 1933 0
     4 1934 0
     4 1935 .
     end
    thanks

  • #2
    This seems to do the job (I altered your sample data to make a more interesting example.)
    Code:
    clear
    input float(id year turnover)
    2 1926 .
    2 1927 .
    2 1928 0
    2 1929 0
    2 1930 1
    2 1931 0
    2 1932 0
    2 1933 1
    2 1934 0
    2 1935 .
    3 1926 .
    3 1927 .
    3 1928 0
    3 1929 1
    3 1930 0
    3 1931 1
    3 1932 0
    3 1933 1
    3 1934 0
    3 1935 .
    4 1926 .
    4 1927 .
    4 1928 0
    4 1929 0
    4 1930 1
    4 1931 1
    4 1932 1
    4 1933 0
    4 1934 0
    4 1935 .
    end
    generate newturnover = turnover
    bysort id (year): replace newturnover = 0 if turnover==1 & sum(turnover)>1
    list, noobs sepby(id)
    Code:
      +---------------------------------+
      | id   year   turnover   newtur~r |
      |---------------------------------|
      |  2   1926          .          . |
      |  2   1927          .          . |
      |  2   1928          0          0 |
      |  2   1929          0          0 |
      |  2   1930          1          1 |
      |  2   1931          0          0 |
      |  2   1932          0          0 |
      |  2   1933          1          0 |
      |  2   1934          0          0 |
      |  2   1935          .          . |
      |---------------------------------|
      |  3   1926          .          . |
      |  3   1927          .          . |
      |  3   1928          0          0 |
      |  3   1929          1          1 |
      |  3   1930          0          0 |
      |  3   1931          1          0 |
      |  3   1932          0          0 |
      |  3   1933          1          0 |
      |  3   1934          0          0 |
      |  3   1935          .          . |
      |---------------------------------|
      |  4   1926          .          . |
      |  4   1927          .          . |
      |  4   1928          0          0 |
      |  4   1929          0          0 |
      |  4   1930          1          1 |
      |  4   1931          1          0 |
      |  4   1932          1          0 |
      |  4   1933          0          0 |
      |  4   1934          0          0 |
      |  4   1935          .          . |
      +---------------------------------+

    Comment


    • #3
      Thanks! worked perfectly

      Comment

      Working...
      X