Calculating previous 18 months in an array

Cuffy

New Member
Hi all,

I am trying to use an array and a do loop to calculate the 1st date of the month for the previous 18 months in order to show usage for items for each of the previous 18 months.

My problem is how to decrement the year twice to go back to 2011 when the mnth value is < 1.

Here is my current code:

def new shared var periods as int extent 19 label "Start".
def new shared var p as int.
def new shared var mnth as int.
def new shared var w-months as date extent 19 label "Start".


/* Include file for E and O Monthly Buckets */
/* Calculate Monthly Buckets */

Assign
periods[1] = 0
periods[2] = 1
periods[3] = 2
periods[4] = 3
periods[5] = 4
periods[6] = 5
periods[7] = 6
periods[8] = 7
periods[9] = 8
periods[10] = 9
periods[11] = 10
periods[12] = 11
periods[13] = 12
periods[14] = 13
periods[15] = 14
periods[16] = 15
periods[17] = 16
periods[18] = 17
periods[19] = 18.
p = 0.


do p = 1 to 19:

mnth = month(today) - periods[p].

/* message p mnth view-as alert-box. pause. */


if mnth < 1 then /*decrement year*/
w-months[p] = date(12 + mnth,1,year(today) - 1).

else
w-months[p] = date(month(today) - periods[p], 1, year(today)).
end. /* DO */


display w-months with side-labels width 80.


Any advice greatly appreciated,

Best regards,

Martin
 

Cringer

ProgressTalk.com Moderator
Staff member
It's not completely what you want, but it's a lot more elegant:
Code:
define variable lv-Date as date no-undo. 
define variable lv-i as integer no-undo. 

lv-Date = 06/01/2013. 


do lv-i = 1 to 18:
    message add-interval(lv-Date,lv-i * -1,"MONTH") view-as alert-box. 
end.
 

Cuffy

New Member
It's not completely what you want, but it's a lot more elegant:
Code:
define variable lv-Date as date no-undo.
define variable lv-i as integer no-undo.
 
lv-Date = 06/01/2013.
 
 
do lv-i = 1 to 18:
    message add-interval(lv-Date,lv-i * -1,"MONTH") view-as alert-box.
end.

Thank you very much, that works well.
 

Cuffy

New Member
It's not completely what you want, but it's a lot more elegant:
Code:
define variable lv-Date as date no-undo.
define variable lv-i as integer no-undo.
 
lv-Date = 06/01/2013.
 
 
do lv-i = 1 to 18:
    message add-interval(lv-Date,lv-i * -1,"MONTH") view-as alert-box.
end.

Thank you very much, that works well.


Unfortunately "add-interval" doesn't seem to want to work in the older version of progress, are there any other alternatives?

Thanks
 

Cringer

ProgressTalk.com Moderator
Staff member
Not really that are as neat as that. The best alternative is to get off ancient, obsolete and unsupported versions of progress ;)
 

Stefan

Well-Known Member
This is very basic...

Code:
DEF VAR iyear AS INT.
DEF VAR imonth AS INT.
DEF VAR iperiod AS INT.
 
ASSIGN 
   iyear  = YEAR( TODAY )
   imonth = MONTH( TODAY )
   .
 
DO iperiod = 1 TO 18:
   
   MESSAGE ISO-DATE( DATE( imonth, 1, iyear ) ) VIEW-AS ALERT-BOX.
   imonth = imonth - 1.
   IF imonth = 0 THEN
      ASSIGN 
         iyear = iyear - 1
         imonth = 12
      .
 
END.
 

Cuffy

New Member
Stefan - Thank you very much.

Cringer - couldn't agree more - hopefully this will be one of the very last reports I have to create on that version of Progress.
 

Nikhil Limaye

New Member
Code below could be another way of doing it -
/********************************************************************/
def var m_cnt as integer.
def var m_date as date.
ASSIGN m_date = today.
do m_cnt = 1 to 18:
if month(m_date) <> 3 then
ASSIGN m_date = m_date - 30.
else
assign m_date = m_date - 27.
if day(m_date) <> 1 then
assign m_date = date(string(month(m_date)) + "/01/"
+ string(year(m_date))).
disp m_cnt m_date with frame a down.
down with frame a.
pause.
end.
/*******************************************************************/
 

Stefan

Well-Known Member
Put code between code tags please.

You should not be putting a character into the date function like this - it makes you dependent on the date format of the progress session, your code will break when session:date-format = 'dmy' (ie non-US).

Results based on today (2013-09-01) in my 'dmy' session (with display date format adjusted to iso-date to prevent confusion):

Code:
1 2013-01-08 <- already wrong on the first iteration
2 2012-01-12 <- not even close
3 2011-01-12 <- and the drama continues

+1 for convulted code
 
Top