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

Hi @mkeintz@PGStats@Reeza@FreelanceReinh@novinosrin 

I have a data set that contains the patients' dosing data. Every patient is supposed to take 2 doses daily every 12 hours (+-30 minutes). I need to calculate the difference between the time (per date per patient) and check which patients have or have not taken their doses correctly. 

The data set has only the following two variables: PT, Dose Time.

e.g., 

30SEP2021:22:31:001
30SEP2021:10:30:001
29SEP2021:22:28:001
29SEP2021:10:30:001
28SEP2021:22:30:001
28SEP2021:10:30:001
27SEP2021:22:30:002
27SEP2021:10:28:002
26SEP2021:22:30:002
26SEP2021:10:30:002
25SEP2021:22:30:003
25SEP2021:10:30:003
24SEP2021:22:30:003
24SEP2021:11:30:003
03OCT2021:21:42:003
03OCT2021:10:34:003

Thank you so much and appreciate your help.

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
/* UNTESTED CODE */
data want;
    set have;
    by pt;
    delta_time=dose_time - lag(dose_time);
    if first.pt then delta_time=.;
    format delta_time time.;
run;

This assumes the data is properly sorted by PT and by dose_time. It also assumes that dose_time is numeric and not character.

 

If you want tested code, do not provide data as screen captures. Provide data using these instructions.

--
Paige Miller

View solution in original post

13 REPLIES 13
PaigeMiller
Diamond | Level 26
/* UNTESTED CODE */
data want;
    set have;
    by pt;
    delta_time=dose_time - lag(dose_time);
    if first.pt then delta_time=.;
    format delta_time time.;
run;

This assumes the data is properly sorted by PT and by dose_time. It also assumes that dose_time is numeric and not character.

 

If you want tested code, do not provide data as screen captures. Provide data using these instructions.

--
Paige Miller
SAS_USER_928
Obsidian | Level 7

Thank you so much @PaigeMiller. This did work. I just updated the following line to get the number of hours between two doses (I believe the prior result was in seconds?).

delta_time=dose_time - lag(dose_time)/3600; 

 

Note: The dose dates were in DateTime format.

 

Thank you so much again for your prompt response and solution. 

PaigeMiller
Diamond | Level 26

@SAS_USER_928 

 

I believe you want

delta_time=(dose_time - lag(dose_time))/3600; 

but since my original solution was formatted with the time. format, I don't see any benefit of doing this division.

--
Paige Miller
SAS_USER_928
Obsidian | Level 7

Hi @PaigeMiller ,

 

That's correct. Sorry I probably deleted additional parenthesis while updating the original variables here for public use.

 

DELTA_TIME= ROUND((DOSE_DT_TIME- LAG(DOSE_DT_TIME))/3600, 0.01);

 

The original answer had values like 45000 etc. in the DELTA_TIME variable which I did not want. That's the reason I divided by 3600 to get something like 12(.35) (hours).

 

Thank you

PaigeMiller
Diamond | Level 26

Yes, I get that, but using the TIME. format essentially displays the result as (for example) 12:05:14

--
Paige Miller
SAS_USER_928
Obsidian | Level 7

Ah that TIME. formatted variable is perfect; I want it but cannot use to calculate/apply Mean statistics on it. That's why I created a new variable and converted to the numeric number of hours as well.

SAS_USER_928
Obsidian | Level 7

OK now I need a little more help. The new variable that I created (that holds the number of hours) is not correct. Can you please help to use your TIME. formatted variable to calculate the number of hours (as number variable) so that I can apply Mean on it?

 

Thank you

PaigeMiller
Diamond | Level 26

@SAS_USER_928 wrote:

Ah that TIME. formatted variable is perfect; I want it but cannot use to calculate/apply Mean statistics on it. That's why I created a new variable and converted to the numeric number of hours as well.


Incorrect. You can calculate means on it just as you can calculate means on any other number.

 

OK now I need a little more help. The new variable that I created (that holds the number of hours) is not correct. Can you please help to use your TIME. formatted variable to calculate the number of hours (as number variable) so that I can apply Mean on it?

PROC MEANS will do it. It works just fine on formatted variables.

--
Paige Miller
SAS_USER_928
Obsidian | Level 7

Hi @PaigeMiller ,

 

No not in SAS application. I create a data set which I then import to a third party application (part of my job). They provide their own set of tests based on the types of variables. Mean was disabled for this character TIME formatted variable. That's the reason I need a numeric variable to hold the number of hours (as number and not as time).

 

Any help in the matter will be greatly appreciated. I am trying to get it to work but failing miserably.

PaigeMiller
Diamond | Level 26

Then use the formula for hours I provided earlier.

--
Paige Miller
SAS_USER_928
Obsidian | Level 7

Hi @PaigeMiller,

 

This time I have a different situation and thought if you could please help... using SAS or Proc SQL.

 

I have a list of pts. that have to complete a lab every N (say between 4 to 5) weeks and outside of this window will be considered NON COMPLIANT.

 

Here is the list of PTs and their lab dates:

PTDateDuration in Weeks
11/5/2021 
12/24/2021 
14/7/2021 
15/19/2021 
21/14/2021 
23/2/2021 
32/25/2022 

 

My data is sorted by PT and Date in ascending order.  For PT1, I need to take the difference between every two labs and calculate the difference (duration) in weeks. If there is only 1 lab performed so far (as is the case with PT3) then use today's date to calculate the duration in weeks.

 

Thanking you in advance for your help. 

PaigeMiller
Diamond | Level 26
data want;
    set have;
    by pt;
    prev_date=lag(date);
    if first.pt then prev_date=.;
    duration_in_weeks = (date-prev_date)/7;
run;
--
Paige Miller
SAS_USER_928
Obsidian | Level 7

Thank you @PaigeMiller. There is only 1 scenario left out. See PT3 which has only one observation and I do not want to exclude that and want the Duration to be calculated as Extraction Date - (Lab) Date to see if the lab is past-due.

 

Note: Extraction Date is hard-code as 2022-03-01.

 

Thank you

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 13 replies
  • 1882 views
  • 1 like
  • 2 in conversation