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

Hi all,

 

I have difficulty designing my program.

 

In my data set, I have hundreds of patients.

Each patient is recorded with his/her blood pressure, and these records are ordered by hour.

 

What I am trying to do is that if the the difference between starting time and current time is greater than 24 hour, then record should switch to another day.

 

It's a bit too abstract, so allow me to present an example:

OBS    ID    SBP    Hour

 1          1      115       15

 2          1      113       17

 3          1      121       23

 4          1      120        0

 5          1      112        5

 6          1      108       13

 7          1      110       15

 8          1      113       16

 9          1      115       17

10         1      113       18

11         1      121       23

12        1      120        0

13        1      112        5

14        1      108       13

15        1      110       15

16        1      113       16

 

I want to add a variable "Day", and the value is

1, for OBS=1 to 6

2, for OBS=7 to 14 (Since time at 7th-OBS equals time at 1st-OBS, it means this monitoring device has started over 24 hour, so the variable "Day" should added one) 

3, for OBS=15 16 (Since it's the second time that the time at nth-OBS equals the time at 1st-OBS, it means this monitoring device has started over 24*2 hour, so the variable "Day" should added two)

 

Regarding this question, on one hand, I post it here hope someone can save me from the mud.

On the other hand, I'm searching information about "do-loop in SAS".

 

If anyone has any idea, I'll appreciate it.

Thanks in advance! 

 

Best,

Bill

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

Sometimes, especially when data comes from a medical device like here, there is no choice on what you get.

Like this?


data WANT;
  retain START_HOUR MIDNIGHT_PASSED;
  drop   START_HOUR MIDNIGHT_PASSED;
  set HAVE;
  by ID;
  if first.ID then do;
    START_HOUR=HOUR;
    DAY=1;        
    MIDNIGHT_PASSED=0;
  end;
  if HOUR<lag(HOUR) then MIDNIGHT_PASSED=1;
  if MIDNIGHT_PASSED & HOUR>=START_HOUR then do;
    DAY+1;
    MIDNIGHT_PASSED=0;
  end;
run;

SAS Output

OBS ID SBP Hour DAY
1 1 115 15 1
2 1 113 17 1
3 1 121 23 1
4 1 120 0 1
5 1 112 5 1
6 1 108 13 1
7 1 110 15 2
8 1 113 16 2
9 1 115 17 2
10 1 113 18 2
11 1 121 23 2
12 1 120 0 2
13 1 112 5 2
14 1 108 13 2
15 1 110 15 3
16 1 113 16 3

 

View solution in original post

8 REPLIES 8
Kurt_Bremser
Super User

Step 1: convert your time values to real SAS datetime values, then you can use the power of the date/datetime interval functions intnx() and intck().

Well-designed programs are the result of well-designed data.

So go back to the source and retrieve the actual timestamps of the measurements.

Chung-Li
Quartz | Level 8

Kurt Bremser:

 

Thank you for your advice, I'll check it!

RW9
Diamond | Level 26 RW9
Diamond | Level 26

I would agree with @Kurt_Bremser there on datetimes etc.

I would also suggest that you should look at SDTM models provided by CDISC.  These are pretty standard across the pharma industry, and if you look at the Phuse code repository you might find some code ready to work with them.  The data you present here is not sufficient for task in hand, what does your import agreement state for structure?

Chung-Li
Quartz | Level 8

RW9:

 

Thank you for your advice, I'll try this later!

Astounding
PROC Star

Questions:

 

Why does observation 9 not indicate a reset and a new day?

 

What if observation 7 was just not part of the data?  The hour would skip from 13 to 16, going past the original starting point of 15.  Wouldn't that also indicate a new day?

Chung-Li
Quartz | Level 8

Astounding:

 

Sorry for misleading.
That's typo, and I've changed it.

ChrisNZ
Tourmaline | Level 20

Sometimes, especially when data comes from a medical device like here, there is no choice on what you get.

Like this?


data WANT;
  retain START_HOUR MIDNIGHT_PASSED;
  drop   START_HOUR MIDNIGHT_PASSED;
  set HAVE;
  by ID;
  if first.ID then do;
    START_HOUR=HOUR;
    DAY=1;        
    MIDNIGHT_PASSED=0;
  end;
  if HOUR<lag(HOUR) then MIDNIGHT_PASSED=1;
  if MIDNIGHT_PASSED & HOUR>=START_HOUR then do;
    DAY+1;
    MIDNIGHT_PASSED=0;
  end;
run;

SAS Output

OBS ID SBP Hour DAY
1 1 115 15 1
2 1 113 17 1
3 1 121 23 1
4 1 120 0 1
5 1 112 5 1
6 1 108 13 1
7 1 110 15 2
8 1 113 16 2
9 1 115 17 2
10 1 113 18 2
11 1 121 23 2
12 1 120 0 2
13 1 112 5 2
14 1 108 13 2
15 1 110 15 3
16 1 113 16 3

 

Chung-Li
Quartz | Level 8

ChrisNZ:

 

It's glad to have someone who feels the same way about the data structure (in the medical field)!

Sometimes you just have to adapt to the data, instead of asking data to be the way you want.

Thank you for your help, really!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1184 views
  • 0 likes
  • 5 in conversation