Taylrl
Taylrl

Reputation: 3919

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,

Upvotes: 0

Views: 501

Answers (3)

Tom
Tom

Reputation: 51611

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.

Upvotes: 1

andrey_sz
andrey_sz

Reputation: 751

Input data:

data example1;
   attrib Activity format = $20.
         logflag  format = 8.;
   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;

To take the results what you want try this:

data example2;
   set example1;
   by logflag Activity notsorted;
   if first.logflag then count=1;
   else count+1;
run;

The result is:

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

enter image description here

Upvotes: 0

Jetzler
Jetzler

Reputation: 797

You are missing the retain statement. Every time the datastep loops the variable is reset. Retain suppresses this behavior.

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

Upvotes: 0

Related Questions