Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Converting dd-mm-yyyy to dd-mm

    Dear Statalist community,

    I have a question on date variables and couldn't find a solution in the help dates file. I have a dataset that looks as follows:

    Code:
    l lat lon id start end in 1/21, sep(7)
    
         +-----------------------------------------------------+
         |        lat         lon   id       start         end |
         |-----------------------------------------------------|
      1. | -34.463232   19.542468    1   01jul1982   01jan1983 |
      2. | -34.463232   19.542468    1   15jul1983   01nov1983 |
      3. | -34.463232   19.542468    1   15jun1984   01nov1984 |
      4. | -34.463232   19.542468    1   15feb1985   01nov1985 |
      5. | -34.463232   19.542468    1   01jul1986   01dec1986 |
      6. | -34.463232   19.542468    1   15jun1987   15nov1987 |
      7. | -34.463232   19.542468    1   01jul1988   01nov1988 |
         +-----------------------------------------------------+
    
    end
    However, I am only interested in the time of the year, not the year itself. Thus, I would like to convert dd-mm-yyyy to dd-mm to get a dataset that looks like the following and does not contain the information about the year anymore:

    Code:
    l lat lon id start end in 1/7, sep(7)
    
         +-----------------------------------------------------+
         |        lat         lon   id       start       end   |
         |-----------------------------------------------------|
      1. | -34.463232   19.542468    1       01jul      01jan  |
      2. | -34.463232   19.542468    1       15jul      01nov  |
      3. | -34.463232   19.542468    1       15jun      01nov  |
      4. | -34.463232   19.542468    1       15feb      01nov  |
      5. | -34.463232   19.542468    1       01jul      01dec  |
      6. | -34.463232   19.542468    1       15jun      15nov  |
      7. | -34.463232   19.542468    1       01jul      01nov  |
         +-----------------------------------------------------+
    
    end
    Is that possible? Thank you for your help!
    Best,
    Nora

  • #2
    See

    Code:
    help doy()
    for day of the year. "1 Jan" is 1 and "31 Dec" is 366.

    doy(e_d)
    Description: the numeric day of the year corresponding to date e_d
    Domain e_d: %td dates 01jan0100 to 31dec9999 (integers -679,350 to 2,936,549)
    Range: integers 1 to 366 or missing
    To generate labels, you may try something like:

    Code:
    clear
    set obs 2
    gen date= cond(_n==1, 0, td(31dec1960))
    format date %td
    tsset date
    tsfill, full
    foreach unit in month day{
        gen `unit'= `unit'(date)
    }
    lab def months 1 "Jan" 2 "Feb" 3 "Mar"  4 "Apr" 5 "May" 6 "Jun" 7 "Jul" 8 "Aug" 9 "Sep" 10 "Oct" 11 "Nov" 12 "Dec"
    lab values month months
    egen wanted= group(month day), label
    Res.:

    Code:
    . l, sep(0)
    
         +----------------------------------+
         |      date   month   day   wanted |
         |----------------------------------|
      1. | 01jan1960     Jan     1    Jan 1 |
      2. | 02jan1960     Jan     2    Jan 2 |
      3. | 03jan1960     Jan     3    Jan 3 |
      4. | 04jan1960     Jan     4    Jan 4 |
      5. | 05jan1960     Jan     5    Jan 5 |
      6. | 06jan1960     Jan     6    Jan 6 |
      7. | 07jan1960     Jan     7    Jan 7 |
      8. | 08jan1960     Jan     8    Jan 8 |
      9. | 09jan1960     Jan     9    Jan 9 |
     10. | 10jan1960     Jan    10   Jan 10 |
     11. | 11jan1960     Jan    11   Jan 11 |
     12. | 12jan1960     Jan    12   Jan 12 |
     13. | 13jan1960     Jan    13   Jan 13 |
     14. | 14jan1960     Jan    14   Jan 14 |
     15. | 15jan1960     Jan    15   Jan 15 |
     16. | 16jan1960     Jan    16   Jan 16 |
     17. | 17jan1960     Jan    17   Jan 17 |
     18. | 18jan1960     Jan    18   Jan 18 |
     19. | 19jan1960     Jan    19   Jan 19 |
     20. | 20jan1960     Jan    20   Jan 20 |
     21. | 21jan1960     Jan    21   Jan 21 |
     22. | 22jan1960     Jan    22   Jan 22 |
     23. | 23jan1960     Jan    23   Jan 23 |
     24. | 24jan1960     Jan    24   Jan 24 |
     25. | 25jan1960     Jan    25   Jan 25 |
     26. | 26jan1960     Jan    26   Jan 26 |
     27. | 27jan1960     Jan    27   Jan 27 |
     28. | 28jan1960     Jan    28   Jan 28 |
     29. | 29jan1960     Jan    29   Jan 29 |
     30. | 30jan1960     Jan    30   Jan 30 |
     31. | 31jan1960     Jan    31   Jan 31 |
     32. | 01feb1960     Feb     1    Feb 1 |
     33. | 02feb1960     Feb     2    Feb 2 |
     34. | 03feb1960     Feb     3    Feb 3 |
     35. | 04feb1960     Feb     4    Feb 4 |
     36. | 05feb1960     Feb     5    Feb 5 |
     37. | 06feb1960     Feb     6    Feb 6 |
     38. | 07feb1960     Feb     7    Feb 7 |
     39. | 08feb1960     Feb     8    Feb 8 |
     40. | 09feb1960     Feb     9    Feb 9 |
     41. | 10feb1960     Feb    10   Feb 10 |
     42. | 11feb1960     Feb    11   Feb 11 |
     43. | 12feb1960     Feb    12   Feb 12 |
     44. | 13feb1960     Feb    13   Feb 13 |
     45. | 14feb1960     Feb    14   Feb 14 |
     46. | 15feb1960     Feb    15   Feb 15 |
     47. | 16feb1960     Feb    16   Feb 16 |
     48. | 17feb1960     Feb    17   Feb 17 |
     49. | 18feb1960     Feb    18   Feb 18 |
     50. | 19feb1960     Feb    19   Feb 19 |
     51. | 20feb1960     Feb    20   Feb 20 |
     52. | 21feb1960     Feb    21   Feb 21 |
     53. | 22feb1960     Feb    22   Feb 22 |
     54. | 23feb1960     Feb    23   Feb 23 |
     55. | 24feb1960     Feb    24   Feb 24 |
     56. | 25feb1960     Feb    25   Feb 25 |
     57. | 26feb1960     Feb    26   Feb 26 |
     58. | 27feb1960     Feb    27   Feb 27 |
     59. | 28feb1960     Feb    28   Feb 28 |
     60. | 29feb1960     Feb    29   Feb 29 |
     61. | 01mar1960     Mar     1    Mar 1 |
     62. | 02mar1960     Mar     2    Mar 2 |
     63. | 03mar1960     Mar     3    Mar 3 |
     64. | 04mar1960     Mar     4    Mar 4 |
     65. | 05mar1960     Mar     5    Mar 5 |
     66. | 06mar1960     Mar     6    Mar 6 |
     67. | 07mar1960     Mar     7    Mar 7 |
     68. | 08mar1960     Mar     8    Mar 8 |
     69. | 09mar1960     Mar     9    Mar 9 |
     70. | 10mar1960     Mar    10   Mar 10 |
     71. | 11mar1960     Mar    11   Mar 11 |
     72. | 12mar1960     Mar    12   Mar 12 |
     73. | 13mar1960     Mar    13   Mar 13 |
     74. | 14mar1960     Mar    14   Mar 14 |
     75. | 15mar1960     Mar    15   Mar 15 |
     76. | 16mar1960     Mar    16   Mar 16 |
     77. | 17mar1960     Mar    17   Mar 17 |
     78. | 18mar1960     Mar    18   Mar 18 |
     79. | 19mar1960     Mar    19   Mar 19 |
     80. | 20mar1960     Mar    20   Mar 20 |
     81. | 21mar1960     Mar    21   Mar 21 |
     82. | 22mar1960     Mar    22   Mar 22 |
     83. | 23mar1960     Mar    23   Mar 23 |
     84. | 24mar1960     Mar    24   Mar 24 |
     85. | 25mar1960     Mar    25   Mar 25 |
     86. | 26mar1960     Mar    26   Mar 26 |
     87. | 27mar1960     Mar    27   Mar 27 |
     88. | 28mar1960     Mar    28   Mar 28 |
     89. | 29mar1960     Mar    29   Mar 29 |
     90. | 30mar1960     Mar    30   Mar 30 |
     91. | 31mar1960     Mar    31   Mar 31 |
     92. | 01apr1960     Apr     1    Apr 1 |
     93. | 02apr1960     Apr     2    Apr 2 |
     94. | 03apr1960     Apr     3    Apr 3 |
     95. | 04apr1960     Apr     4    Apr 4 |
     96. | 05apr1960     Apr     5    Apr 5 |
     97. | 06apr1960     Apr     6    Apr 6 |
     98. | 07apr1960     Apr     7    Apr 7 |
     99. | 08apr1960     Apr     8    Apr 8 |
    100. | 09apr1960     Apr     9    Apr 9 |
    101. | 10apr1960     Apr    10   Apr 10 |
    102. | 11apr1960     Apr    11   Apr 11 |
    103. | 12apr1960     Apr    12   Apr 12 |
    104. | 13apr1960     Apr    13   Apr 13 |
    105. | 14apr1960     Apr    14   Apr 14 |
    106. | 15apr1960     Apr    15   Apr 15 |
    107. | 16apr1960     Apr    16   Apr 16 |
    108. | 17apr1960     Apr    17   Apr 17 |
    109. | 18apr1960     Apr    18   Apr 18 |
    110. | 19apr1960     Apr    19   Apr 19 |
    111. | 20apr1960     Apr    20   Apr 20 |
    112. | 21apr1960     Apr    21   Apr 21 |
    113. | 22apr1960     Apr    22   Apr 22 |
    114. | 23apr1960     Apr    23   Apr 23 |
    115. | 24apr1960     Apr    24   Apr 24 |
    116. | 25apr1960     Apr    25   Apr 25 |
    117. | 26apr1960     Apr    26   Apr 26 |
    118. | 27apr1960     Apr    27   Apr 27 |
    119. | 28apr1960     Apr    28   Apr 28 |
    120. | 29apr1960     Apr    29   Apr 29 |
    121. | 30apr1960     Apr    30   Apr 30 |
    122. | 01may1960     May     1    May 1 |
    123. | 02may1960     May     2    May 2 |
    124. | 03may1960     May     3    May 3 |
    125. | 04may1960     May     4    May 4 |
    126. | 05may1960     May     5    May 5 |
    127. | 06may1960     May     6    May 6 |
    128. | 07may1960     May     7    May 7 |
    129. | 08may1960     May     8    May 8 |
    130. | 09may1960     May     9    May 9 |
    131. | 10may1960     May    10   May 10 |
    132. | 11may1960     May    11   May 11 |
    133. | 12may1960     May    12   May 12 |
    134. | 13may1960     May    13   May 13 |
    135. | 14may1960     May    14   May 14 |
    136. | 15may1960     May    15   May 15 |
    137. | 16may1960     May    16   May 16 |
    138. | 17may1960     May    17   May 17 |
    139. | 18may1960     May    18   May 18 |
    140. | 19may1960     May    19   May 19 |
    141. | 20may1960     May    20   May 20 |
    142. | 21may1960     May    21   May 21 |
    143. | 22may1960     May    22   May 22 |
    144. | 23may1960     May    23   May 23 |
    145. | 24may1960     May    24   May 24 |
    146. | 25may1960     May    25   May 25 |
    147. | 26may1960     May    26   May 26 |
    148. | 27may1960     May    27   May 27 |
    149. | 28may1960     May    28   May 28 |
    150. | 29may1960     May    29   May 29 |
    151. | 30may1960     May    30   May 30 |
    152. | 31may1960     May    31   May 31 |
    153. | 01jun1960     Jun     1    Jun 1 |
    154. | 02jun1960     Jun     2    Jun 2 |
    155. | 03jun1960     Jun     3    Jun 3 |
    156. | 04jun1960     Jun     4    Jun 4 |
    157. | 05jun1960     Jun     5    Jun 5 |
    158. | 06jun1960     Jun     6    Jun 6 |
    159. | 07jun1960     Jun     7    Jun 7 |
    160. | 08jun1960     Jun     8    Jun 8 |
    161. | 09jun1960     Jun     9    Jun 9 |
    162. | 10jun1960     Jun    10   Jun 10 |
    163. | 11jun1960     Jun    11   Jun 11 |
    164. | 12jun1960     Jun    12   Jun 12 |
    165. | 13jun1960     Jun    13   Jun 13 |
    166. | 14jun1960     Jun    14   Jun 14 |
    167. | 15jun1960     Jun    15   Jun 15 |
    168. | 16jun1960     Jun    16   Jun 16 |
    169. | 17jun1960     Jun    17   Jun 17 |
    170. | 18jun1960     Jun    18   Jun 18 |
    171. | 19jun1960     Jun    19   Jun 19 |
    172. | 20jun1960     Jun    20   Jun 20 |
    173. | 21jun1960     Jun    21   Jun 21 |
    174. | 22jun1960     Jun    22   Jun 22 |
    175. | 23jun1960     Jun    23   Jun 23 |
    176. | 24jun1960     Jun    24   Jun 24 |
    177. | 25jun1960     Jun    25   Jun 25 |
    178. | 26jun1960     Jun    26   Jun 26 |
    179. | 27jun1960     Jun    27   Jun 27 |
    180. | 28jun1960     Jun    28   Jun 28 |
    181. | 29jun1960     Jun    29   Jun 29 |
    182. | 30jun1960     Jun    30   Jun 30 |
    183. | 01jul1960     Jul     1    Jul 1 |
    184. | 02jul1960     Jul     2    Jul 2 |
    185. | 03jul1960     Jul     3    Jul 3 |
    186. | 04jul1960     Jul     4    Jul 4 |
    187. | 05jul1960     Jul     5    Jul 5 |
    188. | 06jul1960     Jul     6    Jul 6 |
    189. | 07jul1960     Jul     7    Jul 7 |
    190. | 08jul1960     Jul     8    Jul 8 |
    191. | 09jul1960     Jul     9    Jul 9 |
    192. | 10jul1960     Jul    10   Jul 10 |
    193. | 11jul1960     Jul    11   Jul 11 |
    194. | 12jul1960     Jul    12   Jul 12 |
    195. | 13jul1960     Jul    13   Jul 13 |
    196. | 14jul1960     Jul    14   Jul 14 |
    197. | 15jul1960     Jul    15   Jul 15 |
    198. | 16jul1960     Jul    16   Jul 16 |
    199. | 17jul1960     Jul    17   Jul 17 |
    200. | 18jul1960     Jul    18   Jul 18 |
    201. | 19jul1960     Jul    19   Jul 19 |
    202. | 20jul1960     Jul    20   Jul 20 |
    203. | 21jul1960     Jul    21   Jul 21 |
    204. | 22jul1960     Jul    22   Jul 22 |
    205. | 23jul1960     Jul    23   Jul 23 |
    206. | 24jul1960     Jul    24   Jul 24 |
    207. | 25jul1960     Jul    25   Jul 25 |
    208. | 26jul1960     Jul    26   Jul 26 |
    209. | 27jul1960     Jul    27   Jul 27 |
    210. | 28jul1960     Jul    28   Jul 28 |
    211. | 29jul1960     Jul    29   Jul 29 |
    212. | 30jul1960     Jul    30   Jul 30 |
    213. | 31jul1960     Jul    31   Jul 31 |
    214. | 01aug1960     Aug     1    Aug 1 |
    215. | 02aug1960     Aug     2    Aug 2 |
    216. | 03aug1960     Aug     3    Aug 3 |
    217. | 04aug1960     Aug     4    Aug 4 |
    218. | 05aug1960     Aug     5    Aug 5 |
    219. | 06aug1960     Aug     6    Aug 6 |
    220. | 07aug1960     Aug     7    Aug 7 |
    221. | 08aug1960     Aug     8    Aug 8 |
    222. | 09aug1960     Aug     9    Aug 9 |
    223. | 10aug1960     Aug    10   Aug 10 |
    224. | 11aug1960     Aug    11   Aug 11 |
    225. | 12aug1960     Aug    12   Aug 12 |
    226. | 13aug1960     Aug    13   Aug 13 |
    227. | 14aug1960     Aug    14   Aug 14 |
    228. | 15aug1960     Aug    15   Aug 15 |
    229. | 16aug1960     Aug    16   Aug 16 |
    230. | 17aug1960     Aug    17   Aug 17 |
    231. | 18aug1960     Aug    18   Aug 18 |
    232. | 19aug1960     Aug    19   Aug 19 |
    233. | 20aug1960     Aug    20   Aug 20 |
    234. | 21aug1960     Aug    21   Aug 21 |
    235. | 22aug1960     Aug    22   Aug 22 |
    236. | 23aug1960     Aug    23   Aug 23 |
    237. | 24aug1960     Aug    24   Aug 24 |
    238. | 25aug1960     Aug    25   Aug 25 |
    239. | 26aug1960     Aug    26   Aug 26 |
    240. | 27aug1960     Aug    27   Aug 27 |
    241. | 28aug1960     Aug    28   Aug 28 |
    242. | 29aug1960     Aug    29   Aug 29 |
    243. | 30aug1960     Aug    30   Aug 30 |
    244. | 31aug1960     Aug    31   Aug 31 |
    245. | 01sep1960     Sep     1    Sep 1 |
    246. | 02sep1960     Sep     2    Sep 2 |
    247. | 03sep1960     Sep     3    Sep 3 |
    248. | 04sep1960     Sep     4    Sep 4 |
    249. | 05sep1960     Sep     5    Sep 5 |
    250. | 06sep1960     Sep     6    Sep 6 |
    251. | 07sep1960     Sep     7    Sep 7 |
    252. | 08sep1960     Sep     8    Sep 8 |
    253. | 09sep1960     Sep     9    Sep 9 |
    254. | 10sep1960     Sep    10   Sep 10 |
    255. | 11sep1960     Sep    11   Sep 11 |
    256. | 12sep1960     Sep    12   Sep 12 |
    257. | 13sep1960     Sep    13   Sep 13 |
    258. | 14sep1960     Sep    14   Sep 14 |
    259. | 15sep1960     Sep    15   Sep 15 |
    260. | 16sep1960     Sep    16   Sep 16 |
    261. | 17sep1960     Sep    17   Sep 17 |
    262. | 18sep1960     Sep    18   Sep 18 |
    263. | 19sep1960     Sep    19   Sep 19 |
    264. | 20sep1960     Sep    20   Sep 20 |
    265. | 21sep1960     Sep    21   Sep 21 |
    266. | 22sep1960     Sep    22   Sep 22 |
    267. | 23sep1960     Sep    23   Sep 23 |
    268. | 24sep1960     Sep    24   Sep 24 |
    269. | 25sep1960     Sep    25   Sep 25 |
    270. | 26sep1960     Sep    26   Sep 26 |
    271. | 27sep1960     Sep    27   Sep 27 |
    272. | 28sep1960     Sep    28   Sep 28 |
    273. | 29sep1960     Sep    29   Sep 29 |
    274. | 30sep1960     Sep    30   Sep 30 |
    275. | 01oct1960     Oct     1    Oct 1 |
    276. | 02oct1960     Oct     2    Oct 2 |
    277. | 03oct1960     Oct     3    Oct 3 |
    278. | 04oct1960     Oct     4    Oct 4 |
    279. | 05oct1960     Oct     5    Oct 5 |
    280. | 06oct1960     Oct     6    Oct 6 |
    281. | 07oct1960     Oct     7    Oct 7 |
    282. | 08oct1960     Oct     8    Oct 8 |
    283. | 09oct1960     Oct     9    Oct 9 |
    284. | 10oct1960     Oct    10   Oct 10 |
    285. | 11oct1960     Oct    11   Oct 11 |
    286. | 12oct1960     Oct    12   Oct 12 |
    287. | 13oct1960     Oct    13   Oct 13 |
    288. | 14oct1960     Oct    14   Oct 14 |
    289. | 15oct1960     Oct    15   Oct 15 |
    290. | 16oct1960     Oct    16   Oct 16 |
    291. | 17oct1960     Oct    17   Oct 17 |
    292. | 18oct1960     Oct    18   Oct 18 |
    293. | 19oct1960     Oct    19   Oct 19 |
    294. | 20oct1960     Oct    20   Oct 20 |
    295. | 21oct1960     Oct    21   Oct 21 |
    296. | 22oct1960     Oct    22   Oct 22 |
    297. | 23oct1960     Oct    23   Oct 23 |
    298. | 24oct1960     Oct    24   Oct 24 |
    299. | 25oct1960     Oct    25   Oct 25 |
    300. | 26oct1960     Oct    26   Oct 26 |
    301. | 27oct1960     Oct    27   Oct 27 |
    302. | 28oct1960     Oct    28   Oct 28 |
    303. | 29oct1960     Oct    29   Oct 29 |
    304. | 30oct1960     Oct    30   Oct 30 |
    305. | 31oct1960     Oct    31   Oct 31 |
    306. | 01nov1960     Nov     1    Nov 1 |
    307. | 02nov1960     Nov     2    Nov 2 |
    308. | 03nov1960     Nov     3    Nov 3 |
    309. | 04nov1960     Nov     4    Nov 4 |
    310. | 05nov1960     Nov     5    Nov 5 |
    311. | 06nov1960     Nov     6    Nov 6 |
    312. | 07nov1960     Nov     7    Nov 7 |
    313. | 08nov1960     Nov     8    Nov 8 |
    314. | 09nov1960     Nov     9    Nov 9 |
    315. | 10nov1960     Nov    10   Nov 10 |
    316. | 11nov1960     Nov    11   Nov 11 |
    317. | 12nov1960     Nov    12   Nov 12 |
    318. | 13nov1960     Nov    13   Nov 13 |
    319. | 14nov1960     Nov    14   Nov 14 |
    320. | 15nov1960     Nov    15   Nov 15 |
    321. | 16nov1960     Nov    16   Nov 16 |
    322. | 17nov1960     Nov    17   Nov 17 |
    323. | 18nov1960     Nov    18   Nov 18 |
    324. | 19nov1960     Nov    19   Nov 19 |
    325. | 20nov1960     Nov    20   Nov 20 |
    326. | 21nov1960     Nov    21   Nov 21 |
    327. | 22nov1960     Nov    22   Nov 22 |
    328. | 23nov1960     Nov    23   Nov 23 |
    329. | 24nov1960     Nov    24   Nov 24 |
    330. | 25nov1960     Nov    25   Nov 25 |
    331. | 26nov1960     Nov    26   Nov 26 |
    332. | 27nov1960     Nov    27   Nov 27 |
    333. | 28nov1960     Nov    28   Nov 28 |
    334. | 29nov1960     Nov    29   Nov 29 |
    335. | 30nov1960     Nov    30   Nov 30 |
    336. | 01dec1960     Dec     1    Dec 1 |
    337. | 02dec1960     Dec     2    Dec 2 |
    338. | 03dec1960     Dec     3    Dec 3 |
    339. | 04dec1960     Dec     4    Dec 4 |
    340. | 05dec1960     Dec     5    Dec 5 |
    341. | 06dec1960     Dec     6    Dec 6 |
    342. | 07dec1960     Dec     7    Dec 7 |
    343. | 08dec1960     Dec     8    Dec 8 |
    344. | 09dec1960     Dec     9    Dec 9 |
    345. | 10dec1960     Dec    10   Dec 10 |
    346. | 11dec1960     Dec    11   Dec 11 |
    347. | 12dec1960     Dec    12   Dec 12 |
    348. | 13dec1960     Dec    13   Dec 13 |
    349. | 14dec1960     Dec    14   Dec 14 |
    350. | 15dec1960     Dec    15   Dec 15 |
    351. | 16dec1960     Dec    16   Dec 16 |
    352. | 17dec1960     Dec    17   Dec 17 |
    353. | 18dec1960     Dec    18   Dec 18 |
    354. | 19dec1960     Dec    19   Dec 19 |
    355. | 20dec1960     Dec    20   Dec 20 |
    356. | 21dec1960     Dec    21   Dec 21 |
    357. | 22dec1960     Dec    22   Dec 22 |
    358. | 23dec1960     Dec    23   Dec 23 |
    359. | 24dec1960     Dec    24   Dec 24 |
    360. | 25dec1960     Dec    25   Dec 25 |
    361. | 26dec1960     Dec    26   Dec 26 |
    362. | 27dec1960     Dec    27   Dec 27 |
    363. | 28dec1960     Dec    28   Dec 28 |
    364. | 29dec1960     Dec    29   Dec 29 |
    365. | 30dec1960     Dec    30   Dec 30 |
    366. | 31dec1960     Dec    31   Dec 31 |
         +----------------------------------+
    Last edited by Andrew Musau; 06 Jun 2022, 06:46.

    Comment


    • #3
      Thinking some more about this problem, there is no 1:1 correspondence between days after February 28 and -doy()- (day of the year) values due to leap years. Consider the 90's decade:

      Code:
      forval i= 0/9{
          di "1st March 199`i' was the `=doy(td(1mar199`i'))'(th/st) day of the year"
      }
      Res.:

      Code:
      . forval i= 0/9{
        2.
      .     di "1st March 199`i' was the `=doy(td(1mar199`i'))'(th/st) day of the year"
        3.
      . }
      1st March 1990 was the 60(th/st) day of the year
      1st March 1991 was the 60(th/st) day of the year
      1st March 1992 was the 61(th/st) day of the year
      1st March 1993 was the 60(th/st) day of the year
      1st March 1994 was the 60(th/st) day of the year
      1st March 1995 was the 60(th/st) day of the year
      1st March 1996 was the 61(th/st) day of the year
      1st March 1997 was the 60(th/st) day of the year
      1st March 1998 was the 60(th/st) day of the year
      1st March 1999 was the 60(th/st) day of the year

      1st of March is the 61st day of the year during leap years, otherwise 60th. Therefore, you have to go with numerical values as value labels make no sense.
      Last edited by Andrew Musau; 06 Jun 2022, 09:38.

      Comment


      • #4
        I think you need to explain some missing details of the question. What is the purpose of having only month and day, but not year? Said another way, why is it ok to ignore the year with your data?

        Andrew has already offered a solution but identified a natural pitfall of leap-days.

        If you only care about how the date is displayed, then a simple datetime format can "solve" this, and still retain all the detail of the full calendar date. However, if you need to perform calculations on those dates, then you must retain the year information to account for issues such as leap-days. The below code offers something along these lines to reflect on.

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input byte id int(start end)
        1  8217  8401
        1  8596  8705
        1  8932  9071
        1  9177  9436
        1  9678  9831
        1 10027 10180
        1 10409 10532
        end
        format %td start
        format %td end
        
        clonevar start2 = start
        clonevar end2 = end
        format start2 end2 %tdDDmon
        
        gen str_start2 = string(start2, "%tdDDmon")
        gen str_end2 = string(end2, "%tdDDmon")
        list, sep(0) abbrev(12)
        Result:

        Code:
        . list, sep(0) abbrev(12)
        
             +---------------------------------------------------------------------+
             | id       start         end   start2    end2   str_start2   str_end2 |
             |---------------------------------------------------------------------|
          1. |  1   01jul1982   01jan1983    01jul   01jan        01jul      01jan |
          2. |  1   15jul1983   01nov1983    15jul   01nov        15jul      01nov |
          3. |  1   15jun1984   01nov1984    15jun   01nov        15jun      01nov |
          4. |  1   15feb1985   01nov1985    15feb   01nov        15feb      01nov |
          5. |  1   01jul1986   01dec1986    01jul   01dec        01jul      01dec |
          6. |  1   15jun1987   15nov1987    15jun   15nov        15jun      15nov |
          7. |  1   01jul1988   01nov1988    01jul   01nov        01jul      01nov |
             +---------------------------------------------------------------------+

        Comment


        • #5
          Hi Andrew and Leonardo,
          Thank you very much for your help!

          Both your solutions work perfectly fine but don't really solve my problem. Sorry for not having made it clear what I want to achieve. I want to calculate for each id and time of the year (e.g., beginning of January) the average value of another variable (not displayed here to keep it simple). To this end, I thought I could create a variable that takes on the same value always when the time of the year is the same.

          In the meanwhile, I think I found a way to achieve this:

          Code:
          gen day = day(start)
          gen month = month(start)
          egen daymonth_start = group(day month)
          Repeating the same for the variable end and using daymonth_start and daymonth_end I can now calculate the average value of another variable with the following command:
          Code:
          bysort id daymonth_start: egen mean_var = mean(var)
          Do you think this is a correct way to do it?
          Last edited by Nora Fingado; 06 Jun 2022, 14:28.

          Comment


          • #6
            Ok this clarifies a bit more your intent, but it doesn’t help us to be vague about the details. What you have shown is not quite right, but let’s clarify further what you are trying to do.

            Are you trying to compute monthly averages of some variable? If so, why is the month end date relevant? Please report back with a clear description of what you want to do, in English, rather than code.

            Comment


            • #7
              Hi Leonardo,

              sorry, I mixed two things up that led to confusion. I'll try to clarify. Please ignore my previous post.

              The variables start and end depict the start and end of a vegetation growing season at location x (id) over time, respectively. These start and end dates slightly differ between the years and I would like to find out at what time of year the growing season in a given location typically begins and ends. Do you have any ideas how to do it? As an approximation, I so far just look at the month when the growing season normally begins and ends. The code I used for this is:
              Code:
              gen start_month = month(start)
              gen end_month = month(end)
              collapse (mean) start_month end_month, by(id)
              recast byte start_month end_month, force
              The second thing I want to do is actually unrelated to the first point, but I mixed the two things up in my previous post. So please ignore my additional note that I want to calculate the average value of another variable, this comes at a later stage of my analysis and is not relevant here. Sorry again for the confusion.

              Best,
              Nora

              Comment


              • #8
                Hi Nora, thanks for the added explanation. When defining growing season, what kind of time resolution is required? Not being in agriculture, I will hazard that either growth months or weeks are probably reasonable. You can use the below as a starting point.

                Code:
                clear
                input byte id int(start end)
                1  8217  8401
                1  8596  8705
                1  8932  9071
                1  9177  9436
                1  9678  9831
                1 10027 10180
                1 10409 10532
                end
                format %td start
                format %td end
                
                * identify monthly date
                gen mon_start = mofd(start)
                gen mon_end = mofd(end)
                format mon_start mon_end %tm
                
                // number of calendar months involved in growing season (e.g., May 31 to June 1 counts as 2 months)
                gen months1 = mon_end - mon_start
                
                // alternative: datediff() gives number of units of time (months, here) by comparing day and month.
                // See Methods and Formulas section of PDF documentation following -help datetime functions-
                gen months2 = datediff(start, end, "month")
                gen months2_frac = datediff_frac(start, end, "month")
                
                * identify weekly date
                gen week_start = wofd(start)
                gen week_end = wofd(end)
                format week_start week_end %tw
                
                gen weeks = week_end - week_start

                Comment


                • #9
                  I disagree mildly with Leonardo Guizzetti and go back to the suggestion of Andrew Musau in #2.

                  I've a fair amount of experience with climatic variables whether as response or control and frequently emphasise to students that the climate knows the time of year, but not the human calendar. The frequent presentation of climate statistics by months owes more to human convention and convenience than to real or physical climatology.

                  I tend to work with day of year and often fraction of year defined as

                  (day of year - 0.5) / (365 or 366)

                  where code for the denominator can be

                  Code:
                  365 + !missing(mdy(2, 29, year))
                  as Stata knows whether it's a leap year in which a call for the daily date for 29 February will return a non-missing value, That may seem over-refined.but it is faithful to the data as they arrive.

                  Addressing Leonardo's suggestion, months can often be too ooarse and weeks suffer from ambiguities and awkward details in their definition.

                  Dealing directly with fraction of year keeps as much detail as possible. For averages I would often use trigonometric functions, which wrap around correctly at year end.

                  See also

                  https://www.stata-journal.com/articl...article=st0116

                  https://www.stata-journal.com/articl...article=st0394

                  The second paper emphasises that you're in charge over what makes sense as a natural year. For snow in Scotland or sun in Sydney, start the year in July. or August.

                  Comment


                  • #10
                    Dear Nick, dear Leonardo,

                    thank you very much for your help!
                    Nick Cox, that may be a silly question, but may I ask why you subtract 0.5 from the day of year?

                    Apart from that, you all helped me a lot, thank you!

                    Comment


                    • #11
                      Consider days 1 to 365 in a non-leap year for simplicity. Then day of year / 365 runs 1/365 to 365/365 and does not treat year ends symmetrically. 1/730 to 729/730 as a different rule is symmetric. The point may be trivial in practice but being fussy about it has no essential cost or other downside. The same points arises with leap years.

                      Although there are other issues, this is the main argument also for plotting positions on quantile plots that have the form (rank - a) / (number in sample - 2a + 1) where a is often 1/2.

                      Comment


                      • #12
                        Okay, thank you!

                        Comment

                        Working...
                        X