Help using Base SAS procedures

Lag function yields no results

Reply
Contributor
Posts: 34

Lag function yields no results

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..
Super User
Posts: 5,255

Re: Lag function yields no results

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
Contributor
Posts: 34

Re: Lag function yields no results

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
Super User
Posts: 5,255

Re: Lag function yields no results

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
Contributor
Posts: 34

Re: Lag function yields no results

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

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

This worked so, thanks anyway !

Respected Advisor
Posts: 3,124

Re: Lag function yields no results

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

Contributor
Posts: 34

Re: Lag function yields no results

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;
Ask a Question
Discussion stats
  • 6 replies
  • 268 views
  • 3 likes
  • 3 in conversation