Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a list that includes dates. I need to convert each date to be the last day of the quarter that it's in. So for example I want {2013,7,1,0,0,0} to become {2013,9,30,0,0,0}. Actually I don't need the zero's. I was trying to do this with a rule, but ran into problems. I wanted a rule like: 7,_ -> 9,30. Further I was going to try to insert alternatives into this with something like 7,_ |8,_ |9,_ -> 9,30.

Is there a way to write a rule to handle this? Or, for this specific problem is there a better way to convert these dates to quarter end dates?

An example of a row of data is: {{2009,5,1,0,0,0},"TYAU145A", 92468., 0.5, 46234.}.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

Creating function endq :-

endq = DateList[{#1, 1 + 3 Ceiling[#2/3], 0}] &;

endq @@ {2013, 1, 1, 0, 0, 0}

{2013, 3, 31, 0, 0, 0.}

For example, with data :-

a = {{{2009, 5, 1, 0, 0, 0}, "TYAU145A", 92468., 0.5, 46234.},
    {{2012, 8, 12, 0, 0, 0}, "TYAU145A", 92468., 0.5, 46234.},
    {{2001, 12, 9, 0, 0, 0}, "TYAU145A", 92468., 0.5, 46234.}};

{endq @@ #1, ##2} & @@@ a

{{{2009, 6, 30, 0, 0, 0.}, "TYAU145A", 92468., 0.5, 46234.}, {{2012, 9, 30, 0, 0, 0.}, "TYAU145A", 92468., 0.5, 46234.}, {{2001, 12, 31, 0, 0, 0.}, "TYAU145A", 92468., 0.5, 46234.}}

Alternatively, using a rule :-

qrule = {y_, m_, __} :> DateList[{y, 1 + 3 Ceiling[m/3], 0}];

{# /. qrule, ##2} & @@@ a
share|improve this answer
    
I'm still working on your "endq" solution. I don't understand how it works. I see how it gets the year and the month if I understand how the slots work. How does it get the last day of the month? Although I'm really not sure how any of it works. I assumed endq @@ replaces List with the endq function. I can understand how one head is replaced by another head, say Plus @@. I'm really not seeing what happens. Using FullForm or Hold didn't help me very much, probably because I'm not that facile with them either. Any suggestions? –  Mitchell Kaplan Dec 3 '13 at 21:24
    
@MitchellKaplan - Hi, I'm using Apply (@@) to pick up the numbered slot arguments, e.g. {#1, #2} & @@ {2013, 6, 30}. The month end calculation is done by DateList : setting the day to zero steps back a day, so DateList[{2013, 7, 0}] finds the last day of June. Likewise DateList[{2013, 14, 0}] finds the last day of January. –  Chris Degnen Dec 3 '13 at 22:15
    
Thanks, I've learned a lot from this, although I'll have to play with slots some more to fully get it. –  Mitchell Kaplan Dec 3 '13 at 22:44
add comment
quarterfy[{y_, m_, _, z___}] :=
 Switch[m,
  1 | 2 | 3, {y, 3, 31, z},
  4 | 5 | 6, {y, 6, 30, z},
  7 | 8 | 9, {y, 9, 30, z},
  10 | 11 | 12, {y, 12, 31, z}
  ]

quarterfy@{2013, 7, 1, 0, 0, 0}
(* {2013, 9, 30, 0, 0, 0}  *)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.