Please, I have the daily rates of the U.S Treasuries and I'm trying to create a variable that converts these daily rates into an average by quarter. Normally what i do is using this line of command
bysort qdate: egen avrgq_ust_yield_1m = mean(ust_yield_1m) - command 1
where:
"qdate" is the year-quarter of the daily rate
"avrgq_ust_yield_1m" is the new variable that has the average of the yield over the quarter
"ust_yield_1m" a variable that has the daily rate of one month Treasury.
My question is:
I have 12 of these Treasures, from 1 month to 30 years. so instead of repeating command 1 12 times, i want to do it in a loop, foreach var in varlist {command 1, where "avrgq" abbreviates each variable in the variables' list}, Is that possible?
where
varlist would be local macro, generated for example as
local treasury ust_yield_1m ust_yield_2m and so on.
Here is an example of the data
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str19 date_time_string double(ust_yield_1m ust_yield_2m ust_yield_3m ust_yield_6m ust_yield_1y ust_yield_2y ust_yield_3y ust_yield_5y ust_yield_7y ust_yield_10y ust_yield_20y ust_yield_30y) str10 date_string float(date qdate) "1990-01-02T00:00:00" . . 7.82999992370605 7.88999986648559 7.80999994277954 7.86999988555908 7.90000009536743 7.86999988555908 7.98000001907348 7.94000005722045 . 8 "1990-01-02" 10959 120 "1990-01-03T00:00:00" . . 7.88999986648559 7.94000005722045 7.84999990463256 7.94000005722045 7.96000003814697 7.92000007629394 8.03999996185302 7.98999977111816 . 8.03999996185302 "1990-01-03" 10960 120 "1990-01-04T00:00:00" . . 7.84000015258789 7.90000009536743 7.82000017166137 7.92000007629394 7.92999982833862 7.9099998474121 8.02000045776367 7.98000001907348 . 8.03999996185302 "1990-01-04" 10961 120 "1990-01-05T00:00:00" . . 7.78999996185302 7.84999990463256 7.78999996185302 7.90000009536743 7.94000005722045 7.92000007629394 8.02999973297119 7.98999977111816 . 8.0600004196167 "1990-01-05" 10962 120 "1990-01-08T00:00:00" . . 7.78999996185302 7.88000011444091 7.80999994277954 7.90000009536743 7.94999980926513 7.92000007629394 8.05000019073486 8.02000045776367 . 8.09000015258789 "1990-01-08" 10965 120 "1990-01-09T00:00:00" . . 7.80000019073486 7.82000017166137 7.78000020980835 7.9099998474121 7.94000005722045 7.92000007629394 8.05000019073486 8.02000045776367 . 8.10000038146972 "1990-01-09" 10966 120 "1990-01-10T00:00:00" . . 7.75 7.78000020980835 7.76999998092651 7.9099998474121 7.94999980926513 7.92000007629394 8 8.02999973297119 . 8.10999965667724 "1990-01-10" 10967 120 "1990-01-11T00:00:00" . . 7.80000019073486 7.80000019073486 7.76999998092651 7.9099998474121 7.94999980926513 7.94000005722045 8.01000022888183 8.03999996185302 . 8.10999965667724 "1990-01-11" 10968 120 "1990-01-12T00:00:00" . . 7.73999977111816 7.80999994277954 7.76000022888183 7.92999982833862 7.98000001907348 7.98999977111816 8.06999969482421 8.10000038146972 . 8.17000007629394 "1990-01-12" 10969 120 "1990-01-16T00:00:00" . . 7.88999986648559 7.98999977111816 7.92000007629394 8.10000038146972 8.13000011444091 8.10999965667724 8.18000030517578 8.19999980926513 . 8.25 "1990-01-16" 10973 120 "1990-01-17T00:00:00" . . 7.96999979019165 7.96999979019165 7.9099998474121 8.09000015258789 8.10999965667724 8.10999965667724 8.17000007629394 8.1899995803833 . 8.25 "1990-01-17" 10974 120 "1990-01-18T00:00:00" . . 8.03999996185302 8.07999992370605 8.05000019073486 8.25 8.27999973297119 8.27000045776367 8.3100004196167 8.31999969482421 . 8.35000038146972 "1990-01-18" 10975 120 "1990-01-19T00:00:00" . . 8 8.01000022888183 8 8.19999980926513 8.22999954223632 8.19999980926513 8.23999977111816 8.26000022888183 . 8.28999996185302 "1990-01-19" 10976 120 "1990-01-22T00:00:00" . . 7.98999977111816 7.98999977111816 7.98000001907348 8.18000030517578 8.19999980926513 8.1899995803833 8.25 8.27000045776367 . 8.3100004196167 "1990-01-22" 10979 120 "1990-01-23T00:00:00" . . 7.92999982833862 7.96999979019165 7.96999979019165 8.18000030517578 8.19999980926513 8.18000030517578 8.22999954223632 8.26000022888183 . 8.28999996185302 "1990-01-23" 10980 120 "1990-01-24T00:00:00" . . 7.92999982833862 7.98999977111816 8 8.19999980926513 8.28999996185302 8.27999973297119 8.34000015258789 8.38000011444091 . 8.40999984741211 "1990-01-24" 10981 120 "1990-01-25T00:00:00" . . 7.94999980926513 8.01000022888183 8.02999973297119 8.23999977111816 8.31999969482421 8.3100004196167 8.39000034332275 8.42000007629394 . 8.46000003814697 "1990-01-25" 10982 120 "1990-01-26T00:00:00" . . 7.92999982833862 8.03999996185302 8.06999969482421 8.27999973297119 8.38000011444091 8.38000011444091 8.44999980926513 8.48999977111816 . 8.55000019073486 "1990-01-26" 10983 120 "1990-01-29T00:00:00" . . 8 8.09000015258789 8.07999992370605 8.30000019073486 8.39000034332275 8.39000034332275 8.44999980926513 8.5 . 8.53999996185302 "1990-01-29" 10986 120 "1990-01-30T00:00:00" . . 8 8.14000034332275 8.09000015258789 8.30000019073486 8.39000034332275 8.43000030517578 8.4700002670288 8.51000022888183 . 8.55000019073486 "1990-01-30" 10987 120 "1990-01-31T00:00:00" . . 8 8.13000011444091 8.07999992370605 8.27999973297119 8.35999965667724 8.35000038146972 8.39000034332275 8.43000030517578 . 8.46000003814697 "1990-01-31" 10988 120 "1990-02-01T00:00:00" . . 8.02000045776367 8.13000011444091 8.09000015258789 8.27999973297119 8.35000038146972 8.35000038146972 8.38000011444091 8.42000007629394 . 8.4399995803833 "1990-02-01" 10989 120 "1990-02-02T00:00:00" . . 8.06999969482421 8.17000007629394 8.13000011444091 8.36999988555908 8.43000030517578 8.42000007629394 8.46000003814697 8.5 . 8.51000022888183 "1990-02-02" 10990 120 "1990-02-05T00:00:00" . . 8.07999992370605 8.18000030517578 8.14999961853027 8.36999988555908 8.43000030517578 8.4399995803833 8.47999954223632 8.52999973297119 . 8.52999973297119 "1990-02-05" 10993 120 "1990-02-06T00:00:00" . . 8.09000015258789 8.17000007629394 8.14999961853027 8.42000007629394 8.43000030517578 8.48999977111816 8.52000045776367 8.56999969482421 . 8.57999992370605 "1990-02-06" 10994 120 "1990-02-07T00:00:00" . . 8.07999992370605 8.1899995803833 8.17000007629394 8.42000007629394 8.43000030517578 8.51000022888183 8.52999973297119 8.52000045776367 . 8.56999969482421 "1990-02-07" 10995 120 "1990-02-08T00:00:00" . . 8.07999992370605 8.18000030517578 8.13000011444091 8.36999988555908 8.39000034332275 8.44999980926513 8.47999954223632 8.48999977111816 . 8.5 "1990-02-08" 10996 120 "1990-02-09T00:00:00" . . 8.02000045776367 8.09000015258789 8.02999973297119 8.25 8.23999977111816 8.28999996185302 8.34000015258789 8.3100004196167 . 8.35999965667724 "1990-02-09" 10997 120 "1990-02-12T00:00:00" . . 7.98999977111816 8.10999965667724 8.0600004196167 8.31999969482421 8.31999969482421 8.36999988555908 8.40999984741211 8.39999961853027 . 8.43000030517578 "1990-02-12" 11000 120 "1990-02-13T00:00:00" . . 7.84000015258789 7.96999979019165 7.94999980926513 8.21000003814697 8.21000003814697 8.28999996185302 8.35999965667724 8.35000038146972 . 8.39000034332275 "1990-02-13" 11001 120 "1990-02-14T00:00:00" . . 7.86999988555908 8 8.02999973297119 8.27000045776367 8.27000045776367 8.34000015258789 8.35999965667724 8.35999965667724 . 8.40999984741211 "1990-02-14" 11002 120 "1990-02-15T00:00:00" . . 7.96999979019165 8.07999992370605 8.10999965667724 8.35000038146972 8.35000038146972 8.39999961853027 8.4399995803833 8.43000030517578 . 8.4700002670288 "1990-02-15" 11003 120 "1990-02-16T00:00:00" . . 7.94000005722045 8.06999969482421 8.09000015258789 8.35999965667724 8.34000015258789 8.40999984741211 8.43000030517578 8.42000007629394 . 8.46000003814697 "1990-02-16" 11004 120 "1990-02-20T00:00:00" . . 8.0600004196167 8.19999980926513 8.23999977111816 8.52000045776367 8.52000045776367 8.57999992370605 8.63000011444091 8.61999988555908 . 8.65999984741211 "1990-02-20" 11008 120 "1990-02-21T00:00:00" . . 8.01000022888183 8.1899995803833 8.2200002670288 8.5 8.52000045776367 8.60000038146972 8.63000011444091 8.61999988555908 . 8.65999984741211 "1990-02-21" 11009 120 "1990-02-22T00:00:00" . . 7.98999977111816 8.14999961853027 8.17000007629394 8.44999980926513 8.47999954223632 8.47999954223632 8.56999969482421 8.53999996185302 . 8.5600004196167 "1990-02-22" 11010 120 "1990-02-23T00:00:00" . . 7.92999982833862 8.09000015258789 8.10999965667724 8.43000030517578 8.44999980926513 8.4700002670288 8.5600004196167 8.52999973297119 . 8.5600004196167 "1990-02-23" 11011 120 "1990-02-26T00:00:00" . . 7.96999979019165 8.09000015258789 8.10000038146972 8.38000011444091 8.39999961853027 8.39999961853027 8.48999977111816 8.46000003814697 . 8.48999977111816 "1990-02-26" 11014 120 "1990-02-27T00:00:00" . . 7.98999977111816 8.10000038146972 8.0600004196167 8.34000015258789 8.34000015258789 8.34000015258789 8.43000030517578 8.40999984741211 . 8.44999980926513 "1990-02-27" 11015 120 "1990-02-28T00:00:00" . . 8.03999996185302 8.14000034332275 8.11999988555908 8.43000030517578 8.44999980926513 8.4399995803833 8.53999996185302 8.51000022888183 . 8.53999996185302 "1990-02-28" 11016 120 "1990-03-01T00:00:00" . . 8.07999992370605 8.1899995803833 8.21000003814697 8.52999973297119 8.52999973297119 8.52999973297119 8.61999988555908 8.59000015258789 . 8.60999965667724 "1990-03-01" 11017 120 "1990-03-02T00:00:00" . . 8.02000045776367 8.17000007629394 8.2200002670288 8.47999954223632 8.5 8.5 8.56999969482421 8.53999996185302 . 8.55000019073486 "1990-03-02" 11018 120 "1990-03-05T00:00:00" . . 8.09000015258789 8.27000045776367 8.3100004196167 8.55000019073486 8.59000015258789 8.57999992370605 8.6899995803833 8.64999961853027 . 8.65999984741211 "1990-03-05" 11021 120 "1990-03-06T00:00:00" . . 8.17000007629394 8.23999977111816 8.3100004196167 8.53999996185302 8.5600004196167 8.56999969482421 8.64000034332275 8.59000015258789 . 8.59000015258789 "1990-03-06" 11022 120 "1990-03-07T00:00:00" . . 8.18000030517578 8.2200002670288 8.28999996185302 8.53999996185302 8.55000019073486 8.5600004196167 8.63000011444091 8.57999992370605 . 8.57999992370605 "1990-03-07" 11023 120 "1990-03-08T00:00:00" . . 8.18000030517578 8.23999977111816 8.35000038146972 8.59000015258789 8.57999992370605 8.56999969482421 8.61999988555908 8.56999969482421 . 8.5600004196167 "1990-03-08" 11024 120 "1990-03-09T00:00:00" . . 8.26000022888183 8.32999992370605 8.46000003814697 8.72999954223632 8.71000003814697 8.68000030517578 8.71000003814697 8.64999961853027 . 8.63000011444091 "1990-03-09" 11025 120 "1990-03-12T00:00:00" . . 8.26000022888183 8.32999992370605 8.4399995803833 8.71000003814697 8.69999980926513 8.67000007629394 8.69999980926513 8.63000011444091 . 8.61999988555908 "1990-03-12" 11028 120 "1990-03-13T00:00:00" . . 8.26000022888183 8.34000015258789 8.47999954223632 8.77000045776367 8.77000045776367 8.75 8.77999973297119 8.72999954223632 . 8.7200002670288 "1990-03-13" 11029 120 "1990-03-14T00:00:00" . . 8.22999954223632 8.3100004196167 8.40999984741211 8.6899995803833 8.69999980926513 8.67000007629394 8.71000003814697 8.64999961853027 . 8.60999965667724 "1990-03-14" 11030 120 end format %td date format %tq qdate
Thank you in advance for your valuable assistance.
0 Response to Using loop to create mean variables
Post a Comment