BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cmemtsa
Quartz | Level 8
data T;
input ID L EXP;
Datalines;
A1 1 10
A1 1 10
A1 2 20
A1 2 20 A1 4 20 A1 4 20 A1 5 20 A2 1 10 A2 2 60 A2 2 60 A2 3 60 A2 3 60 A2 5 50
A2 7 50 A3 2 60 A3 4 60 ;
 data want;
do until (last.id);
  set T;
  by id;

  if l <= 1 then flag = 1;
end;

put flag=;

do until(last.id);
  set T;
  by id;
  if not first.id and lag(l) ne l - 1  then flag = 0;
  if not flag then exp = 0;
  output;
end;
drop flag;
run;

Hi,

On this dataset (already sorted by ID, L) , I want to update EXP to 0 in these cases:

              1. The First.L per ID is not 1 (and all the subsequent to it)

              2. The first time the sequence of L is broken per ID and all the subsequent to it. (there are repetitions of L)

i.e the output should be 

A1 1 10
A1 1 10
A1 2 20

A1 2 20

A1 4 0
A1 4 0
A1 5 0
A2 1 10
A2 2 60
A2 2 60
A2 3 60
A2 3 60
A2 5 0

A2 7 0
A3 2 0
A3 4 0

 

The above code does not properly work in this case.

How should be amended?

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

Please explain what "The first time the sequence of L is broken per ID"  means.

View solution in original post

4 REPLIES 4
andreas_lds
Jade | Level 19

Please explain what "The first time the sequence of L is broken per ID"  means.

cmemtsa
Quartz | Level 8
e.g. in A1 we have L with sequence 1,1,2, 2, (which is normal: could be also 1,2,2) and then goes to 4. All EXP should be set to 0 for L=4 and all the subsequent (per ID).
andreas_lds
Jade | Level 19

Maybe this solves it, i renamed "L" to increase readability.

data want;
   set have;
   by ID;

   retain reset;

   LastLorax = lag(Lorax);

   if first.Id then do;
      LastLorax = .;
      reset = 0;

      if Lorax ^= 1 then do;         
         reset = 1;
      end;
   end;
   else do;
      if Lorax - LastLorax > 1 or Lorax = 4 then do;
         reset = 1;
      end;
   end;

   if reset then do;
      exp = 0;
   end;

   keep ID Lorax Exp;
run;
cmemtsa
Quartz | Level 8
It works for this samples dataset with taking out the (or Lorax=4) condition.
I'm testing it in other samples as well, see how it responds.
Thanks.

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