Taylrl
Taylrl

Reputation: 3919

Creating a flag for first and last in SAS

I want to be able to create a flag, here called timeflag, that is set to 1 for every first and last entry of a certain Session, designated by logflag. What I have is the following but this gives me null data points:

data OUT.TENMAY_TIMEFLAG;
   set IN.TENMAY_LOGFLAG;
   if first.logflag then timeflag = 1;
   if last.logflag then timeflag = 1;
run;

What is it about the first. and last. functions that I am not understanding here or is it that I have 2 if statements?

Upvotes: 1

Views: 789

Answers (2)

Tom
Tom

Reputation: 51566

To have SAS create FIRST. and LAST. automatic variables you need to use a BY statement. If you want the new variable to be coded 1/0 then no need for the IF statement, just assign the automatic variable to a new permanent variable. To make one variable that is 1 for the first and the last then just use an OR.

data want;
  set have;
  by logflag ;
  timeflag = first.logflag or last.logflag ;
run;

Upvotes: 2

andrey_sz
andrey_sz

Reputation: 751

data OUT.TENMAY_TIMEFLAG;
   set IN.TENMAY_LOGFLAG;
   by logflag;
   if first.logflag then timeflag = 1;
   if last.logflag then timeflag = 1;
run;

P.S. in this case the dataset IN.TENMAY_LOGFLAG should be sorted by logflag.

Upvotes: 1

Related Questions