BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Xinhui
Obsidian | Level 7

Hello All

I'm trying to calculate rolling RSI (which is a financial indicate), I found this code, it should help me do the calculation. However, I'm very understand what is this piece of code doing. Would you please explain the code line by line, so that I can do the necessary changes to solve my case.  

 

%macro calRSI();

%do i=16 %to 7570;

data RSI;

set RSI;

lAveGain = lag1(AveGain);

lAveLoss = lag1(AveLoss);

if days = &i then do;

AveGain = (lAveGain*13+gain)/14;

AveLoss = (lAveLoss*13+loss)/14;

RS = AveGain / AveLoss;

RSI = 100-100/(1+RS); end;

%end; %mend;

%calRSI;

data summer.RSIresult;

set RSI; run;

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

The simplest way to look at macro code is to expand it yourself, here is the first two iterations of the macro loop:

data rsi;
  set rsi;
  lAveGain = lag1(AveGain);
  lAveLoss = lag1(AveLoss);
  if days = 16 then do;
    AveGain = (lAveGain*13+gain)/14;
    AveLoss = (lAveLoss*13+loss)/14;
    RS = AveGain / AveLoss;
    RSI = 100-100/(1+RS); 
  end;

data rsi;
  set rsi;
  lAveGain = lag1(AveGain);
  lAveLoss = lag1(AveLoss);
  if days = 17 then do;
    AveGain = (lAveGain*13+gain)/14;
    AveLoss = (lAveLoss*13+loss)/14;
    RS = AveGain / AveLoss;
    RSI = 100-100/(1+RS); 
  end;

...

So its basically repeating that code 16 to 7570 times.  Does it do what you want, I cannot possibly tell.  Is it good coding, I would say not.  Mixed case, no indentations, not finishing data steps with run all make the code very hard to read, and the macro loop seems to be pretty useless, as you could do the same thing directly in one datastep rather than generating 7500+ datasteps.

View solution in original post

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

The simplest way to look at macro code is to expand it yourself, here is the first two iterations of the macro loop:

data rsi;
  set rsi;
  lAveGain = lag1(AveGain);
  lAveLoss = lag1(AveLoss);
  if days = 16 then do;
    AveGain = (lAveGain*13+gain)/14;
    AveLoss = (lAveLoss*13+loss)/14;
    RS = AveGain / AveLoss;
    RSI = 100-100/(1+RS); 
  end;

data rsi;
  set rsi;
  lAveGain = lag1(AveGain);
  lAveLoss = lag1(AveLoss);
  if days = 17 then do;
    AveGain = (lAveGain*13+gain)/14;
    AveLoss = (lAveLoss*13+loss)/14;
    RS = AveGain / AveLoss;
    RSI = 100-100/(1+RS); 
  end;

...

So its basically repeating that code 16 to 7570 times.  Does it do what you want, I cannot possibly tell.  Is it good coding, I would say not.  Mixed case, no indentations, not finishing data steps with run all make the code very hard to read, and the macro loop seems to be pretty useless, as you could do the same thing directly in one datastep rather than generating 7500+ datasteps.

Xinhui
Obsidian | Level 7

Thank you for helping, I have used the "proc expand" code solved the problem. Still thank you. 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

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