Dear Community,

I am trying to construct a real rate of return on a bond of 2 years to maturity. The data I am using is the 2-year treasury constant maturity rate for the U.S. (gs2) and the CPI for All Urban Consumers: All Items for the U.S. (cpi).

The data for gs2 and cpi I am using is given below and has been constructed as monthly time series data.

I would like to construct the real rate variable as the difference between the nominal rate and inflation over the past 12 months.

How do I go about doing this?

Thank you


Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(gs2 cpi)
 7.06  56.8
 6.85  57.1
 6.63  57.4
 6.42  57.6
 5.98  57.9
 5.81    58
 5.38  58.2
  5.9  58.5
 6.09  59.1
 6.09  59.5
 5.96    60
 6.25  60.3
 6.13  60.7
 6.27    61
 6.61  61.2
 6.71  61.4
 7.11  61.6
 7.14  61.9
 7.18  62.1
 7.49  62.5
 7.57  62.9
 7.58  63.4
 7.74  63.9
 8.01  64.5
 8.24  65.2
 8.49  65.7
 8.37    66
 8.57  66.5
 8.85  67.1
 9.42  67.4
 9.72  67.7
 9.86  68.3
 9.72  69.1
 9.79  69.8
 9.78  70.6
 9.78  71.5
 9.22  72.3
 9.14  73.1
 9.46  73.8
10.06  74.6
11.49  75.2
11.81  75.9
11.39  76.7
 11.5  77.8
13.42  78.9
14.88  80.1
 12.5    81
 9.45  81.8
 8.73  82.7
 9.03  82.7
10.53  83.3
11.57    84
12.09  84.8
13.51  85.5
14.08  86.3
13.26    87
13.92  87.9
13.57  88.5
14.15  89.1
15.46  89.8
14.51  90.6
15.35  91.6
16.28  92.3
16.46  93.2
15.54  93.4
12.88  93.7
13.29    94
14.57  94.3
14.82  94.6
14.19  94.5
 14.2  94.9
13.78  95.8
14.47    97
 13.8  97.5
12.32  97.7
11.78  97.9
10.19  98.2
  9.8    98
 9.66  97.6
 9.33  97.8
 9.64  97.9
 9.66  97.9
 9.57  98.6
 9.49  99.2
10.18  99.5
10.69  99.9
11.07 100.2
10.79 100.7
10.57   101
10.66 101.2
10.84 101.3
10.64 101.9
10.79 102.4
11.31 102.6
11.69 103.1
12.47 103.4
12.91 103.7
12.88 104.1
12.43 104.5
 12.2   105
end