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

I need to create a variable that gives me the "end" time of an event. Specifically, I need to create a "phase 1"  variable that is based on the variables measurement 1-4 and datetime 1-4. To do this, I need to extract the datetime associated with the last measurement <= 5.

For example, my phase_1 variable for Obs #2 would be 22JAN19:13:33:00.

 

Here's an example of what my data look like: 

 

obsdatetime_1measurement_1datetime_2measurement_2datetime_3measurement_3datetime_4measurement_4
117APR19:22:56:00318APR19:05:05:00418APR19:06:35:00418APR19:07:04:005
222JAN19:11:40:00422JAN19:13:33:00522JAN19:15:29:00922JAN19:16:25:0010
303MAY15:19:37:00004MAY15:04:30:00304MAY15:07:22:00604MAY15:10:53:008
418FEB20:01:14:00120FEB20:03:05:00120FEB20:04:17:00420FEB20:11:00:007
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Before we go too far into this do you ever have more than 4 grouped variables?

Are your measure values monotonic (always increasing, or at least not decreasing), the datetimes as well?

Do have a need for a phase_2 or phase_3? Different boundaries other than 5?

An approach that works for a single number of variables with one boundary may not be flexible enough to extend to a more complex case.

 

Untested but this should get you started:

data want;
   set have;
   array d (*) datetime: ;
   array m (*) measure: ;
   do i=dim(m) to 1 by -1;
      if m[i] le 5 then do; 
         phase_1= d[i];
         leave;
      end;
   end;
run;
      

The arrays start at the highest indexed value assuming it corresponds to the latest datetime value. The first value less than or equal 5 pulls the corresponding date time. The LEAVE statement says to quit executing the loop the first time a value meeting the condition is found.

If none of the values are less than or equal to 5 no value is assigned to phase_1.

View solution in original post

4 REPLIES 4
ballardw
Super User

Before we go too far into this do you ever have more than 4 grouped variables?

Are your measure values monotonic (always increasing, or at least not decreasing), the datetimes as well?

Do have a need for a phase_2 or phase_3? Different boundaries other than 5?

An approach that works for a single number of variables with one boundary may not be flexible enough to extend to a more complex case.

 

Untested but this should get you started:

data want;
   set have;
   array d (*) datetime: ;
   array m (*) measure: ;
   do i=dim(m) to 1 by -1;
      if m[i] le 5 then do; 
         phase_1= d[i];
         leave;
      end;
   end;
run;
      

The arrays start at the highest indexed value assuming it corresponds to the latest datetime value. The first value less than or equal 5 pulls the corresponding date time. The LEAVE statement says to quit executing the loop the first time a value meeting the condition is found.

If none of the values are less than or equal to 5 no value is assigned to phase_1.

mjalvarez
Calcite | Level 5
Thank you for your response and help! To answer your questions -

Yes, I do have more than 4 grouped variables. I have 20 in total.

All measurement and date variables are monotonic, always increasing.

Right now, I don't need a phase_2 or phase_3.


mjalvarez
Calcite | Level 5

I get a blank (.) value for phase_1 using this code -

data want;
set have;

array d (*) exam_date: ;
array m (*) exam_measure: ;
do i=dim(m) to 1 by -1;
if m[i] le 5 then do;
phase_1= d[i];
leave;
end;
end;
run;

 

 

ballardw
Super User

@mjalvarez wrote:

I get a blank (.) value for phase_1 using this code -

data want;
set have;

array d (*) exam_date: ;
array m (*) exam_measure: ;
do i=dim(m) to 1 by -1;
if m[i] le 5 then do;
phase_1= d[i];
leave;
end;
end;
run;

 

 


And the contents of the data set have is what? As I mentioned my code is untested because you did not provide an actual data set and you need to show which values actually create missing. I did say if none of the measure values are less than or equal to 5 then the phase variable will not be assigned.

 

Since your code above also uses different variable names then were implied by your example data that is something else to look at: the spelling of the variables.

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
  • 4 replies
  • 672 views
  • 0 likes
  • 2 in conversation