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
UTSATT | PHDATE | PHDATE2 | PHDATE3 | |||||||||
1 | 1 | 20 | 20 | . | 1 | 1 | . | . | 1 | 20JAN10 | 20JAN10 | . |
0 | . | . | . | . | . | . | . | . | . | . | . | . |
0 | . | . | . | . | . | . | . | . | . | . | . | . |
0 | . | . | . | . | . | . | . | . | . | . | . | . |
0 | . | . | . | . | . | . | . | . | . | . | . | . |
2 | 3 | 8 | 20 | . | 3 | . | 1 | . | 1 | 08MAR10 | 08MAR10 | . |
0 | . | . | . | . | . | . | 1 | . | 1 | 19MAR10 | . | . |
Or are you having some issues using the lag correctly...
Please share your full code.
What does the dots in your data mean, missing values?
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;
UTSATT | PHmonth | Day | CheckDay | PH_Date | START_PHmonth | PH1 | PH2 | PH3 | Gjenstående | phdate | phdate2 | phdate3 | phdate4 |
1 | 1 | 20 | 20 | 1 | 1 | 1 | 20Jan2010 | 20Jan2010 | |||||
0 | |||||||||||||
0 | |||||||||||||
0 | |||||||||||||
0 | |||||||||||||
2 | 3 | 8 | 20 | 3 | 1 | 1 | 08Mar2010 | 08Mar2010 | |||||
0 | 1 | 1 | 19Mar2010 | ||||||||||
0 | |||||||||||||
0 | |||||||||||||
0 | |||||||||||||
0 | |||||||||||||
0 | |||||||||||||
0 | |||||||||||||
0 | |||||||||||||
0 | |||||||||||||
0 | |||||||||||||
0 | |||||||||||||
0 | |||||||||||||
0 | |||||||||||||
0 | |||||||||||||
0 | |||||||||||||
0 | |||||||||||||
0 | |||||||||||||
0 | |||||||||||||
0 | |||||||||||||
0 | |||||||||||||
3 | 1 | 11 | 20 | 1 | 1 | 1 | 11Jan2011 | 11Jan2011 | |||||
0 | 1 | 1 | 21Jan2011 | ||||||||||
0 | 1 | 1 | 01Feb2011 | ||||||||||
0 | |||||||||||||
0 |
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.
phdate3 = INTNX('day',LAG(phdate2),30);
phdate4 = INTNX('day',LAG(phdate3),30);
This worked so, thanks anyway !
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
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; |
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.