BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
noda6003
Quartz | Level 8

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  
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data want;
    set book1;
    by pid;
    prev_est=lag(est);
    if first.pid then anl=.;
    else anl=stdt-prev_est;
    drop prev_est;
run;

 

 

--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26
data want;
    set book1;
    by pid;
    prev_est=lag(est);
    if first.pid then anl=.;
    else anl=stdt-prev_est;
    drop prev_est;
run;

 

 

--
Paige Miller
Kurt_Bremser
Super User

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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 475 views
  • 1 like
  • 3 in conversation