BookmarkSubscribeRSS Feed
SeanZ
Obsidian | Level 7

hi, i am trying to calculate the drawdown in portfolio analysis. I have a time series daily portfolio returns. And would like to calculate max drawdown (from peaks to bottom) and recovery as well for certain periods. I found some codes below, but could not follow the logic here. Can someone explain a little bit the logic or have some codes without macro involed? Thanks.  

 

 

%MACRO DO_BRANCH;

 

 

%DO A = %EVAL((%SCAN(&W,&C)) %TO 1 %BY -1;

%DO B = %EVAL(&A -1) %TO 1 %BY -1;

A&A.B&B = (LAG&B(sp500) -LAG&A(sp500))/ LAG&A(sp500);

%END;

%END;

DRAW = MIN(A%EVAL(%SCAN(&W,&C))B%EVAL((%SCAN(&W,&C)) -1)

%DO A = %EVAL((%SCAN(&W,&C)) %TO 1 %BY -1;

%DO B = %EVAL(&A -1) %TO 1 %BY -1;

,A&A.B&B

%END;

%END;

);

P0 = sp500;

%DO A = 1 %TO %SCAN(&W,&C);

P&A = LAG&A(DOLLAR%SCAN(&Y,&D));

%END;

PEAK = MAX (P0

%DO A = 1 %TO %SCAN(&W,&C);

,P&A

%END;

);

 

 

%DO E = 300 %TO 1 %BY -1;

IF LAG&E(DOLLAR%SCAN(&Y,&D)) GE PEAK THEN RECOVER = &E;

%END;

 

 

%MEND DO_BRANCH;

%DO_BRANCH;

1 REPLY 1
Ksharp
Super User

Can you post some dummy data and the output you want to see ?

It seems you need RANGE() .

 

proc sql;

 select range(return) as range from have;

quit;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 1 reply
  • 1686 views
  • 0 likes
  • 2 in conversation