I'm going to select N subsets from a matrix in a DO loop.
N is variable, let's say 5;
Any tips on how to name the subsets: SUB1, SUB2, SUB3, SUB4, SUB5 ?
proc iml; x=j(10,5); call randgen(x,'normal'); names='sub1':'sub5'; do i=1 to ncol(x); call valset(names[i],x[,i]); end; show names; print sub1,sub2,sub3,sub4,sub5; quit;
What do you specifically mean by a subset from a matrix? 🙂
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:
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;
I rarely name the subsets. In a loop you can extract the first subset, do what you want with it (for example, compute its mean) and store the result. Then you extract the next subset and store its results, etc, until all subsets have been handled.
For a reminder that you should pre-allocate the results matrix for efficiency, see
proc iml; x=j(10,5); call randgen(x,'normal'); names='sub1':'sub5'; do i=1 to ncol(x); call valset(names[i],x[,i]); end; show names; print sub1,sub2,sub3,sub4,sub5; quit;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.