Example input data and the desired result go a long way towards letting us figure out the issues.
Provide your existing dataset, or a sample, or something with the same variable names and types that has the same behavior if your data is sensitive, as a data step. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712 will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.
Also provide an example of the desired outpu for that example data.
The way you are using LAG is very likely why things only work for the first record. LAG inside a condion, like
ELSE IF _N_ GT 1 THEN DO;
BR = LAG(ACCOUNT);
is much more complex then you may think.
Please look at the results of this code:
data start;
input x y;
datalines;
1 2
3 4
5 6
;
run;
data new;
set start;
if _n_=1 then br=500;
else if _n_ gt 1 then do;
br = lag(x);
put _n_= br=;
end;
run;
Lag maintains a separate queue for each instance of the function. So when it is inside a condition as you used the first time lag is encountered then there is no previous value. So the values you are seeing for your BR are not in the sequence you want. And you are complicating things by using an apparently calculated variable, account, that does not exist in your base data BACKTESTING_H (at least the first time you ran the code).
All bets may be off after the first time you run something like this:
DATA BACKTESTING_H;
SET BACKTESTING_H;
as you have changed your previous data, possibly so much that running other code later to address the original logic may not work at all.
... View more