BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Nilani
Fluorite | Level 6

Hi, I have a dataset like the following.

 

data my_data;
input appno id movement_date Date9. movement_indicator $;
format movement_date Date9. ;
datalines;
1 10 21JUN2023 A
1 10 19JUL2023 D
2 20 01DEC2022 A
2 20 04APR2023 D
2 20 16JUN2023 A
2 20 26NOV2023 D
4 20 28DEC2022 A
4 20 18MAR2023 D
4 50 28DEC2022 A
4 50 18MAR2023 D
6 60 17APR2023 D
7 70 27JUL2023 A
8 80 27NOV2022 D
8 80 27MAR2023 A
8 80 27JUL2023 D
;
run;

 

Arrivals are marked as 'A' and departures are 'D'. I want to calculate the time spent by each appno and id. I just want to disregard any single arrivals, and single departures. Also, if the first movement is a departure in any group they should also be ignored. The following is what I tried. But, still struglling to remove single departures and first departures.

 

data want(drop=LAG);
format LAG DATETIME24.;
set my_data;
LAG=LAG(movement_date);
by appno id;
if movement_indicator = 'A' then dif=0;
if movement_indicator = 'D' then dif=intCK("days",LAG,movement_date);
run;

Thanks. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15
/*
How about: 
 - filter data first
 - perform processing
?
*/

data have2;
  set my_data;
  by appno id;
  if not (first.ID=1 and 1=last.ID);
  if not (first.ID and movement_indicator="D");
  if not (last.ID and movement_indicator="A");
run;
proc print;
run;

data want;
  merge 
    have2(where=(movement_indicator="A") rename=(movement_date=date_arr) )
    have2(where=(movement_indicator="D") rename=(movement_date=date_dep) )
  ;
   diff = date_dep - date_arr;
run;
proc print;
run;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

3 REPLIES 3
yabwon
Onyx | Level 15
/*
How about: 
 - filter data first
 - perform processing
?
*/

data have2;
  set my_data;
  by appno id;
  if not (first.ID=1 and 1=last.ID);
  if not (first.ID and movement_indicator="D");
  if not (last.ID and movement_indicator="A");
run;
proc print;
run;

data want;
  merge 
    have2(where=(movement_indicator="A") rename=(movement_date=date_arr) )
    have2(where=(movement_indicator="D") rename=(movement_date=date_dep) )
  ;
   diff = date_dep - date_arr;
run;
proc print;
run;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Nilani
Fluorite | Level 6

Thanks so much

Ksharp
Super User
/*
Assuming your data have been sorted by DATE within APPNO.
*/
data my_data;
input appno id movement_date Date9. movement_indicator $;
format movement_date Date9. ;
datalines;
1 10 21JUN2023 A
1 10 19JUL2023 D
2 20 01DEC2022 A
2 20 04APR2023 D
2 20 16JUN2023 A
2 20 26NOV2023 D
4 20 28DEC2022 A
4 20 18MAR2023 D
4 50 28DEC2022 A
4 50 18MAR2023 D
6 60 17APR2023 D
7 70 27JUL2023 A
8 80 27NOV2022 D
8 80 27MAR2023 A
8 80 27JUL2023 D
9 80 24NOV2022 D
9 80 27NOV2022 A
9 80 27MAR2023 A
9 80 27JUL2023 D
;
run;
data temp;
 merge my_data 
 my_data(firstobs=2 keep=appno movement_indicator rename=(appno=_a movement_indicator=_m));
if (appno=_a and movement_indicator='A' and _m='D') or
   (appno=lag(appno) and lag(movement_indicator)='A' and movement_indicator='D' );
drop _:;
run;
data want;
 set temp;
 dif=dif(movement_date);
 if movement_indicator='A' then call missing(dif);
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 3 replies
  • 506 views
  • 2 likes
  • 3 in conversation