I have this type of data;
ObjectPrice | Regnr | depreciation | Counter | Primary | opprinpris | transaksjonstid | est_price |
2237500,00 | AE28404 | 0,000416667 | 1 | 1 | 2237500 | ||
1600000,00 | AE28404 | 0,000277667 | 2 | 0 | 2237500 | 1096 | 1556578,033 |
1100000,00 | AE30864 | 0,000277667 | 1 | 1 | 1100000 | ||
800000,00 | AR99993 | 0,000277667 | 1 | 1 | 800000 | ||
795000,00 | AR99993 | 0,000277667 | 2 | 0 | 800000 | 398 | 711590,9333 |
799000,00 | AR99993 | 0,000277667 | 3 | 0 | 800000 | 32 | ... |
I want
IF counter = 3 then transaksjonstid= lag (transaksjonstid)+(transaksjonstid)
else if counter =4 then transaksjonstid = lag2(transaksjonstid)+lag(transaksjonstid) +transaksjonstid
etc...
Code not tested.
lag_transaksjonstid=lag (transaksjonstid);
lag_transaksjonstid2=lag 2(transaksjonstid);
IF counter = 3 then transaksjonstid=lag_transaksjonstid+transaksjonstid;
else if counter =4 then transaksjonstid = lag_transaksjonstid2+lag_transaksjonstid +transaksjonstid;
Code not tested.
lag_transaksjonstid=lag (transaksjonstid);
lag_transaksjonstid2=lag 2(transaksjonstid);
IF counter = 3 then transaksjonstid=lag_transaksjonstid+transaksjonstid;
else if counter =4 then transaksjonstid = lag_transaksjonstid2+lag_transaksjonstid +transaksjonstid;
Not entirely sure on your logic, but it sounds like a join or subquery (possibly hash) would be easier:
proc sql;
create table WANT as
select A.*,
(select sum(TRANSAKSJONSTID) from HAVE where REGNR=A.REGNR and (A.COUNTER-COUNTER) <= COUNTER <= A.COUNTER))
as TOT
from HAVE A;
quit;
Do not call the lag() function conditionally, as it feeds its queue only when called. If you don't call it for N records, the first call at N+1 will still yield the initial missing value, instead of the expected value from the observation N!
You shouldn't use lag this way, set up your lag variables and then use those conditionally:
data have;
infile cards dsd dlm=',';
input ObjectPrice Regnr $ depreciation Counter Primary opprinpris transaksjonstid est_price;
cards;
2237500.00,AE28404,0.000416667,1,1,2237500,,
1600000.00,AE28404,0.000277667,2,0,2237500,1096,1556578.033
1100000.00,AE30864,0.000277667,1,1,1100000,,
800000.00,AR99993,0.000277667,1,1,800000,,
795000.00,AR99993,0.000277667,2,0,800000,398,711590.9333
799000.00,AR99993,0.000277667,3,0,800000,32,
;
run;
data want;
set have;
l1=lag(transaksjonstid);
l2=lag2(transaksjonstid);
if counter = 3 then test = transaksjonstid + l1;
else if counter= 4 then test = transaksjonstid + l1 + l2;
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.