I have a SAS dataset with STS and CHGDATE. I would like to create a New Date variable NEWCHGDATE while retaining the same Date until the same STS=03 continues.
ID CHGDATE STS
101 11/22/2022 03
101 11/23/2022 03
101 11/24/2022 03
101 03/08/2023 04
102 01/11/2022 03
102 01/12/2022 05
102 02/12/2023 03
102 02/15/2023 04
WANT
ID CHGDATE STS NEWCHGDATE
101 11/22/2022 03 11/22/2022
101 11/23/2022 03 11/22/2022
101 11/24/2022 03 11/22/2022
101 03/08/2023 04 03/08/2023
102 01/11/2022 03 01/11/2022
102 01/12/2022 05 01/12/2022
102 02/12/2023 03 02/12/2023
102 02/15/2023 04 02/15/2023
For e.g In the above, ID 101 has STS=03 from 11/22 until 11/24 so I would like to retain the first Date 11/22/2022 until it reached STS=04 on 03/08/2023.
Here is my Query which is not giving me the expected result
data want;
set have;
by id;
if first.id then do;
lag_sts = '';
newchgdate = chgdate;
end;
else lag_sts = sts;
if sts not in ('03') then newchgdate = chgdate;
else newchgdate = ifn(missing(lag_sts),., lag(newchgdate));
run;
Use a
RETAINstatement to specify which variables will not get reset to missing at the top of the DATA Step implicit loop.Use the
LAGfunction to examine the priorstsvalue. Be careful about using LAG inside a conditional clause (which is not happening here). In this example the LAG is occurring in the test expression.Example:
The test expression is a little tricky if you are new to SAS and DATA Step.