function scalar store_days (scalar yr1, scalar mon1, scalar yr2, scalar mon2) scalar days = 0 loop i=yr1..yr2 --quiet loop j=1..12 --quiet if (i==yr1 && jmon2) break # finished else # change the final '7' if not using a 7-day week days += monthlen(j, i, 7) endif endloop endloop return days end function # main script # note: pdata.mat is matrix representation of spreadsheet matrix pdata = mread("pdata.mat") n = rows(pdata) # arguments are first (year, month) and last (year, month) sdays = store_days(2007, 3, 2009, 9) # create daily time series of the correct length nulldata sdays --preserve setobs 7 2007/03/01 series yr = NA series mon = NA scalar k0 = 1 scalar k string vname # choose your supermarket here scalar supermkt_index = 63 series supermkt = supermkt_index loop i=1..n --quiet if pdata[i,1] == supermkt[i] # the observation pertains to the chosen supermarket thisprod = pdata[i,2] # construct the product-price series name sprintf vname, "prod_%d", thisprod if !isseries(@vname) # must be a 'new' product: create series series @vname = NA endif thisyear = pdata[i,3] thismonth = pdata[i,4] thisprice = pdata[i,5] start = pdata[i,6] stop = pdata[i,7] # figure where to start the data if thismonth == 1 prevmonth = 12 prevyear = thisyear - 1 else prevmonth = thismonth - 1 prevyear = thisyear endif k = store_days(2007, 3, prevyear, prevmonth) + start # write the data into the relevant rows loop j=start..stop --quiet yr[k] = thisyear mon[k] = thismonth @vname[k] = thisprice k++ endloop endif endloop print supermkt yr mon prod* -o