DATA Step, Macro, Functions and more

Computation using SAS

Accepted Solution Solved
Reply
Super Contributor
Posts: 324
Accepted Solution

Computation using SAS


Hello-

I wish to compute length of stay for each observation by subtracting ADT_IN from ADR_OUT.

 

Please help me with the code 

 

ADT_IN ADT_OUT
2/9/2017 19:50 2/10/2017 14:21
2/8/2017 17:50 2/10/2017 12:40
2/7/2017 11:43 2/9/2017 12:50
2/8/2017 12:56 2/11/2017 0:42
2/8/2017 12:56 2/11/2017 0:42
2/8/2017 12:56 2/11/2017 0:42
2/8/2017 12:56 2/11/2017 0:42
2/8/2017 12:56 2/11/2017 0:42
2/8/2017 12:56 2/11/2017 0:42

Accepted Solutions
Solution
2 weeks ago
Occasional Contributor
Posts: 6

Re: Computation using SAS

[ Edited ]
Posted in reply to desireatem

 

 

data have;
input (ADT_IN ADT_OUT)(:mdyampm. &);
format ADT_IN ADT_OUT datetime.;
cards;
2/9/2017 19:50 2/10/2017 14:21
2/8/2017 17:50 2/10/2017 12:40
2/7/2017 11:43 2/9/2017 12:50
2/8/2017 12:56 2/11/2017 0:42
2/8/2017 12:56 2/11/2017 0:42
2/8/2017 12:56 2/11/2017 0:42
2/8/2017 12:56 2/11/2017 0:42
2/8/2017 12:56 2/11/2017 0:42
2/8/2017 12:56 2/11/2017 0:42
;

data want;
  set have;
  /* This code doesn't take into account whether or not adding 1 to the result. */
  LOS = datepart(ADT_OUT) - datepart(ADT_IN);
run;

 

View solution in original post


All Replies
Super Contributor
Posts: 324

Computation using SAS

Posted in reply to desireatem


Hello-

I wish to compute length of stay for each observation by subtracting ADT_IN from ADR_OUT.

 

Please help me with the code 

 

data;

input ADT_IN ADT_OUT;

 

Cards;

2/9/2017 19:50   2/10/2017 14:21

 2/8/2017 17:50  2/10/2017 12:40

;

run;

 

Each that has an associated time to it.

Super User
Posts: 23,237

Re: Computation using SAS

[ Edited ]
Posted in reply to desireatem

I've merged your two posts since they're the same. What have you tried so far? Post any code that you've attempted to date. 

Hint, try INTCK and/or direct subtraction.

 

There's a tutorial on dates and times here:

https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/...

 

Edit: Unfortunately your data step appears incorrect, instructions on posting data is here.

 

71 data;
72 input ADT_IN ADT_OUT;
73 Cards;
 
NOTE: Invalid data for ADT_IN in line 74 1-8.
NOTE: Invalid data for ADT_OUT in line 74 10-14.
RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0
74 2/9/2017 19:50 2/10/2017 14:21
ADT_IN=. ADT_OUT=. _ERROR_=1 _N_=1
NOTE: Invalid data for ADT_IN in line 75 1-8.
NOTE: Invalid data for ADT_OUT in line 75 10-14.
75 2/8/2017 17:50 2/10/2017 12:40
ADT_IN=. ADT_OUT=. _ERROR_=1 _N_=2
Super Contributor
Posts: 324

Re: Computation using SAS

Thank you. I didnt meant to post it twice. The first was showing error messages when pasted

Solution
2 weeks ago
Occasional Contributor
Posts: 6

Re: Computation using SAS

[ Edited ]
Posted in reply to desireatem

 

 

data have;
input (ADT_IN ADT_OUT)(:mdyampm. &);
format ADT_IN ADT_OUT datetime.;
cards;
2/9/2017 19:50 2/10/2017 14:21
2/8/2017 17:50 2/10/2017 12:40
2/7/2017 11:43 2/9/2017 12:50
2/8/2017 12:56 2/11/2017 0:42
2/8/2017 12:56 2/11/2017 0:42
2/8/2017 12:56 2/11/2017 0:42
2/8/2017 12:56 2/11/2017 0:42
2/8/2017 12:56 2/11/2017 0:42
2/8/2017 12:56 2/11/2017 0:42
;

data want;
  set have;
  /* This code doesn't take into account whether or not adding 1 to the result. */
  LOS = datepart(ADT_OUT) - datepart(ADT_IN);
run;

 

Super Contributor
Posts: 324

Re: Computation using SAS

Thank you!

Occasional Contributor
Posts: 6

Re: Computation using SAS

[ Edited ]
Posted in reply to desireatem

I edited and added a information in my initial post.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 198 views
  • 1 like
  • 3 in conversation