I have this raw data, actually , need create a new var, named flag. Only give flag a value when each AVALC=Y and next AVALC=N
data have;
input usubjid $ ADY AVALC $;
datalines;
A 15 N
A 36 N
A 57 Y
A 60 N
A 61 Y
A 62 N
A 63 Y
A 64 Y
A 65 Y
B 15 Y
B 36 Y
B 57 N
B 58 N
B 60 Y
; run;
output like below
usubjid $ ADY  AVALC  FLAG $;
A 15 N       .
A 36 N       .
A 57 Y CHECK
A 60 N    .
A 61 Y CHECK
A 62 N   .
A 63 Y   .
A 64 Y  . 
A 65 Y  .
B 15 Y   .
B 36 Y CHECK
B 57 N  . 
B 58 N  .
B 60 Y  .
; run;
I try this code below, the first code, it marked the N, with flat=o, then i want to give the flag before it the value'check' but faile.
DATA FLAG;
SET HAVE;
BY usubjid;
length flag $6.;
retain flag;
if first.usubjid then flag = .;
IF AVALC = 'N' and lag(AVALC) = 'Y' then FLAG='o';
ELSE FLAG=' ';
RUN;
data flag1;
set flag;
BY usubjid;
if flag='o' then lag(flag)='check';
run;
proc sort data=have;
	by usubjid descending ady;
run;
DATA FLAG;
	SET HAVE;
	BY usubjid;
	length flag $6.;
	if first.usubjid then flag = ' ';
	else IF  AVALC = 'Y' and lag(AVALC) = 'N' then FLAG='check';
RUN;
					
				
			
			
				data have;
input usubjid $ ADY AVALC $;
datalines;
A 15 N
A 36 N
A 57 Y
A 60 N
A 61 Y
A 62 N
A 63 Y
A 64 Y
A 65 Y
B 15 Y
B 36 Y
B 57 N
B 58 N
B 60 Y
; run;
data _null_;
  call symputX("N",nobs,"G");
  stop;
  set have nobs=nobs;
run;
data want;
array test[&n.] $ 1 _temporary_;
do _N_=1 to &n.;
  set have curobs=curobs;
  test[curobs]=AVALC;
end;
do _N_=1 to &n.;
  set have curobs=curobs;
  by usubjid;
  if not last.usubjid then
    do;
      if AVALC="Y" and test[curobs+1]="N" then flag="CHECK";
      else flag=" ";
    end;
  else flag=" ";
  output;
end;
stop;
run;
					
				
			
			
				data want;
  merge 
    have 
    have(firstobs=2 
         keep=usubjid AVALC 
         rename=(AVALC=NEXT_AVALC usubjid=NEXT_usubjid)
        );
  if usubjid=NEXT_usubjid 
     and
     AVALC="Y" and NEXT_AVALC="N" then flag="CHECK";
  drop NEXT_:;
run;
					
				
			
			
				It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.
Find more tutorials on the SAS Users YouTube channel.