SAS Procedures

Help using Base SAS procedures
BookmarkSubscribeRSS Feed
ArthMizz
Calcite | Level 5

Hi,

 

I am trying to compute the Relative Strengh Index (RSI) of 500 stocks but on a monthly basis. I wrote the following code based on a paper found online, however, there are some mistakes but I do not know how to fix them. Moreover, there are some statements that I do not understand what "%do i=16 %to 7570;" stand for in the code.

 

data Project.MonthlyPrice;
    set RSI;
    by PERMNO;
    lprc = lag(prc);
    if prc>lprc then gain = prc-lprc;
    if prc<lprc then loss = lprc-prc;
    if first.PERMNO then do;
  gain = .;
  loss = .;
    end;
    retain months 0;
    months+1;
    if first.PERMNO then months = 1;
    if gain = . then gain = 0;
    if loss = . then loss = 0;
    retain sumgain 0;
    sumgain = sumgain + gain;
    retain sumloss 0;
    sumloss = sumloss + loss;
    if months=15 then do;
        AveGain = (sumgain-gain) / 14;
 AveLoss = (sumloss-loss) / 14;  
 RS =  AveGain / AveLoss;
 RSI = 100-100/(1+RS);
    end;
    drop sumgain sumloss;
run;
%macro calRSI();
    %do i=16 %to 7570;
        data Project.MonthlyPrice;
     set RSI;
     lAveGain = lag1(AveGain);
     lAveLoss = lag1(AveLoss);
     if months = &i then do;
         AveGain = (lAveGain*13+gain)/14;
  AveLoss = (lAveLoss*13+loss)/14;
  RS =  AveGain / AveLoss;
  RSI = 100-100/(1+RS);
     end;
    %end;
%mend;
data Project.MonthlyPrice;
    set RSI;
run;

 

 

Thank in advance

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

Please provide a link to the paper and some example data. Makes it a lot easier to help you.

ArthMizz
Calcite | Level 5

here's the link to the paper: http://support.sas.com/resources/papers/proceedings12/163-2012.pdf

 

I am using CRSP from WRDS Monthly stock return and S&P500 returns in my dataset. I used the past 10 years of data (from December 2007 to December 2017) and the entire database (almost 7200 stocks).

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1396 views
  • 0 likes
  • 2 in conversation