Dear all,
May you help me to find what is wrong with my statement below?
I tried to generated the last Column with correct date , however, the statement generated column " Line start date new " which is wrong at the highlight column.
data test9;
set test8;
retain line_start_date_new;
if summerge=1 then line_start_date_new=lag(line_start_date);
else if missing(summerge) then line_start_date_new=.;
run;
Yesterday, I got help codes here, which generated correct results. But I do not understand how my codes are different from the help codes below. 😞
data test9;
set test8;
retain line_start_date_new ;
line_start_date_new = ifc(summerge=1, lag(line_start_date), line_start_date_new);
if missing(summerge) then call missing(line_start_date_new);
run;
PatientID | Line start date | Line end date | summerge | Line start date new ( wrong) | Line start date new (correct) |
A | 4/21/2015 | 4/24/2015 | 1 | . | |
A | 8/12/2015 | 8/31/2015 | . | . | |
A | 11/5/2015 | 11/5/2015 | . | . | |
A | 1/12/2016 | 4/26/2016 | . | . | |
A | 6/7/2016 | 9/20/2016 | 1 | 4/21/2015 | 1/12/2016 |
A | 10/5/2016 | 11/2/2016 | 2 | 4/21/2015 | 1/12/2016 |
A | 1/31/2017 | 1/31/2017 | . | . | . |
A | 5/4/2017 | 5/4/2017 | 1 | 6/7/2016 | 1/31/2017 |
A | 11/14/2017 | 1/10/2018 | 2 | 6/7/2016 | 1/31/2017 |
B | 1/16/2014 | 6/19/2014 | . | . | . |
B | 7/10/2014 | 9/4/2014 | . | . | . |
B | 10/9/2014 | 5/14/2015 | . | . | . |
B | 6/8/2015 | 6/8/2015 | 1 | 5/4/2017 | 10/9/2014 |
B | 9/24/2015 | 9/24/2015 | . | . | . |
B | 1/25/2016 | 3/7/2016 | . | . | . |
B | 3/24/2016 | 8/25/2016 | 1 | 6/8/2015 | 1/25/2016 |
B | 10/14/2016 | 2/17/2017 | 2 | 6/8/2015 | 1/25/2016 |
LAG() does NOT return the value from the previous observation. LAG() knows nothing about observations. It just remembers the values the variable had when you called it before. By only running LAG() on some observations it cannot return the value you want since you never passed that value to it so it could remember it. The IFN(), IFC() implementation work because SAS runs both the expressions every time the IFN() function runs.
It is just easier if you set the value into a variable first and then conditionally use the variable.
data test9;
set test8;
retain line_start_date_new;
lag_line_start_date=lag(line_start_date);
if summerge=1 then line_start_date_new=lag_line_start_date;
else if missing(summerge) then line_start_date_new=.;
drop lag_line_start_date ;
run;
Conditional Lag is a concept that take sometime to master
Anyways, The code that uses IFC will work. But since your dates are numeric, use IFN instead of IFC.
To learn more about conditional lag, I think there is a tech paper floating in google authored by Mkeiintz or Howard schrier.
this don't look correct
retain linestdt_new ;
should it be
retain line_start_date_new ;
LAG() does NOT return the value from the previous observation. LAG() knows nothing about observations. It just remembers the values the variable had when you called it before. By only running LAG() on some observations it cannot return the value you want since you never passed that value to it so it could remember it. The IFN(), IFC() implementation work because SAS runs both the expressions every time the IFN() function runs.
It is just easier if you set the value into a variable first and then conditionally use the variable.
data test9;
set test8;
retain line_start_date_new;
lag_line_start_date=lag(line_start_date);
if summerge=1 then line_start_date_new=lag_line_start_date;
else if missing(summerge) then line_start_date_new=.;
drop lag_line_start_date ;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.