DATA Step, Macro, Functions and more

Urgent help! with assignment of sorting portfolios

New Contributor
Posts: 2

Urgent help! with assignment of sorting portfolios

Hi everyone,


I am now in a seminar of advanced investments, where i have an assignment of sorting the portfolios and construct the investing strategy based on momentum and volatility. However, I don't know anything about programming and I was told that SAS is the only the program that can do this assignment. I know this is a very troublesome question, as i need help from the very beginning with SAS...


Here is the code for creating momentum that my teacher gave us. and we need to do something similar for the variance ( a proxy for volatility). And how shall i do it?


data stkid;
set work.example;
keep stock code year month yearmth;

data ret;
set work.example;
keep stock_code year month retyearmth return;

proc sql ;
create table data as select *, log(b.return+1) as logretfrom stkid as a,ret as b
where a.stock_code=b.stock_code and (yearmth-12)<=b.retyearmth<=(yearmth-2);

proc sort data=data;by stock_code yearmth;

proc means

Also here are the codes for the major steps of the assignment, single sorting and double sorting. But don't know how to use it...

My variable name are: stockid, yearmonth, return.  SAS with SQL


I really tried to learn it by myself from all the materials online, but i am afraid this is really not my area. I relly run out of options so that's why i come here to ask this big question, hoping someone can help me work out the whole code, actually the main connections. I shall be more than grateful!

Super Contributor
Posts: 490

Re: Urgent help! with assignment of sorting portfolios

I can not follow you....

What do you want to do you want us to help you with exactly?

Be specific with what you need.

Give sample data and say how you want it to be, so we can help...

New Contributor
Posts: 2

Re: Urgent help! with assignment of sorting portfolios

Hello Mohamed,


The data is too big, so I will not upload it, but it looks like this:


time         stockid       return

200501    123             0.55

200502     123           -0.01

200501      234           0.09

200502      234            0.08


So it is a panel data, with around 500 stocks' monthly return in around 10 year's time. And the time format in the raw data is year together with month.


The assignment is to create and test two investing strategies based on momentum and volatility. Momentum will be the return of a stock of the past 12 month minus the past 1 month. So for example, the momentum for stock 123 in 200501 will be its cumulative return during 200401-200412. The proxy for volatility will be the variance, which is calculated based on the past 3 years. For example again, the volatility for stock 123 in 200501 will be calculated using the returns from 200201-200412.


Firstly I need to construct the two measures in my data set, then I need to follow the first two steps in the link (winsorize, single sorting). The macros are already there, but I do not know how to connect them... I would appreciate your help very much!

Ask a Question
Discussion stats
  • 2 replies
  • 2 in conversation