Search code examples
sasdatastep

First and last function sas


I have some data which looks like this

data example1;     
   input Activity $ logflag;
   Activity1 1
   Activity2 1
   Activity3 1
   Activity4 1
   Activity1 2
   Activity2 2
   Activity3 2
   Activity1 3
   Activity2 3
   Activity3 3
   Activity4 3
   Activity1 4
   Activity2 4
   ; 
run;

Where basically the variable 'logflag' increments by 1 every time the 'Activity' returns to 'Activity1' however I want to get to this;

data example2;     
   input Activity $ logflag count;
   Activity1 1 1
   Activity2 1 2
   Activity3 1 3
   Activity4 1 4
   Activity1 2 1
   Activity2 2 2
   Activity3 2 3
   Activity1 3 1
   Activity2 3 2
   Activity3 3 3
   Activity4 3 4
   Activity1 4 1
   Activity2 4 2
   ;
run;

Whereby I have a 'count' which increments by 1 every time a new 'Activity' appears within a certain 'logflag'.

what I am using is this;

data AS2.TENMAY_EXAMPLE4;
  set AS2.TENMAY_SESSIONID;
  by logflag Activity notsorted;
  if first.logflag then count=0;
  if first.Activity then count+1;
run;

and I am getting this

data example2;     
   input Activity $ logflag count;
   Activity1 1 1
   Activity2 1 2
   Activity3 1 2
   Activity4 1 2
   Activity1 2 1
   Activity2 2 2
   Activity3 2 2
   Activity1 3 1
   Activity2 3 2
   Activity3 3 2
   Activity4 3 2
   Activity1 4 1
   Activity2 4 2
   ;
run;

What I cant understand is why the counter increments by 1 then goes to 2 but then never gets to 3 or higher. I am sure I had this working before but I can't work out what I have changed.

Would anyone be able to help with this?

Thanks,


Solution

  • You must already have a variable named COUNT in the input dataset. So each time the SET statement runs the value from the input dataset overwrites the value from the previous observation.

    To get your example then COUNT is probably 1 for every observation. So that when you increment when ACTIVITY changes it goes to 2. The value for the first observation per LOGFLAG group is 1 since you first set it to 0 before incrementing it.