Announcement

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

  • Generating Monthly Variable from Daily Rainfall Data

    I have daily rainfall data from Indian Meteorological department from 2000 to 2015. The data reports the day of the year (numbered from 1 to 365), latitude, longitude and year (2000 to 2020). The data does not report month of the year. Is there a way to use the day of the year information to generate a variable for month?
    Below is an example of the data


    Code:
    * Example generated by -dataex-. For    more info,    type    help    dataex
    clear
    input int day float(lat lon rainfall    Year)
    1  6.5  66.5 -999 2015
    1  6.5 66.75 -999 2015
    1  6.5    67 -999 2015
    1  6.5 67.25 -999 2015
    1  6.5  67.5 -999 2015
    1  6.5 67.75 -999 2015
    1  6.5    68 -999 2015
    1  6.5 68.25 -999 2015
    1  6.5  68.5 -999 2015
    1  6.5 68.75 -999 2015
    1  6.5    69 -999 2015
    1  6.5 69.25 -999 2015
    1  6.5  69.5 -999 2015
    1  6.5 69.75 -999 2015
    1  6.5    70 -999 2015
    1  6.5 70.25 -999 2015
    1  6.5  70.5 -999 2015
    1  6.5 70.75 -999 2015
    1  6.5    71 -999 2015
    1  6.5 71.25 -999 2015
    1  6.5  71.5 -999 2015
    1  6.5 71.75 -999 2015
    1  6.5    72 -999 2015
    1  6.5 72.25 -999 2015
    1  6.5  72.5 -999 2015
    1  6.5 72.75 -999 2015
    1  6.5    73 -999 2015
    1  6.5 73.25 -999 2015
    1  6.5  73.5 -999 2015
    1  6.5 73.75 -999 2015
    1  6.5    74 -999 2015
    1  6.5 74.25 -999 2015
    1  6.5  74.5 -999 2015
    1  6.5 74.75 -999 2015
    1  6.5    75 -999 2015
    1  6.5 75.25 -999 2015
    1  6.5  75.5 -999 2015
    1  6.5 75.75 -999 2015
    1  6.5    76 -999 2015
    1  6.5 76.25 -999 2015
    1  6.5  76.5 -999 2015
    1  6.5 76.75 -999 2015
    1  6.5    77 -999 2015
    1  6.5 77.25 -999 2015
    1  6.5  77.5 -999 2015
    1  6.5 77.75 -999 2015
    1  6.5    78 -999 2015
    1  6.5 78.25 -999 2015
    1  6.5  78.5 -999 2015
    1  6.5 78.75 -999 2015
    1  6.5    79 -999 2015
    1  6.5 79.25 -999 2015
    1  6.5  79.5 -999 2015
    1  6.5 79.75 -999 2015
    1  6.5    80 -999 2015
    1  6.5 80.25 -999 2015
    1  6.5  80.5 -999 2015
    1  6.5 80.75 -999 2015
    1  6.5    81 -999 2015
    1  6.5 81.25 -999 2015
    1  6.5  81.5 -999 2015
    1  6.5 81.75 -999 2015
    1  6.5    82 -999 2015
    1  6.5 82.25 -999 2015
    1  6.5  82.5 -999 2015
    1  6.5 82.75 -999 2015
    1  6.5    83 -999 2015
    1  6.5 83.25 -999 2015
    1  6.5  83.5 -999 2015
    1  6.5 83.75 -999 2015
    1  6.5    84 -999 2015
    1  6.5 84.25 -999 2015
    1  6.5  84.5 -999 2015
    1  6.5 84.75 -999 2015
    1  6.5    85 -999 2015
    1  6.5 85.25 -999 2015
    1  6.5  85.5 -999 2015
    1  6.5 85.75 -999 2015
    1  6.5    86 -999 2015
    1  6.5 86.25 -999 2015
    1  6.5  86.5 -999 2015
    1  6.5 86.75 -999 2015
    1  6.5    87 -999 2015
    1  6.5 87.25 -999 2015
    1  6.5  87.5 -999 2015
    1  6.5 87.75 -999 2015
    1  6.5    88 -999 2015
    1  6.5 88.25 -999 2015
    1  6.5  88.5 -999 2015
    1  6.5 88.75 -999 2015
    1  6.5    89 -999 2015
    1  6.5 89.25 -999 2015
    1  6.5  89.5 -999 2015
    1  6.5 89.75 -999 2015
    1  6.5    90 -999 2015
    1  6.5 90.25 -999 2015
    1  6.5  90.5 -999 2015
    1  6.5 90.75 -999 2015
    1  6.5    91 -999 2015
    1  6.5 91.25 -999 2015
    1  6.5  91.5 -999 2015
    1  6.5 91.75 -999 2015
    1  6.5    92 -999 2015
    1  6.5 92.25 -999 2015
    1  6.5  92.5 -999 2015
    1  6.5 92.75 -999 2015
    1  6.5    93 -999 2015
    1  6.5 93.25 -999 2015
    1  6.5  93.5 -999 2015
    1  6.5 93.75 -999 2015
    1  6.5    94 -999 2015
    1  6.5 94.25 -999 2015
    1  6.5  94.5 -999 2015
    1  6.5 94.75 -999 2015
    1  6.5    95 -999 2015
    1  6.5 95.25 -999 2015
    1  6.5  95.5 -999 2015
    1  6.5 95.75 -999 2015
    1  6.5    96 -999 2015
    1  6.5 96.25 -999 2015
    1  6.5  96.5 -999 2015
    1  6.5 96.75 -999 2015
    1  6.5    97 -999 2015
    1  6.5 97.25 -999 2015
    1  6.5  97.5 -999 2015
    1  6.5 97.75 -999 2015
    1  6.5    98 -999 2015
    1  6.5 98.25 -999 2015
    1  6.5  98.5 -999 2015
    1  6.5 98.75 -999 2015
    1  6.5    99 -999 2015
    1  6.5 99.25 -999 2015
    1  6.5  99.5 -999 2015
    1  6.5 99.75 -999 2015
    1  6.5   100 -999 2015
    1 6.75  66.5 -999 2015
    1 6.75 66.75 -999 2015
    1 6.75    67 -999 2015
    1 6.75 67.25 -999 2015
    1 6.75  67.5 -999 2015
    1 6.75 67.75 -999 2015
    1 6.75    68 -999 2015
    1 6.75 68.25 -999 2015
    1 6.75  68.5 -999 2015
    1 6.75 68.75 -999 2015
    1 6.75    69 -999 2015
    1 6.75 69.25 -999 2015
    1 6.75  69.5 -999 2015
    1 6.75 69.75 -999 2015
    1 6.75    70 -999 2015
    1 6.75 70.25 -999 2015
    1 6.75  70.5 -999 2015
    1 6.75 70.75 -999 2015
    1 6.75    71 -999 2015
    1 6.75 71.25 -999 2015
    1 6.75  71.5 -999 2015
    1 6.75 71.75 -999 2015
    1 6.75    72 -999 2015
    1 6.75 72.25 -999 2015
    1 6.75  72.5 -999 2015
    1 6.75 72.75 -999 2015
    1 6.75    73 -999 2015
    1 6.75 73.25 -999 2015
    1 6.75  73.5 -999 2015
    1 6.75 73.75 -999 2015
    1 6.75    74 -999 2015
    1 6.75 74.25 -999 2015
    1 6.75  74.5 -999 2015
    1 6.75 74.75 -999 2015
    1 6.75    75 -999 2015
    1 6.75 75.25 -999 2015
    1 6.75  75.5 -999 2015
    1 6.75 75.75 -999 2015
    1 6.75    76 -999 2015
    1 6.75 76.25 -999 2015
    1 6.75  76.5 -999 2015
    1 6.75 76.75 -999 2015
    1 6.75    77 -999 2015
    1 6.75 77.25 -999 2015
    1 6.75  77.5 -999 2015
    1 6.75 77.75 -999 2015
    1 6.75    78 -999 2015
    1 6.75 78.25 -999 2015
    1 6.75  78.5 -999 2015
    1 6.75 78.75 -999 2015
    1 6.75    79 -999 2015
    1 6.75 79.25 -999 2015
    1 6.75  79.5 -999 2015
    1 6.75 79.75 -999 2015
    1 6.75    80 -999 2015
    1 6.75 80.25 -999 2015
    1 6.75  80.5 -999 2015
    1 6.75 80.75 -999 2015
    1 6.75    81 -999 2015
    1 6.75 81.25 -999 2015
    1 6.75  81.5 -999 2015
    1 6.75 81.75 -999 2015
    1 6.75    82 -999 2015
    1 6.75 82.25 -999 2015
    1 6.75  82.5 -999 2015
    1 6.75 82.75 -999 2015
    1 6.75    83 -999 2015
    1 6.75 83.25 -999 2015
    1 6.75  83.5 -999 2015
    1 6.75 83.75 -999 2015
    1 6.75    84 -999 2015
    1 6.75 84.25 -999 2015
    1 6.75  84.5 -999 2015
    1 6.75 84.75 -999 2015
    1 6.75    85 -999 2015
    1 6.75 85.25 -999 2015
    1 6.75  85.5 -999 2015
    1 6.75 85.75 -999 2015
    1 6.75    86 -999 2015
    1 6.75 86.25 -999 2015
    1 6.75  86.5 -999 2015
    1 6.75 86.75 -999 2015
    1 6.75    87 -999 2015
    1 6.75 87.25 -999 2015
    1 6.75  87.5 -999 2015
    1 6.75 87.75 -999 2015
    1 6.75    88 -999 2015
    1 6.75 88.25 -999 2015
    1 6.75  88.5 -999 2015
    1 6.75 88.75 -999 2015
    1 6.75    89 -999 2015
    1 6.75 89.25 -999 2015
    1 6.75  89.5 -999 2015
    1 6.75 89.75 -999 2015
    1 6.75    90 -999 2015
    1 6.75 90.25 -999 2015
    1 6.75  90.5 -999 2015
    1 6.75 90.75 -999 2015
    1 6.75    91 -999 2015
    1 6.75 91.25 -999 2015
    1 6.75  91.5 -999 2015
    1 6.75 91.75 -999 2015
    1 6.75    92 -999 2015
    1 6.75 92.25 -999 2015
    1 6.75  92.5 -999 2015
    1 6.75 92.75 -999 2015
    1 6.75    93 -999 2015
    1 6.75 93.25 -999 2015
    1 6.75  93.5 -999 2015
    1 6.75 93.75 -999 2015
    1 6.75    94 -999 2015
    1 6.75 94.25 -999 2015
    1 6.75  94.5 -999 2015
    1 6.75 94.75 -999 2015
    1 6.75    95 -999 2015
    1 6.75 95.25 -999 2015
    1 6.75  95.5 -999 2015
    1 6.75 95.75 -999 2015
    1 6.75    96 -999 2015
    1 6.75 96.25 -999 2015
    1 6.75  96.5 -999 2015
    1 6.75 96.75 -999 2015
    1 6.75    97 -999 2015
    1 6.75 97.25 -999 2015
    1 6.75  97.5 -999 2015
    1 6.75 97.75 -999 2015
    1 6.75    98 -999 2015
    1 6.75 98.25 -999 2015
    1 6.75  98.5 -999 2015
    1 6.75 98.75 -999 2015
    1 6.75    99 -999 2015
    1 6.75 99.25 -999 2015
    1 6.75  99.5 -999 2015
    1 6.75 99.75 -999 2015
    1 6.75   100 -999 2015
    1    7  66.5 -999 2015
    1    7 66.75 -999 2015
    1    7    67 -999 2015
    1    7 67.25 -999 2015
    1    7  67.5 -999 2015
    1    7 67.75 -999 2015
    1    7    68 -999 2015
    1    7 68.25 -999 2015
    1    7  68.5 -999 2015
    1    7 68.75 -999 2015
    1    7    69 -999 2015
    1    7 69.25 -999 2015
    1    7  69.5 -999 2015
    1    7 69.75 -999 2015
    1    7    70 -999 2015
    1    7 70.25 -999 2015
    1    7  70.5 -999 2015
    1    7 70.75 -999 2015
    1    7    71 -999 2015
    1    7 71.25 -999 2015
    1    7  71.5 -999 2015
    1    7 71.75 -999 2015
    1    7    72 -999 2015
    1    7 72.25 -999 2015
    1    7  72.5 -999 2015
    1    7 72.75 -999 2015
    1    7    73 -999 2015
    1    7 73.25 -999 2015
    1    7  73.5 -999 2015
    1    7 73.75 -999 2015
    1    7    74 -999 2015
    1    7 74.25 -999 2015
    1    7  74.5 -999 2015
    1    7 74.75 -999 2015
    1    7    75 -999 2015
    1    7 75.25 -999 2015
    1    7  75.5 -999 2015
    1    7 75.75 -999 2015
    1    7    76 -999 2015
    1    7 76.25 -999 2015
    1    7  76.5 -999 2015
    1    7 76.75 -999 2015
    1    7    77 -999 2015
    1    7 77.25 -999 2015
    1    7  77.5 -999 2015
    1    7 77.75 -999 2015
    1    7    78 -999 2015
    1    7 78.25 -999 2015
    1    7  78.5 -999 2015
    1    7 78.75 -999 2015
    1    7    79 -999 2015
    1    7 79.25 -999 2015
    1    7  79.5 -999 2015
    1    7 79.75 -999 2015
    1    7    80 -999 2015
    1    7 80.25 -999 2015
    1    7  80.5 -999 2015
    1    7 80.75 -999 2015
    1    7    81 -999 2015
    1    7 81.25 -999 2015
    1    7  81.5 -999 2015
    1    7 81.75 -999 2015
    1    7    82 -999 2015
    1    7 82.25 -999 2015
    1    7  82.5 -999 2015
    1    7 82.75 -999 2015
    1    7    83 -999 2015
    1    7 83.25 -999 2015
    1    7  83.5 -999 2015
    1    7 83.75 -999 2015
    1    7    84 -999 2015
    1    7 84.25 -999 2015
    1    7  84.5 -999 2015
    1    7 84.75 -999 2015
    1    7    85 -999 2015
    1    7 85.25 -999 2015
    1    7  85.5 -999 2015
    1    7 85.75 -999 2015
    1    7    86 -999 2015
    1    7 86.25 -999 2015
    1    7  86.5 -999 2015
    1    7 86.75 -999 2015
    1    7    87 -999 2015
    1    7 87.25 -999 2015
    1    7  87.5 -999 2015
    1    7 87.75 -999 2015
    1    7    88 -999 2015
    1    7 88.25 -999 2015
    1    7  88.5 -999 2015
    1    7 88.75 -999 2015
    1    7    89 -999 2015
    1    7 89.25 -999 2015
    1    7  89.5 -999 2015
    1    7 89.75 -999 2015
    1    7    90 -999 2015
    1    7 90.25 -999 2015
    1    7  90.5 -999 2015
    1    7 90.75 -999 2015
    1    7    91 -999 2015
    1    7 91.25 -999 2015
    1    7  91.5 -999 2015
    1    7 91.75 -999 2015
    1    7    92 -999 2015
    1    7 92.25 -999 2015
    1    7  92.5 -999 2015
    1    7 92.75 -999 2015
    1    7    93 -999 2015
    1    7 93.25 -999 2015
    1    7  93.5 -999 2015
    1    7 93.75 -999 2015
    1    7    94 -999 2015
    1    7 94.25 -999 2015
    1    7  94.5 -999 2015
    1    7 94.75 -999 2015
    1    7    95 -999 2015
    1    7 95.25 -999 2015
    1    7  95.5 -999 2015
    1    7 95.75 -999 2015
    1    7    96 -999 2015
    1    7 96.25 -999 2015
    1    7  96.5 -999 2015
    1    7 96.75 -999 2015
    1    7    97 -999 2015
    1    7 97.25 -999 2015
    1    7  97.5 -999 2015
    1    7 97.75 -999 2015
    1    7    98 -999 2015
    1    7 98.25 -999 2015
    1    7  98.5 -999 2015
    1    7 98.75 -999 2015
    1    7    99 -999 2015
    1    7 99.25 -999 2015
    1    7  99.5 -999 2015
    1    7 99.75 -999 2015
    1    7   100 -999 2015
    1 7.25  66.5 -999 2015
    1 7.25 66.75 -999 2015
    1 7.25    67 -999 2015
    1 7.25 67.25 -999 2015
    1 7.25  67.5 -999 2015
    1 7.25 67.75 -999 2015
    1 7.25    68 -999 2015
    1 7.25 68.25 -999 2015
    1 7.25  68.5 -999 2015
    1 7.25 68.75 -999 2015
    1 7.25    69 -999 2015
    1 7.25 69.25 -999 2015
    1 7.25  69.5 -999 2015
    1 7.25 69.75 -999 2015
    1 7.25    70 -999 2015
    1 7.25 70.25 -999 2015
    1 7.25  70.5 -999 2015
    1 7.25 70.75 -999 2015
    1 7.25    71 -999 2015
    1 7.25 71.25 -999 2015
    1 7.25  71.5 -999 2015
    1 7.25 71.75 -999 2015
    1 7.25    72 -999 2015
    1 7.25 72.25 -999 2015
    1 7.25  72.5 -999 2015
    1 7.25 72.75 -999 2015
    1 7.25    73 -999 2015
    1 7.25 73.25 -999 2015
    1 7.25  73.5 -999 2015
    1 7.25 73.75 -999 2015
    1 7.25    74 -999 2015
    1 7.25 74.25 -999 2015
    1 7.25  74.5 -999 2015
    1 7.25 74.75 -999 2015
    1 7.25    75 -999 2015
    1 7.25 75.25 -999 2015
    1 7.25  75.5 -999 2015
    1 7.25 75.75 -999 2015
    1 7.25    76 -999 2015
    1 7.25 76.25 -999 2015
    1 7.25  76.5 -999 2015
    1 7.25 76.75 -999 2015
    1 7.25    77 -999 2015
    1 7.25 77.25 -999 2015
    1 7.25  77.5 -999 2015
    1 7.25 77.75 -999 2015
    1 7.25    78 -999 2015
    1 7.25 78.25 -999 2015
    1 7.25  78.5 -999 2015
    1 7.25 78.75 -999 2015
    1 7.25    79 -999 2015
    1 7.25 79.25 -999 2015
    1 7.25  79.5 -999 2015
    1 7.25 79.75 -999 2015
    1 7.25    80 -999 2015
    1 7.25 80.25 -999 2015
    1 7.25  80.5 -999 2015
    1 7.25 80.75 -999 2015
    1 7.25    81 -999 2015
    1 7.25 81.25 -999 2015
    1 7.25  81.5 -999 2015
    1 7.25 81.75 -999 2015
    1 7.25    82 -999 2015
    1 7.25 82.25 -999 2015
    1 7.25  82.5 -999 2015
    1 7.25 82.75 -999 2015
    1 7.25    83 -999 2015
    1 7.25 83.25 -999 2015
    1 7.25  83.5 -999 2015
    1 7.25 83.75 -999 2015
    1 7.25    84 -999 2015
    1 7.25 84.25 -999 2015
    1 7.25  84.5 -999 2015
    1 7.25 84.75 -999 2015
    1 7.25    85 -999 2015
    1 7.25 85.25 -999 2015
    1 7.25  85.5 -999 2015
    1 7.25 85.75 -999 2015
    1 7.25    86 -999 2015
    1 7.25 86.25 -999 2015
    1 7.25  86.5 -999 2015
    1 7.25 86.75 -999 2015
    1 7.25    87 -999 2015
    1 7.25 87.25 -999 2015
    1 7.25  87.5 -999 2015
    1 7.25 87.75 -999 2015
    1 7.25    88 -999 2015
    1 7.25 88.25 -999 2015
    1 7.25  88.5 -999 2015
    1 7.25 88.75 -999 2015
    1 7.25    89 -999 2015
    1 7.25 89.25 -999 2015
    1 7.25  89.5 -999 2015
    1 7.25 89.75 -999 2015
    1 7.25    90 -999 2015
    end

  • #2
    Code:
    gen ddate = mdy(12, 31, Year-1) + day
    format ddate %td
    gen mdate = mofd(ddate)
    format mdate %tm
    gen month = month(ddate)
    When you say you want month of the year, that I take to mean the variable created above called month. But I think that for most purposes, the variable mdate, which reflects both the month and the year in one variable is more useful, so consider using that (or both of them).

    Your example data has only one date instantiated, despite its length, so the code is not thoroughly tested, but I believe it is correct in generality.

    As an aside, I notice your rainfall variable takes on the value -999. Since I don't expect that 999 units (whatever those units might be) of water emerge from the ground as droplets and levitate back to the clouds on any day in any place, I assume that -999 represents either the absence of rain, or the absence of information about the rainfall. It is a very bad practice to encode things that way in Stata and it has a very high probability of leading you to seriously distorted calculations later on. If -999 represents the absence of rain, then the amount of rainfall is actually 0 and should be coded that way. If -999 means that the amount of rainfall is unknown, then it should be coded as a Stata missing value. See -help missing-.

    Comment


    • #3
      Clyde Schechter Thank you for the code. It generates the month variable.
      Thank you for pointing this out '-999' are the absence of rainfall coded by IMD.
      I have changed it to 0
      Code:
      (replace rainfall=. if rainfall<0)

      Comment


      • #4
        I've worked with rainfall data. I would change -999 to missing . and also count the number of non-missing values each month and year for each station. That is a handle on data quality.

        I would be surprised if you didn't have explicit 0 values too.

        Comment


        • #5
          Nick Cox Thank you for these suggestions!
          There are explicit 0 values in the data. I have counted the observations for each station as well using
          Code:
           bys _CY _CX: egen count=count(rainfall)

          Comment

          Working...
          X