Here is some toy data I made up, along with code that does the calculations:
-runby- is written by Robert Picard and me, and is available from SSC.
In the future, when asking for help with code, please use the -dataex- command to provide example data, as I have done here.
If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.
When asking for help with code, always show example data. When showing example data, always use -dataex-.
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float(region date rainfall) 1 1935 8.032198 1 1936 8.447136 1 1937 8.591763 1 1938 7.934585 1 1939 8.369435 1 1940 8.44331 1 1941 8.423146 1 1942 8.084593 1 1943 8.307385 1 1944 8.3846445 1 1945 8.484856 1 1946 8.497174 1 1947 8.168061 1 1948 8.087855 1 1949 8.216142 1 1950 8.231004 1 1951 8.483223 1 1952 8.502059 1 1953 8.739008 1 1954 8.629378 2 1935 7.217003 2 1936 7.499479 2 1937 7.89219 2 1938 7.496597 2 1939 7.579321 2 1940 7.69753 2 1941 7.775066 2 1942 7.681837 2 1943 7.593424 2 1944 7.503234 2 1945 7.523049 2 1946 7.634192 2 1947 7.493707 2 1948 7.393755 2 1949 7.418781 2 1950 7.425 2 1951 7.736089 2 1952 7.677586 2 1953 7.616431 2 1954 7.657046 3 1935 7.065272 3 1936 7.608771 3 1937 7.938552 3 1938 7.620558 3 1939 7.721437 3 1940 7.664909 3 1941 7.514309 3 1942 7.370231 3 1943 7.467028 3 1944 7.430826 3 1945 7.604745 3 1946 7.699978 3 1947 7.412583 3 1948 7.380505 3 1949 7.266688 3 1950 7.3843 3 1951 7.506262 3 1952 7.639979 3 1953 7.77132 3 1954 7.92295 4 1935 6.034285 4 1936 6.730779 4 1937 6.784344 4 1938 6.081991 4 1939 6.521652 4 1940 6.590026 4 1941 6.467077 4 1942 6.01835 4 1943 6.377407 4 1944 6.548792 4 1945 6.740992 4 1946 6.795482 4 1947 6.361302 4 1948 6.543336 4 1949 6.380631 4 1950 6.541751 4 1951 6.695799 4 1952 6.588926 4 1953 6.909254 4 1954 6.555641 5 1935 5.060695 5 1936 5.123369 5 1937 5.262172 5 1938 5.054333 5 1939 5.254365 5 1940 5.223055 5 1941 5.296315 5 1942 5.244389 5 1943 5.018603 5 1944 5.234845 5 1945 5.369242 5 1946 5.450609 5 1947 5.517453 5 1948 5.413876 5 1949 5.469325 5 1950 5.481056 5 1951 5.790877 5 1952 5.884436 5 1953 5.987456 5 1954 5.901814 end format %ty date capture program drop one_region program define one_region centile rainfall, centile(20 80) gen pctile20 = r(c_1) gen pctile80 = r(c_2) exit end runby one_region, by(region) gen int shock = 0 if !missing(rainfall) replace shock = -1 if rainfall < pctile20 replace shock = 1 if rainfall > pctile80
In the future, when asking for help with code, please use the -dataex- command to provide example data, as I have done here.
If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.
When asking for help with code, always show example data. When showing example data, always use -dataex-.
Comment