Problem: We have a table of stock returns with columns "Date", "Ticker", "Return", which is not handy to analyse. We need to convert this table to the format "Date", "Ticker1 Return", "Ticker2 Return", "Ticker3 Return" etc. Let's see how to do it in SAS.
* Let's assume there is a SAS data file stocks in the file library.
data Ticker1;
set file.stocks;
if ticker="Ticker1";
Ticker1_Return=Return;
proc sort; by date; run;
data Ticker2;
set file.stocks;
if ticker="Ticker2";
Ticker2_Return=Return;
proc sort; by date; run;
data Ticker3;
set file.stocks;
if ticker="Ticker3";
Ticker3_Return=Return;
proc sort; by date; run;
data alltickers;
merge Ticker1 Ticker2 Ticker3;
by date;
* Done! Now, the dataset alltickers is the table we needed
* Saving the result to a CSV file alltickers.csvdata savealltickers;
set alltickers;
file "~/alltickers.csv";
if _n_=1 then put "date, Ticker1, Ticker2, Ticker3";
put date "," Ticker1"," Ticker2"," Ticker3";