Some will judge me tough on the questioner already, so here is a reproducible example. The idea is that we switch on heating when it drops below 17 deg C (hereafter 17C), so the temperature rise needed is (17 - temp)C in one day if the temperature is below 17C and 0C otherwise. It is common to sum that over months, seasons or years; the questioner doesn't know about that, so I point in the direction of egen
's total()
function as the best tool. Either for individual days or summed over longer periods the units of measurement are deg C days or degree days with a convention about which temperature scale is being used (Celsius or Fahrenheit, etc.). Some may want to think of this graphically as a an area on a temperature-time graph, or as an integral approximated discretely.
. clear
. input temp date
temp date
1. 19 1
2. 18 2
3. 17 3
4. 16 4
5. 15 5
6. end
. gen heatday = max(0, 17 - temp)
. list
+-----------------------+
| temp date heatday |
|-----------------------|
1. | 19 1 0 |
2. | 18 2 0 |
3. | 17 3 0 |
4. | 16 4 1 |
5. | 15 5 2 |
+-----------------------+
The heart of the matter is thus max(0, 17 - temp)
. Another way to do it in Stata could be cond(temp < 17, 17 - temp, 0)
which divides opinion according to whether it is too long (bad) or perhaps more explicit (good).