using excel to analyze time series data

I have two columns of data - column A has dates in chronological order
and column B has corresponding closing prices. I'd like to
programmically (or with a formula), find the first date of each month
and add the corresponding closing prices and then divide by the number
of closing prices that meet the criteria.

Is this something that I can do in excel? Should I learn VBA? Or
should I look elsewhere?
0
sodani (28)
6/9/2011 1:32:11 AM
excel 39879 articles. 2 followers. Follow

8 Replies
647 Views

Similar Articles

[PageSpeed] 53

Just want to also note that by the first date of each month, I meant
the earliest date of each month, which in my data isn't always the 1st.
0
sodani (28)
6/9/2011 1:35:39 AM
On Jun 9, 9:32=A0am, eggman2001 <sod...@gmail.com> wrote:
> I have two columns of data - column A has dates in chronological order
> and column B has corresponding closing prices. I'd like to
> programmically (or with a formula), find the first date of each month
> and add the corresponding closing prices and then divide by the number
> of closing prices that meet the criteria.
>
> Is this something that I can do in excel? Should I learn VBA? Or
> should I look elsewhere?

not sure if my understanding is correct~
you can use column C, say, if your data is from A2:A100, then in C2:
=3DEOMONTH(A2,-1)+1 and copy cell to the whole series will do.
0
6/9/2011 7:56:45 AM
On Jun 8, 6:32=A0pm, eggman2001 <sod...@gmail.com> wrote:
> I have two columns of data - column A has dates in
> chronological order and column B has corresponding
> closing prices. I'd like to programmically (or with
> a formula), find the first date of each month and add
> the corresponding closing prices and then divide by
> the number of closing prices that meet the criteria.
> Is this something that I can do in excel? Should I
> learn VBA?

It might be doable in Excel alone.  But it seems easier to do in VBA.
See the function below.

To enter the function, press alt+F11 to open the VBA window.  Click
Insert > Module to open an editing pane on the left.  Copy and paste
the text of the function below into the VBA editing pane.  You can now
close the VBA window.

Usage....  You could write:

=3DavgPrice(A:A,D:D)

But it would be more efficient to write:

=3DavgPrice(A2:A4000,D2:D4000)

Alternatively, changes can be made to the VBA function to make it
somewhat more efficient with ranges like A:A and D:D.  However, such
ranges might be overly inclusive, including data that should not be
included in the average.

-----

'***** start copy here (you can omit this line)
Option Explicit
Function avgPrice(dR As Range, pR As Range) As Double
'calculate arithmetic average of price (pR)
'of first date in each month (dR).  assume
'dR and pR are each single columns.  assume
'dR and pR are in ascending order of dates
'in dR.
Dim d, p, s As Double, n As Long, i As Long
Dim m As Long, m0 As Long
d =3D dR
p =3D pR
s =3D p(1, 1)
m0 =3D Month(d(1, 1))
n =3D 1
For i =3D 2 To UBound(d, 1)
   m =3D Month(d(i, 1))
   If m <> m0 Then
      s =3D s + p(i, 1)
      m0 =3D m
      n =3D n + 1
   End If
Next
avgPrice =3D s / n
End Function
'***** end copy here (you can omit this line)
0
joeu2004 (766)
6/9/2011 5:15:45 PM
On Jun 9, 3:56=A0am, chia-yao chang <chiayaoch...@gmail.com> wrote:
> On Jun 9, 9:32=A0am, eggman2001 <sod...@gmail.com> wrote:
>
> > I have two columns of data - column A has dates in chronological order
> > and column B has corresponding closing prices. I'd like to
> > programmically (or with a formula), find the first date of each month
> > and add the corresponding closing prices and then divide by the number
> > of closing prices that meet the criteria.
>
> > Is this something that I can do in excel? Should I learn VBA? Or
> > should I look elsewhere?
>
> not sure if my understanding is correct~
> you can use column C, say, if your data is from A2:A100, then in C2:
> =3DEOMONTH(A2,-1)+1 and copy cell to the whole series will do.

I guess I'd like to do this in a way that will give me a single output
value. I assume this should get done programmically. Any thoughts?
0
sodani (28)
6/9/2011 5:18:01 PM
Errata....

On Jun 9, 10:15=A0am, joeu2004 <joeu2...@hotmail.com> wrote:
> Usage.... =A0You could write:
> =3DavgPrice(A:A,D:D)

As I wrote the function, this form can__not__ be used.  No matter:  it
is generally not efficient, and it might not be reliable.

I wrote:
> m0 =3D Month(d(1, 1))
[....]
> =A0 =A0m =3D Month(d(i, 1))

This assumes essentially contiguous data -- no major gaps, like sparse
years.  The modified function below is more reliable.

The following is an Excel implementation of the same algorithm.  I
have not tested to see which is more efficient.  Obviously the VBA
function is easier to user.

Assume dates in A2:A4000 and prices in D2:D4000.

=3D(D2+SUMPRODUCT(--(A2:A3999-DAY(A2:A3999)<>A3:A4000-
DAY(A3:A4000)),D3:D4000))
/(1+SUMPRODUCT(--(A2:A3999-DAY(A2:A3999)<>A3:A4000-DAY(A3:A4000))))


-----

Option Explicit
Function avgPrice(dR As Range, pR As Range) As Double
'calculate arithmetic average of price (pR)
'of first date in each month (dR).  assume
'dR and pR are each single columns.  assume
'dR and pR are in ascending order of dates
'in dR.
Dim d, p, s As Double, n As Long, i As Long
Dim m As Long, m0 As Long
d =3D dR
p =3D pR
s =3D p(1, 1)
m0 =3D d(1, 1) - Day(d(1, 1))
n =3D 1
For i =3D 2 To UBound(d, 1)
   m =3D d(i, 1) - Day(d(i, 1))
   If m <> m0 Then
      s =3D s + p(i, 1)
      m0 =3D m
      n =3D n + 1
   End If
Next
avgPrice =3D s / n
End Function


0
joeu2004 (766)
6/9/2011 5:56:06 PM
On Jun 9, 10:56=A0am, joeu2004 <joeu2...@hotmail.com> wrote:
>=A0The modified function below is more reliable.
[....]
> m0 =3D d(1, 1) - Day(d(1, 1))

I meant to explain....

If A2 is a date, A2-DAY(A2) is the date of the end of the previous
month.  If A2-DAY(A2) =3D A3-DAY(A3), then A2 and A3 are in the same
month.
0
joeu2004 (766)
6/9/2011 5:58:55 PM
On Jun 9, 1:58=A0pm, joeu2004 <joeu2...@hotmail.com> wrote:
> On Jun 9, 10:56=A0am, joeu2004 <joeu2...@hotmail.com> wrote:
>
> >=A0The modified function below is more reliable.
> [....]
> > m0 =3D d(1, 1) - Day(d(1, 1))
>
> I meant to explain....
>
> If A2 is a date, A2-DAY(A2) is the date of the end of the previous
> month. =A0If A2-DAY(A2) =3D A3-DAY(A3), then A2 and A3 are in the same
> month.

I don't know VBA (yet). Seems like it will be useful to learn in order
to complete these types of tasks.
0
sodani (28)
6/9/2011 8:16:40 PM
On Jun 9, 10:56=A0am, joeu2004 <joeu2...@hotmail.com> wrote:
> The following is an Excel implementation of the same
> algorithm.  I have not tested to see which is more
> efficient. =A0Obviously the VBA function is easier to user.

Apparently, it is also much more efficient.  For a large data set
(4570), the SUMPRODUCT formula takes more than 2.75 times as long as
the VBA function on my computer.

(But we are still talking about milliseconds for a single execution on
my computer.)
0
joeu2004 (766)
6/9/2011 11:34:46 PM
Reply:

Similar Artilces:

Formula without using numbers after decimal in the answer
I have a formula that derives the answer from a figure with a decimal. I don't want to use the figures after the decimal. Is there a way to just use the whole number and omit the numbers after the decimal without having to manually key in all these numbers manually? Thanks, Mustang You can use the INT function. This 'rounds down' any number to th nearest integer, e.g. if A1=2.567, a formula in B2 of =INT(A1) return 2 HTH Bruc -- swatsp0 ----------------------------------------------------------------------- swatsp0p's Profile: http://www.excelforum.com/member.php?...

Using Relative path for XML data file?
Is there a way to specify a relative path to an XML data file imported into Excel 2003? I am writing a web app that generates report data as XML for the user to download to their local machine. This data is to be consumed by an Excel reporting spreadsheet, which contains display-formatted tables and charts that are mapped to various data fields in an XML Map, which is in turn linked to the xml data file they will download. The idea is the user only needs to download the data for updates, not the whole spreadsheet. However, since I cannot predict the path where the user will store their...

Changing Cells and entering data in them
Thanks for the help again. Big thanks to Steve you've got me this far. I went out and bought a book, but it's like reading a foreign language. I was informed today that I can't have message boxes come up. I need to have the code point at the cells and if they are blank turn which ever one is blank red or if both are then both turn red then pause for each cell to be filled in. Cell F14 "Last Name" then automatically go to Cell F16 "First Name" on tab or enter. Basically if Cell F22 or F23 has an X in it, Cells F14 an F16 turn red and cell F14 has the focus...

Excel button problem
Hi All I have a macro that copies a worksheet in the active workbook and puts it into a new workbook - then formats it and deletes any buttons on the worksheet. On the first click on the button the macro works ok. On the second click, it fails because the all assigned macros on all buttons in the active workbook changed from "mba" to "book1!mba". Book 1 doesn't exists (wasn't opened, wasn't saved, doesn't have the macros). I've never experienced this problem before?? Can anyone help to solve this problem? FYI The macro to do this is c...

New to excel
Hi All, I'm new to Excel ( and to this forum :) ) and so I hope somebody may b able to help me. I've got 2 questions.... QUESTION 1 I've got a spreadsheet which takes data from one worksheet and uses i to calculate data in a second worksheet using the following code formula: =IF('4th November 2005'!B19="","nothing here dude",IF(B19<'4th Novembe 2005'!B19,"UP",IF(B19='4th November 2005'!B19,"Same",IF(B19>'4t November 2005'!B19,"DOWN")))) The problem is, when I create a new worksheet I have...

Looking for Excel Help
I'm a very novice Excel user and am looking for a little help with creating a formula for a spreadsheet I'm creating for my personal use. I would appreciate some assistance if possible. Thanks in advance. Dan --- Message posted from http://www.ExcelForum.com/ Hi Dan! Post a sample of what you want to do. Your question is just a tad open ended <g> -- Regards Norman Harker MVP (Excel) Sydney, Australia njharker@optusnet.com.au Excel and Word Function Lists (Classifications, Syntax and Arguments) available free to good homes. "DanB4105" <DanB4105.ywtpa@excelfor...

Excel 97 #9
Please can anyone help??? I have two columns in Excel 97. The first contains a list of statu values eg. pending, or granted or withdrawn. The second contains date eg.01/12/1997, 05/06/2003. I woudl like to know how to get all th granted apps before 31/12/2003. Can anyone help please -- Message posted from http://www.ExcelForum.com theres many ways, but an easiest way would be to do a sort. Highlight the 2 columns, click on data, then sort, then sort by status, then by date. this should group them all together. hope this helps...toe >-----Original Message----- >Please can anyo...

Stop My Documents from opening all the time?
Is there anyway to stop My Documents from opening everytimg I start a Windows Explorer session? I guess there's no future in trying to delete it, because so many products try to put stuff there. Right? Laurel wrote: > Is there anyway to stop My Documents from opening everytimg I start a > Windows Explorer session? > If you want Explorer to open somewhere else, simply modify the properties of the shortcut you're using to open it. All you need do is create/modify the target field of your Windows Explorer shortcut to point where you want it. Co...

using the journal on outlook
Once I link an email to the journal, can I still find that email in my mail box? I seem to be able to get to it only via the journal. If this is the way it is supposed to be, how do I remove it from the journal and get it back into my mail box? Am I just missing something? -- thanks, Independent Are you linking to the item or putting a copy into the journal item? Also, has the item been archived or not? "Independent" <Independent@discussions.microsoft.com> wrote in message news:868279F2-53C8-403A-97F5-604CEECD873C@microsoft.com... > Once I link an email to the journ...

Excel corrupts when asking to update vlookups
We are experiencing weird behavior with some Office 2K3 Excel spreadsheets that contain lots of calculations, but no macros. On some pc’s Excel acts normally, on others you get the error. I have a couple of screen shots available. Any help is appreciated. If desired, send your file to my address below. I will only look if: 1. You send a copy of this message on an inserted sheet 2. You give me the newsgroup and the subject line 3. You send a clear explanation of what you want 4. You send before/after examples and expected results. -- Don Gu...

Uninstall of mappoint has caused errors with excel
Hi, I am running Office 2003 on the terminal server (windows 2003) and had a copy of mappoint as well. This is a mapping program. We ininstalled mappoint which has caused an error message with Excel and other office products. The error says "Cd:\documents and settings\administrator.ocrdc1\application data\microsoft\addins c:\Program files\common files\microsoft shared\geography\mpoai9.dll is not a valid add-in." I then click OK and excel opens up and everything is fine. The problem is that we are using other programs as well such as Quickbooks that export to excel and t...

Pulling data from separate tabs
When charting in Excel 2002 is there a way to use sets of data from two different tabs within the same worksheet? For example, a spreadsheet contains separate tabs for prior year and current year data. Is there a way to reference the data or label series to pick up data from both? I tried pointing and clicking, and then typing the following as a reference for the axis labels: ='Prior Year'!$B$110:$M$110,'Current Year'!'$B$110:$M$110 but receive an error that I'm referring to an external worksheet. I've used the comma (') in the past to reference breaks ...

How do I Remove a Split from my Comments in Excel 2003? #2
I have set my current workbook to split/freeze the first column and first 2 rows. Now, when I add a comment to the second row (in any column) my comments are cut off if I should scroll down. I don't ever remember the behavior before. And I don't know what I've done to enable it but it's really annoying. How do turn this off ? ...

learning Excel #3
Hi, I was considering learning Excel as an additional tool for my data analysis work. Is it better to use data sets that I have previously used with SPSS and apply the same analysis tools as in SPSS? For applying the appropriate tools, I was considering using excel's online help. The second option I have is to use some excel book for data analysis and apply the techniques to data sets provided with the book. Any suggestions????? regards Metal ...

unable to paste Excel 2003 chart into Outlook 2003
(This was posted on "excel.charting" group.) I have a user who's unable to paste an Excel 2003 chart into Outlook 2003 email message. In Outlook options, the checkbox is selected for "Use Microsoft Office Word 2003 to edit e-mail messages". When I tested this on my own computer running the same version of Office, if the box is check, I have no problem pasting; if this box is cleared, I cannot paste. But on his computer, it doesn't work regardless. Thanks and regards, TL ...

Show date, time & day of week in one cell
Can I show the date, time and day of week in one cell. I have: 09/03/07 8:30 AM in one cell using the format (Format/Cells/Custom): [$-409]mm/dd/yy h:mm AM/PM;@ Excel refuses to accept ddd for Mon or dddd Monday at the end of the format I want it to read: 09/03/07 8:30 AM Monday in 1 cell. I have Excel 2003. One way: mm/dd/yy h:mm AM/PM dddd;@ In article <Xns99B8A3CAF9130pencilunlistedcom@208.49.82.220>, Burp <burp@beep.comINVALID> wrote: > Can I show the date, time and day of week in one cell. > > I have: > 09/03/07 8:30 AM > in one cell using the for...

Disable Secure Sockets Layer on exchange server when using RPC over HTTP
Hi im trying to enable RPC over HTTP to enable users to establish contact to my Excahger server 2003 over the internet. Now, I dont want to use SSL (security not that important) and i am told by this article that i can disable SSL in windows registry. Quote: Note While RPC over HTTP does not require Secure Sockets Layer, you must modify the registry to enable RPC over HTTP if you do not want to use Secure Sockets Layer. Microsoft recommends that you enable and require Secure Sockets Layer for your RPC over HTTP communications. At this address: http://support.microsoft.com/?id=833401 But i ...

data sort
ok now should be simple >> I need to sort by month on data that is held in format >> day/month so eg 1511 1510 3011 3010 now custom/ends with/ 11... does not work custom/ends with/ ??11.. or *11 does not work either contains 11 does not work (& would also be wrong if data set contained 1011) but still I am stumped so any help would be great cheers Alex I would be inclined to add a new, temporary field of formulas that pull off the right 2 digits, and sort by that: =RIGHT(A1,2) -- Jim Rech Excel MVP ...

Over Time Pay rate 1.51?
Its that time of year again, Audit time. The auditors have come across a very strange problem it seems that around October of last year random employees have had there OT pay rates increased to 1.51 (instead of the standard 1.50), I cant for the life of me figure out how this has come to be. Has anyone come across this problem before? And is there something I can do to fix or prevent this from reoccurring? It’s just not feasible to check each employee every week before payroll. Im certain that the employees were setup correctly initially. Thanks I have seen this and have not been ab...

mail queues for long on Exchange 2000 server time before being sent
I have a situation where all outgoing mail sits in a queues on the Exchange 2000 server (one per domain) for about 45 minutes before being sent. The queues in question have a status of ready. There are also other queues of that are retries (mostly NDRs from junk mail). We are only a small company and the amount of mail is not great. What might Exchange be waiting for before sending this mail? Is there something I can tweak to make it send mail sooner? Thanks for any help, Angus. Angus, Where does the mail go after it leaves your Exchange server, does it go to a smarthost/isp or d...

Help with importing data
Can I have users fill in a form in Access and have that data be transferred and updated to a spreadsheet. Need for fill out several fields and then export to a specific spreadsheet and place that data into the cells that will update that cell (add to the total in that cell) of a spreadsheet. ...

Does Outlook use the DAV protocol?
I'm an Outlook Express user who wants to switch to Outlook. I received a notice from Microsoft that includes the following: "... as of June 30, 2008, Microsoft is disabling the DAV protocol and you will no longer be able to access your Hotmail Inbox via Outlook Express." Please tell me if this action by Microsoft will affect Outlook in the same manner, or am I free to make the switch. "BudV" <BudVitoff@(NO)att.(SPAM)net> wrote in message news:%230XUDi%23zIHA.2384@TK2MSFTNGP02.phx.gbl... > I'm an Outlook Express user who wants to switch to Outlook...

Formula for cross tab data filling
Hi All Excel 2003 How to using formula for data filling as below (Y/N) ? Sheet A Product A Product B Product C System A Y N Y System B Y Y N Sheet B System A Product A System A Product C System B Product A System B Product B moonhkt ...

q Emailing Excel Sheet With Outlook
I am trying to send a table from Excel within the body of an Outlook (lastest versions) to be sent as a fax. Am able to do this, BUT when the document prints out on the fax machine the formating is off (too big for the sheet). I am flexible on changing the method I send it to the fax machine, however it must be sent to the fax via macro. Below is the code I am using. Any help would be greatly appreciated. David Public Sub DoIt() 'On Error GoTo Handler Dim EmailAddress(0 To 2) As String Dim Count As Integer Dim N As Integer Dim sRec1(0) As String Dim sRec2(0 To 1) As...

predict future data
Is there a way to create an XY line graph wih plotted data, yet leave room to predict future data on the axes? I can get the graph, but the x and y axes stop at the last data points, and I want those axes continued so that the existing data can be examined and future data predicted and plotted on the same graph, but I am not sure how to accomplish this. Any suggestions would be appreciated. Thanks. Jeff 1) Click on data series in chart, use Add trendline; in Option tab specify some units forward OR 2) Read Help about TREND and FORECAST, and SLOPE and INTERCEPT OR 3) Get crystal bal...