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

Hello,

 

I have a account list with value at different time from T1 to T6. I am trying to flag the record when any of 3 consecutive time records have value under 5:

I know I can check if (T1<5 and T2<5 and T3<5 ) or (T2<5 and T3<5 and T4<5) or .... under four combinations

I wonder whether there is any smart and advance way to code this? 

 

Thank you,

 

 

AcctT1T2T3T4T5T6
a444333
b1064448
c999221
d888765
e429332
1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

If you contemplate looking for series of numbers substantially longer than 3 time points, you might want to forego the syntax of 

 

  x{i}<5 and x{i+1}<5 and ...

 

and try using the MAX function:

 

data want;
    set have;
    array t t1-t6;
    flag=0;
    do i=1 to dim(t)-2 until (flag=1);
        flag=(max(t{i},t{i+1},t{i+2})<5);
    end;
    drop i;
run;

Also the "until (flag=1)" expression stops the looping if a qualifying series if found in an early iteration.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

UNTESTED CODE

 

data want;
    set have;
    array t t1-t6;
    flag=0;
    do i=1 to dim(t)-2;
        if t(i)<5 and t(i+1)<5 and t(i+2)<5 then flag=1;
    end;
    drop i;
run;

 

 

If you want tested code, do not provide data as screen captures. Provide data as working SAS data step code (instructions), or by typing it in yourself.

--
Paige Miller
mkeintz
PROC Star

If you contemplate looking for series of numbers substantially longer than 3 time points, you might want to forego the syntax of 

 

  x{i}<5 and x{i+1}<5 and ...

 

and try using the MAX function:

 

data want;
    set have;
    array t t1-t6;
    flag=0;
    do i=1 to dim(t)-2 until (flag=1);
        flag=(max(t{i},t{i+1},t{i+2})<5);
    end;
    drop i;
run;

Also the "until (flag=1)" expression stops the looping if a qualifying series if found in an early iteration.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

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!

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