I am using Stata version 16 and would need some help in creating of loops to automate data. When I intend to do is to extract data from different rows in another column. In each of such columns, I need data from the column "unemp_data" but data would last for the quarters before the recession date, followed by 10 quarters after the recession date. For example, the first recession starts at 1974Q3 and so data would be from row 1 to 33. It is then inputted into different rows depending on the number of quarters before the recession. The column "unemp_time" is the number of quarters before a recession date, with 0 means the recession has started. For now, I intend to create 2 new columns. The first column is unemployment data from the column "unemp_data" and would have data from the first 33 rows of that column and would be inputted on row 14 to 46 as shown below.
The second column would have data from the same column and would start from the first recession onwards (1974Q3) and end at row 55 but since it is 22 quarters before the recession, it would start at row 14 of the column "unemp_time" and end at row 46.
This is an example of my dataset:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float id str3 location str7 time long quarters float unemp_data byte pt_output float(spell quarters_to_start rec1 rec2 unemp_time r1) 1 "AUT" "1969-Q1" 1 2.28056 . 0 22 1.308654 1.729307 -35 . 2 "AUT" "1969-Q2" 2 1.799027 . 0 22 1.308654 1.729307 -34 . 3 "AUT" "1969-Q3" 3 1.735777 . 0 22 1.308654 1.729307 -33 . 4 "AUT" "1969-Q4" 4 1.718443 . 0 22 1.308654 1.729307 -32 . 5 "AUT" "1970-Q1" 5 1.538099 . 0 22 1.308654 1.729307 -31 . 6 "AUT" "1970-Q2" 6 1.328224 . 0 22 1.308654 1.729307 -30 . 7 "AUT" "1970-Q3" 7 1.198998 . 0 22 1.308654 1.729307 -29 . 8 "AUT" "1970-Q4" 8 1.225661 . 0 22 1.308654 1.729307 -28 . 9 "AUT" "1971-Q1" 9 1.173203 . 0 22 1.308654 1.729307 -27 . 10 "AUT" "1971-Q2" 10 1.33824 . 0 22 1.308654 1.729307 -26 . 11 "AUT" "1971-Q3" 11 1.042691 . 0 22 1.308654 1.729307 -25 . 12 "AUT" "1971-Q4" 12 1.277456 . 0 22 1.308654 1.729307 -24 . 13 "AUT" "1972-Q1" 13 1.15306 . 0 22 1.308654 1.729307 -23 . 14 "AUT" "1972-Q2" 14 1.064005 . 0 22 1.308654 1.729307 -22 .9719061 15 "AUT" "1972-Q3" 15 1.357699 . 0 22 1.308654 1.729307 -21 .490373 16 "AUT" "1972-Q4" 16 1.039917 . 0 22 1.308654 1.729307 -20 .4271231 17 "AUT" "1973-Q1" 17 1.035841 . 0 22 1.308654 1.729307 -19 .4097891 18 "AUT" "1973-Q2" 18 1.177305 . 0 22 1.308654 1.729307 -18 .2294451 19 "AUT" "1973-Q3" 19 1.108755 . 0 22 1.308654 1.729307 -17 .019569993 20 "AUT" "1973-Q4" 20 .9943526 . 0 22 1.308654 1.729307 -16 -.10965598 21 "AUT" "1974-Q1" 21 1.26808 . 0 22 1.308654 1.729307 -15 -.08299291 22 "AUT" "1974-Q2" 22 1.299121 . 0 22 1.308654 1.729307 -14 -.13545096 23 "AUT" "1974-Q3" 23 1.308654 1 1 22 1.308654 1.729307 -13 .029586077 24 "AUT" "1974-Q4" 24 1.478947 . 1 22 1.308654 1.729307 -12 -.26596296 25 "AUT" "1975-Q1" 25 1.452747 . 1 22 1.308654 1.729307 -11 -.031197906 26 "AUT" "1975-Q2" 26 1.88046 2 1 22 1.308654 1.729307 -10 -.155594 27 "AUT" "1975-Q3" 27 1.734818 . 1 22 1.308654 1.729307 -9 -.24464893 28 "AUT" "1975-Q4" 28 1.982104 . 1 22 1.308654 1.729307 -8 .04904509 29 "AUT" "1976-Q1" 29 1.854092 . 1 22 1.308654 1.729307 -7 -.26873696 30 "AUT" "1976-Q2" 30 1.830328 . 1 22 1.308654 1.729307 -6 -.27281296 31 "AUT" "1976-Q3" 31 1.804084 . 1 22 1.308654 1.729307 -5 -.13134897 32 "AUT" "1976-Q4" 32 1.657884 . 1 22 1.308654 1.729307 -4 -.19989896 33 "AUT" "1977-Q1" 33 1.556965 . 1 22 1.308654 1.729307 -3 -.3143014 34 "AUT" "1977-Q2" 34 1.492262 . 1 22 1.308654 1.729307 -2 -.04057395 35 "AUT" "1977-Q3" 35 1.743809 . 1 22 1.308654 1.729307 -1 -.009532928 36 "AUT" "1977-Q4" 36 1.708798 . 1 22 1.308654 1.729307 0 0 37 "AUT" "1978-Q1" 37 1.78792 . 1 22 1.308654 1.729307 1 .1702931 38 "AUT" "1978-Q2" 38 2.194976 . 1 22 1.308654 1.729307 2 .14409304 39 "AUT" "1978-Q3" 39 2.050228 . 1 22 1.308654 1.729307 3 .5718061 40 "AUT" "1978-Q4" 40 2.223532 . 1 22 1.308654 1.729307 4 .426164 41 "AUT" "1979-Q1" 41 2.196454 . 1 22 1.308654 1.729307 5 .67345 42 "AUT" "1979-Q2" 42 2.063658 . 1 22 1.308654 1.729307 6 .54543805 43 "AUT" "1979-Q3" 43 2.00938 . 1 22 1.308654 1.729307 7 .52167404 44 "AUT" "1979-Q4" 44 2.050331 . 1 22 1.308654 1.729307 8 .49543 45 "AUT" "1980-Q1" 45 1.729307 1 2 9 1.308654 1.729307 9 .34923005 46 "AUT" "1980-Q2" 46 1.763711 . 2 9 1.308654 1.729307 10 .24831104 47 "AUT" "1980-Q3" 47 2.052905 . 2 9 1.308654 1.729307 11 . 48 "AUT" "1980-Q4" 48 1.885693 . 2 9 1.308654 1.729307 12 . 49 "AUT" "1981-Q1" 49 2.130085 2 2 9 1.308654 1.729307 13 . 50 "AUT" "1981-Q2" 50 2.577704 . 2 9 1.308654 1.729307 14 . 51 "AUT" "1981-Q3" 51 2.53258 . 2 9 1.308654 1.729307 15 . 52 "AUT" "1981-Q4" 52 2.889663 . 2 9 1.308654 1.729307 16 . 53 "AUT" "1982-Q1" 53 2.902107 . 2 9 1.308654 1.729307 17 . 54 "AUT" "1982-Q2" 54 3.268415 1 3 6 1.308654 1.729307 18 . 55 "AUT" "1982-Q3" 55 3.767183 . 3 6 1.308654 1.729307 19 . 56 "AUT" "1982-Q4" 56 4.004116 2 3 6 1.308654 1.729307 20 . 57 "AUT" "1983-Q1" 57 4.186985 . 3 6 1.308654 1.729307 21 . 58 "AUT" "1983-Q2" 58 4.326129 . 3 6 1.308654 1.729307 22 . 59 "AUT" "1983-Q3" 59 4.280576 . 3 6 1.308654 1.729307 23 . 60 "AUT" "1983-Q4" 60 3.7363 1 4 35 1.308654 1.729307 24 . 61 "AUT" "1984-Q1" 61 3.937536 . 4 35 1.308654 1.729307 25 . 62 "AUT" "1984-Q2" 62 3.801948 2 4 35 1.308654 1.729307 26 . 63 "AUT" "1984-Q3" 63 3.670202 . 4 35 1.308654 1.729307 27 . 64 "AUT" "1984-Q4" 64 3.740511 . 4 35 1.308654 1.729307 28 . 65 "AUT" "1985-Q1" 65 3.584991 . 4 35 1.308654 1.729307 29 . 66 "AUT" "1985-Q2" 66 3.616743 . 4 35 1.308654 1.729307 30 . 67 "AUT" "1985-Q3" 67 3.645316 . 4 35 1.308654 1.729307 31 . 68 "AUT" "1985-Q4" 68 3.545668 . 4 35 1.308654 1.729307 32 . 69 "AUT" "1986-Q1" 69 3.312451 . 4 35 1.308654 1.729307 33 . 70 "AUT" "1986-Q2" 70 2.947661 . 4 35 1.308654 1.729307 34 . 71 "AUT" "1986-Q3" 71 2.836876 . 4 35 1.308654 1.729307 35 . 72 "AUT" "1986-Q4" 72 3.318925 . 4 35 1.308654 1.729307 36 . 73 "AUT" "1987-Q1" 73 3.932383 . 4 35 1.308654 1.729307 37 . 74 "AUT" "1987-Q2" 74 3.714838 . 4 35 1.308654 1.729307 38 . 75 "AUT" "1987-Q3" 75 3.986203 . 4 35 1.308654 1.729307 39 . 76 "AUT" "1987-Q4" 76 3.516049 . 4 35 1.308654 1.729307 40 . 77 "AUT" "1988-Q1" 77 3.595898 . 4 35 1.308654 1.729307 41 . 78 "AUT" "1988-Q2" 78 3.492004 . 4 35 1.308654 1.729307 42 . 79 "AUT" "1988-Q3" 79 3.606824 . 4 35 1.308654 1.729307 43 . 80 "AUT" "1988-Q4" 80 3.512885 . 4 35 1.308654 1.729307 44 . 81 "AUT" "1989-Q1" 81 3.068615 . 4 35 1.308654 1.729307 45 . 82 "AUT" "1989-Q2" 82 3.235731 . 4 35 1.308654 1.729307 46 . 83 "AUT" "1989-Q3" 83 2.959549 . 4 35 1.308654 1.729307 47 . 84 "AUT" "1989-Q4" 84 3.307182 . 4 35 1.308654 1.729307 48 . 85 "AUT" "1990-Q1" 85 3.153923 . 4 35 1.308654 1.729307 49 . 86 "AUT" "1990-Q2" 86 3.269969 . 4 35 1.308654 1.729307 50 . 87 "AUT" "1990-Q3" 87 3.412282 . 4 35 1.308654 1.729307 51 . 88 "AUT" "1990-Q4" 88 3.239165 . 4 35 1.308654 1.729307 52 . 89 "AUT" "1991-Q1" 89 3.383817 . 4 35 1.308654 1.729307 53 . 90 "AUT" "1991-Q2" 90 3.44205 . 4 35 1.308654 1.729307 54 . 91 "AUT" "1991-Q3" 91 3.58801 . 4 35 1.308654 1.729307 55 . 92 "AUT" "1991-Q4" 92 3.613474 . 4 35 1.308654 1.729307 56 . 93 "AUT" "1992-Q1" 93 3.392481 . 4 35 1.308654 1.729307 57 . 94 "AUT" "1992-Q2" 94 3.799317 . 4 35 1.308654 1.729307 58 . 95 "AUT" "1992-Q3" 95 3.598001 1 5 33 1.308654 1.729307 59 . 96 "AUT" "1992-Q4" 96 3.767504 . 5 33 1.308654 1.729307 60 . 97 "AUT" "1993-Q1" 97 3.833333 2 5 33 1.308654 1.729307 61 . 98 "AUT" "1993-Q2" 98 3.966667 . 5 33 1.308654 1.729307 62 . 99 "AUT" "1993-Q3" 99 4.033333 . 5 33 1.308654 1.729307 63 . 100 "AUT" "1993-Q4" 100 4 . 5 33 1.308654 1.729307 64 . end
This the code for my loops:
Code:
foreach var of varlist quarters { if `var' <=33 { gen r1 = unemp_data[_n-13] - rec1[1] in 1/46 } else if `var'>=23 & `var'<=55{ gen r2 = unemp_data[_n+9] - rec2[1] if _n>= 14 in 14/46 } }
0 Response to Help needed: Using loops to automate extracting of data
Post a Comment