Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It seems you only want to compare consecutive years (by firm) which can be done like this:</p> <pre><code>clear all set more off input firm_id year total_workers 1 1975 614 1 1976 68 1 1977 708 1 1978 18 1 1979 536 3 1975 154 3 1976 59 3 1977 115 3 1978 40 3 1979 380 4 1975 49 4 1976 42 4 1977 53 4 1978 54 4 1979 34 5 1975 254 5 1976 1115 end sort firm_id year // important list, sepby(firm_id) by firm_id: gen flag = (total_workers[_n] &gt; 10*total_workers[_n-1]) list if flag == 1 </code></pre> <p>The important points are the <code>sort</code> and the use of <a href="http://www.stata.com/help.cgi?subscripting" rel="nofollow">subscripting</a>.</p> <p>To make it one line shorter, you can incorporate the <code>sort</code> into the main instruction like this:</p> <pre><code>bysort firm_id (year): gen flag = (total_workers[_n] &gt; 10*total_workers[_n-1]) </code></pre> <p>The reason your code doesn't work is that Stata evaluates it on the dataset line by line (i.e. observation by observation from top to bottom) - I appreciate any correction if there's an error here -. You are asking Stata to tabulate only if one observed instance of the variable is larger than itself multiplied by 10 which is impossible (i.e. always false). See the output of the following:</p> <pre><code>bysort firm_id (year): gen flag = 1 if total_workers &gt; 10*total_workers </code></pre> <p>Using subscripts explicitly, the previous line is equivalent to</p> <pre><code>bysort firm_id (year): gen flag = 1 if total_workers[_n] &gt; 10*total_workers[_n] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload