I Have a dataset which has PID and 2 dates and i need duration taken from date STDT-EST(from same PID above EST).
This is the dataset
DATA WORK.Book1;
LENGTH
PID $ 12
STDT 8
EXEN 8
EXD 8
STDY 8
EST 8
GR $ 8 ;
FORMAT
PID $CHAR12.
STDT DATE9.
EXEN BEST12.
EXD BEST12.
STDY BEST12.
EST DATE9.
GR $CHAR8. ;
INFORMAT
PID $CHAR12.
STDT DATE9.
EXEN BEST12.
EXD BEST12.
STDY BEST12.
EST DATE9.
GR $CHAR8. ;
INFILE DATALINES4
DLM='7F'x
MISSOVER
DSD ;
INPUT
PID : $CHAR12.
STDT : BEST32.
EXEN : BEST32.
EXD : BEST32.
STDY : BEST32.
EST : BEST32.
GR : $CHAR8. ;
DATALINES4;
01-001-S0012.11122277no event
01-001-S0012222842108222843
01-001-S0012222852108222841
01-001-S0012222852108222842
01-001-S0012222872108222843
01-001-S0015.11122306no event
01-001-S001522313238223131
01-001-S0015.3301522320no event
01-001-S0015.4302222327no event
01-001-S00152233853029223341
01-001-S0015.6303622341no event
01-001-S001922354111223541
01-001-S0019.23822361no event
01-001-S0019.3101522368no event
01-001-S0025.11122389no event
;;;;
and i want something like below duration
PID | STDT | EXEN | EXD | STDY | EST | GR | ANL |
01-001-S0012 | 1 | 1 | 1 | 28Dec2020 | no event | ||
01-001-S0012 | 04Jan2021 | 2 | 10 | 8 | 04Jan2021 | 3 | value as 01jan2021-28dec2020 |
01-001-S0012 | 05Jan2021 | 2 | 10 | 8 | 04Jan2021 | 1 | value as 05jan2021-04jan2021 |
01-001-S0012 | 05Jan2021 | 2 | 10 | 8 | 04Jan2021 | 2 | value as 05jan2021-04jan2021 |
01-001-S0012 | 07Jan2021 | 2 | 10 | 8 | 04Jan2021 | 3 | value as 07jan2021-04jan2021 |
01-001-S0015 | 1 | 1 | 1 | 26Jan2021 | no event | ||
01-001-S0015 | 02Feb2021 | 2 | 3 | 8 | 02Feb2021 | 1 | value as 02Feb2021-26Jan2021 |
01-001-S0015 | 3 | 30 | 15 | 09Feb2021 | no event | ||
01-001-S0015 | 4 | 30 | 22 | 16Feb2021 | no event | ||
01-001-S0015 | 27Feb2021 | 5 | 30 | 29 | 23Feb2021 | 1 | value as 27Feb2021-23Feb2021 |
01-001-S0015 | 6 | 30 | 36 | 02Mar2021 | no event | ||
01-001-S0019 | 15Mar2021 | 1 | 1 | 1 | 15Mar2021 | 1 | Value as 15Mar2021-02Mar2021 |
01-001-S0019 | 2 | 3 | 8 | 22Mar2021 | no event | ||
01-001-S0019 | 3 | 10 | 15 | 29Mar2021 | no event | ||
01-001-S0025 | 1 | 1 | 1 | 19Apr2021 | no event |
data want;
set book1;
by pid;
prev_est=lag(est);
if first.pid then anl=.;
else anl=stdt-prev_est;
drop prev_est;
run;
data want;
set book1;
by pid;
prev_est=lag(est);
if first.pid then anl=.;
else anl=stdt-prev_est;
drop prev_est;
run;
I'd use the IFN function:
data want;
set book1;
by pid;
anl = ifn(first.pid,.,stdt - lag(est));
run;
All arguments to IFN are evaluated before the condition in the first argument is applied, so LAG will always correctly fill its queue.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.