Help using Base SAS procedures

Conditional lag

Accepted Solution Solved
Reply
Contributor
Posts: 35
Accepted Solution

Conditional lag

I have this type of data;

ObjectPriceRegnrdepreciationCounterPrimaryopprinpristransaksjonstidest_price
2237500,00AE284040,000416667112237500
1600000,00AE284040,00027766720223750010961556578,033
1100000,00AE308640,000277667111100000
800000,00AR999930,00027766711800000
795000,00AR999930,00027766720800000398711590,9333
799000,00AR999930,0002776673080000032...

I want

IF counter = 3 then transaksjonstid= lag (transaksjonstid)+(transaksjonstid)

else if counter =4 then transaksjonstid = lag2(transaksjonstid)+lag(transaksjonstid) +transaksjonstid

etc...


Accepted Solutions
Solution
‎05-19-2015 09:48 AM
Super User
Posts: 9,682

Re: Conditional lag

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;

View solution in original post


All Replies
Solution
‎05-19-2015 09:48 AM
Super User
Posts: 9,682

Re: Conditional lag

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;

Super User
Super User
Posts: 7,407

Re: Conditional lag

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;

Super User
Posts: 6,946

Re: Conditional lag

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!

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Valued Guide
Posts: 858

Re: Conditional lag

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;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 246 views
  • 3 likes
  • 5 in conversation