BookmarkSubscribeRSS Feed
Kiteulf
Quartz | Level 8

lag function is having some issues with givng me the right answer:

IF lag(UTSATT) IN (2,3) THEN do; phdate3 = INTNX('day',LAG(phdate2),30); end;

My data looks like

UTSATTPHDATEPHDATE2PHDATE3
112020.11..120JAN1020JAN10.
0............
0............
0............
0............
23820.3.1.108MAR1008MAR10.
0......1.119MAR10..
6 REPLIES 6
LinusH
Tourmaline | Level 20

Or are you having some issues using the lag correctly...Smiley Wink

Please share your full code.

What does the dots in your data mean, missing values?

Data never sleeps
Kiteulf
Quartz | Level 8

DATA Utsattbet2;

  SET Utsattbet;

  IF START_PHMonth > PHMonth THEN phdate = INTNX('day',datepart(etl_insert_dt),30);

  IF START_PHMonth = PHMonth AND gjenstående > 0 THEN phdate = INTNX('day',datepart(etl_insert_dt),0);

  IF START_PHmonth = PHmonth THEN DO;

  IF UTSATT IN (1,2,3) THEN phdate2 = phdate;

  End;

  IF lag(UTSATT) IN (2,3) THEN do; phdate3 = INTNX('day',LAG(phdate2),30); end;

  IF lag2(UTSATT) IN (3) THEN phdate4 =lag(phdate3)+60;

format phdate phdate2 phdate3 phdate4 test date7.;

RUN;

UTSATTPHmonthDayCheckDayPH_DateSTART_PHmonthPH1PH2PH3Gjenståendephdatephdate2phdate3phdate4
11202011120Jan201020Jan2010
0
0
0
0
2382031108Mar201008Mar2010
01119Mar2010
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
31112011111Jan201111Jan2011
01121Jan2011
01101Feb2011
0
0
LinusH
Tourmaline | Level 20

Is this your output data?

Please prove corresponding input data as well.

Supplying sample data in datalines/cards form will help us testing your code.

Data never sleeps
Kiteulf
Quartz | Level 8

phdate3 = INTNX('day',LAG(phdate2),30);

phdate4 = INTNX('day',LAG(phdate3),30);

This worked so, thanks anyway !

Haikuo
Onyx | Level 15

LAG function, if using conditionally, will produce "unexpected" results. "unexpected" does not mean "inconsistent" or "buggy", it merely doesn't behave the way granted or implied by its name-"lag" in a layman term. If you want to know LAG () inside-out, you need to study the SAS docs. However, if you just want it to produce the results you are expecting, as a rule of thumb, only to use it UNCONDITIONALLY. Now go back to your code:

IF lag(UTSATT) IN (2,3) THEN do; phdate3 = INTNX('day',LAG(phdate2),30); end;

IF lag2(UTSATT) IN (3) THEN phdate4 =lag(phdate3)+60;

The bold part of LAG has been used conditionally, you need to set up some temp variables to avoid that. So it can be as simple as the following:

_phdate2=lag( phdate2); _phdate3=lag(phdate3);

IF lag(UTSATT) IN (2,3) THEN do; phdate3 = INTNX('day',_phdate2,30); end;

IF lag2(UTSATT) IN (3) THEN phdate4 =_phdate3+60;

Good luck,

Haikuo

Kiteulf
Quartz | Level 8

I had to change it slightly!

_phdate2=lag( phdate2);
_phdate3=lag2( phdate2);
IF lag(UTSATT) IN (2,3) THEN do;
phdate3 = INTNX('day',_phdate2,30);
end;
IF lag2(UTSATT) IN (3) THEN do;
phdate4 = INTNX('day',_phdate3,60);end;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 6 replies
  • 903 views
  • 3 likes
  • 3 in conversation