A useful approach is to read each stock, analyze each stock, and then go on to the next stock. After reading all the data and computing statistics for each stock, you can do additional analysis that compares different stocks.
For example, the following loop reads one stock into X. If computes various statistics for each stock. After the loop is over, I have a matrix called RESULTS that contains all the information that I need to build my portfolio. The following program uses the following techniques:
How to read big data in blocks
How to use the WHERE clause to read subsets of a data set
How to assign matrix attributes so you can access rows or columns by names
proc iml;
StockNames = {"IBM" "Intel" "Microsoft"}; /*{"AAPL" "AXP" ... "WMT" "XOM"}*/
results = j(3, ncol(StockNames), .);
mattrib results rowname={"Mean" "Stddev" "MaxVol"}
colname=StockNames;
use Sashelp.stocks;
/* http://blogs.sas.com/content/iml/2013/01/21/reading-big-data.html */
do ID = 1 to ncol(StockNames);
/* read in data for each stock */
/* http://blogs.sas.com/content/iml/2016/04/04/where-clause-in-sasiml.html */
read all var _NUM_ into X[colname=varNames]
where(Stock=(StockNames[ID]));
/* http://blogs.sas.com/content/iml/2012/10/01/access-rows-or-columns-of-a-matrix-by-names.html */
results["Mean", ID] = mean(X[,"Close"]);
results["Stddev", ID] = std(X[,"Close"]);
results["MaxVol", ID] = max(X[,"Volume"]);
end;
close;
print results;
... View more