3
$\begingroup$

I'm importing financial data into Mathematica, creating column headers for each array and exporting to Excel. The code below accomplishes most of this, but it creates a different worksheet for each array. I want to combine multiple arrays onto a single worksheet, one per column with the header at the top. Suggestions? Thanks.

spy = Prepend[FinancialData["SPY", "Jan.1,2012"], {, "SPY"}];

efa = Prepend[FinancialData["EFA", "Jan.1,2012"], {, "EFA"}];

Export["data.xls", {spy, efa}, "XLS"]
$\endgroup$
1
  • $\begingroup$ Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. $\endgroup$ Commented Feb 19, 2015 at 5:10

1 Answer 1

3
$\begingroup$

If spy and efa have the same length, you can use Transpose:

Export["data.xls", Transpose[Join @@ Transpose /@ {spy, efa}]]

enter image description here

Update: If spy and efa have different lengths, you can use PadRight before the outermost Transpose:

d1 = FinancialData["SPY", "Jan.1,2012"];
d2 = FinancialData["EFA", "Jan.1,2012"];

spyb = Prepend[d1[[;; 10]], {, "SPY"}];
efab = Prepend[d2[[;; 20]], {, "EFA"}];

Export["data3.xls", Transpose[PadRight[Join @@ Transpose /@ {spyb, efab}, Automatic, ""]]]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thanks, kguler. This is a simple fix. However, there will be times when spy, efa or others will not be the same length. Any suggestions for creating the same type of format if they have different lengths? $\endgroup$ Commented Feb 20, 2015 at 2:13
  • $\begingroup$ @Anthony, please see the update for an approach to deal with different-length columns. $\endgroup$ Commented Feb 20, 2015 at 6:17

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.