BookmarkSubscribeRSS Feed
lindamtl
Fluorite | Level 6

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;

  

4 REPLIES 4
LinusH
Tourmaline | Level 20
Please share the log.
Also, I see you are mixing numerical and character missing calue for your flag variable, stick to char.
Then you shoudl only do lag() when it's not first.usubjid, otherswise it will cross BY groups.
Data never sleeps
LinusH
Tourmaline | Level 20
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 never sleeps
yabwon
Onyx | Level 15
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;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



yabwon
Onyx | Level 15
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;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



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!

What is ANOVA?

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.

Discussion stats
  • 4 replies
  • 558 views
  • 2 likes
  • 3 in conversation