MATLAB: Sum up daily values to monthly values in a table of different company identifiers

convert dateMATLABmonthsum up values each month

Hello all,
I have a table of unique company identifiers (column 1, 'PERMCO') and their corresponding day (column 2,'DATE') and the values for the specific days I want to sum for each month (column 3,'VALUE').
It looks like this and has the size 18828200×3 with around 2000 different company identifiers (PERMCO):
PERMCO DATE VALUE
2584 20030214 4
2584 20030215 2
2584 20030226 2
2584 20030227 1
2584 20030228 1
2584 20030303 2
2584 20030305 5
2584 20030330 4
etc.
I want to sum up VALUE for each month, keep the last day in the month and store it all in a new table/matrix so that it looks like this:
2584 20030228 10
2584 20030330 11
etc.
I couldn't find a solution that works for this purpose. Thanks a lot for your help!

Best Answer

It's trivial to do any number of ways. But first, your date must be stored as a proper datetime. If it's stored as you show:
yourtable.DATE = datetime(yourtable.DATE, 'ConvertFrom', 'yyyymmdd');
Once that's done, the easiest way:
result = groupsummary(yourtable, {'PERMCO', 'DATE'}, {'none', 'month'}, 'sum')
It can also be done with findgroups and splitapply:
[monthid, actualmonth] = discretize(yourtable.DATE, 'month');
[group, PERMCO, monthnum] = findgroups(yourtable.PERMCO, monthid);
sum_VALUE = splitapply(@sum, yourtable.VALUE, group);
MONTH = actualmonth(monthnum);
result = table(PERMCO, MONTH, sum_VALUE);
It could also be done using rowfun or varfun.