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

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...

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

4 REPLIES 4
Ksharp
Super User

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;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

Kurt_Bremser
Super User

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!

Steelers_In_DC
Barite | Level 11

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;

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!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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