<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to flag the last AVALC = 'Y' record prior to this AVALC='N' record in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921220#M362778</link>
    <description>&lt;P&gt;I've edited this response to include 3 scenarios:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Every instance of last Y preceding an N&lt;/LI&gt;
&lt;LI&gt;First instance.&lt;/LI&gt;
&lt;LI&gt;Last instance.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm a great fan of the capabilities of multiple SET statements, but I see this task as a single SET situation.&amp;nbsp;&amp;nbsp;If what you really want is to flag &lt;EM&gt;&lt;STRONG&gt;EVERY instance&lt;/STRONG&gt;&lt;/EM&gt; of the last Y to precede an N (3rd A, 5th A and 2nd B), then a single SET with a BY ... NOTSORTED does the trick:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 want;
  set have;
  by usubjid  avalc notsorted;
  if avalc='Y' and last.avalc=1 and last.usubjid=0 then flag=1;
  else flag=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now if your rule is to flag &lt;EM&gt;&lt;STRONG&gt;only the first&lt;/STRONG&gt;&lt;/EM&gt; qualifying observations&amp;nbsp;for each ID (i.e. 3rd A and 2nd B), very little needs to be changed:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set have;
  by usubjid  avalc notsorted;
  if first.usubjid=1 then _count=0;
  if avalc='Y' and last.avalc=1 and last.usubjid=0 AND _count=0 then flag=1;
  else flag=0;
  _count+flag;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Adding this 3rd scenario DOES require multiple sets:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set have; 
  by usubjid avalc notsorted;
  retain _last_y ;           /*Indicates Final instance of Y preceding an N */
  if avalc='Y' and last.avalc=1 and last.usubjid=0 then _last_y=_n_;

  if last.usubjid then do until (last.usubjid);
    set have;
    by usubjid;
    _n+1;
    if _n=_last_y then flag=1;
    else flag=0;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 21 Mar 2024 17:09:15 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2024-03-21T17:09:15Z</dc:date>
    <item>
      <title>How to flag the last AVALC = 'Y' record prior to this AVALC='N' record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921159#M362757</link>
      <description>&lt;P&gt;How to flag the last AVALC = 'Y' record prior to this AVALC='N' record. For example as below:&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to flag the third A record and second B record.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input usubjid $ ADY AVALC $;&lt;BR /&gt;datalines;&lt;BR /&gt;A 15 N&lt;BR /&gt;A 36 N&lt;BR /&gt;A 57 Y&lt;BR /&gt;A 60 N&lt;BR /&gt;A 61 Y&lt;BR /&gt;A 62 N&lt;BR /&gt;A 63 Y&lt;BR /&gt;A 64 Y&lt;BR /&gt;A 65 Y&lt;BR /&gt;B 15 Y&lt;BR /&gt;B 36 Y&lt;BR /&gt;B 57 N&lt;BR /&gt;B 58 N&lt;BR /&gt;B 60 Y&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Mar 2024 18:27:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921159#M362757</guid>
      <dc:creator>SerenaJJ</dc:creator>
      <dc:date>2024-03-20T18:27:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag the last AVALC = 'Y' record prior to this AVALC='N' record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921166#M362760</link>
      <description>&lt;P&gt;Why not flagging record 5? It is a Y prior to N.&lt;/P&gt;
&lt;P&gt;What is the rule the says that the N on the 4th record is the (only) N to consider for usubjid A? Examples without rules are incomplete.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for providing data in the data step.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Mar 2024 18:59:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921166#M362760</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-03-20T18:59:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag the last AVALC = 'Y' record prior to this AVALC='N' record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921185#M362766</link>
      <description>Can you advise as to why row 5 would not be flagged? It satisfies your criteria. I presume this is being done at a subject level? Please confirm if that is the case.&lt;BR /&gt;</description>
      <pubDate>Wed, 20 Mar 2024 20:45:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921185#M362766</guid>
      <dc:creator>Mazi</dc:creator>
      <dc:date>2024-03-20T20:45:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag the last AVALC = 'Y' record prior to this AVALC='N' record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921188#M362767</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/464649"&gt;@SerenaJJ&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A simple solution is to use a second set-statement to perform look-ahead.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first data step flags all occurences of a Y followed ny N within the same ID.&amp;nbsp; The next is a bit more complicated, because it is necessary to keep information of the flag having been set already.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=nextavalc nextusubjid);
  set have end=eof;
  by usubjid;
  if not eof then do;
    set have (firstobs=2 keep=avalc usubjid rename=(avalc=nextavalc usubjid=nextusubjid));
    if not eof and usubjid=nextusubjid and avalc='Y' and nextavalc='N' then Flag=1;
  end;
run; 

data want2 (drop=FlagSet nextavalc nextusubjid);
  set have end=eof;
  by usubjid;
  retain FlagSet;
  if first.usubjid then flagset = 0;
  if not eof then do;
    set have (firstobs=2 keep=avalc usubjid rename=(avalc=nextavalc usubjid=nextusubjid));
    if not eof and usubjid=nextusubjid and FlagSet=0 and avalc='Y' and nextavalc='N' then do;
      Flag=1;
      FlagSet=1;
    end;
  end;
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The conditional execution of the do-block is needed to get the last observation written.&lt;/P&gt;
&lt;P&gt;Rename of the variables from the second set statement prevents that&amp;nbsp; the values from the&amp;nbsp; first set statement is owerwritten in the program vector, so it is possible to compare the values from the corrent and the next observation.&lt;/P&gt;
&lt;P&gt;The comparison of the ID's prevents the last observation with value Y to be flagged if&amp;nbsp; the next observation has value N, but not the same ID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Mar 2024 21:00:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921188#M362767</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2024-03-20T21:00:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag the last AVALC = 'Y' record prior to this AVALC='N' record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921220#M362778</link>
      <description>&lt;P&gt;I've edited this response to include 3 scenarios:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Every instance of last Y preceding an N&lt;/LI&gt;
&lt;LI&gt;First instance.&lt;/LI&gt;
&lt;LI&gt;Last instance.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm a great fan of the capabilities of multiple SET statements, but I see this task as a single SET situation.&amp;nbsp;&amp;nbsp;If what you really want is to flag &lt;EM&gt;&lt;STRONG&gt;EVERY instance&lt;/STRONG&gt;&lt;/EM&gt; of the last Y to precede an N (3rd A, 5th A and 2nd B), then a single SET with a BY ... NOTSORTED does the trick:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 want;
  set have;
  by usubjid  avalc notsorted;
  if avalc='Y' and last.avalc=1 and last.usubjid=0 then flag=1;
  else flag=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now if your rule is to flag &lt;EM&gt;&lt;STRONG&gt;only the first&lt;/STRONG&gt;&lt;/EM&gt; qualifying observations&amp;nbsp;for each ID (i.e. 3rd A and 2nd B), very little needs to be changed:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set have;
  by usubjid  avalc notsorted;
  if first.usubjid=1 then _count=0;
  if avalc='Y' and last.avalc=1 and last.usubjid=0 AND _count=0 then flag=1;
  else flag=0;
  _count+flag;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Adding this 3rd scenario DOES require multiple sets:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set have; 
  by usubjid avalc notsorted;
  retain _last_y ;           /*Indicates Final instance of Y preceding an N */
  if avalc='Y' and last.avalc=1 and last.usubjid=0 then _last_y=_n_;

  if last.usubjid then do until (last.usubjid);
    set have;
    by usubjid;
    _n+1;
    if _n=_last_y then flag=1;
    else flag=0;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Mar 2024 17:09:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921220#M362778</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-03-21T17:09:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag the last AVALC = 'Y' record prior to this AVALC='N' record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921221#M362779</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; 

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 want;
 set have;
 by usubjid AVALC notsorted;
 retain found 0;
 if first.usubjid then found=0;
 if last.AVALC and AVALC='Y' and not found then do;found=1;flag=1;end;
 drop found;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Mar 2024 02:29:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921221#M362779</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-03-21T02:29:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag the last AVALC = 'Y' record prior to this AVALC='N' record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921246#M362790</link>
      <description>&lt;PRE&gt;data have;
input usubjid $ ADY AVALC $;
datalines;
A 15 Y
A 36 N
A 57 Y
A 60 N
A 61 Y
A 62 N
A 63 Y
A 64 N
A 65 Y
B 15 Y
B 36 Y
B 57 N
B 58 N
B 60 Y
;
run;

data want;
  do until(last.usubjid);
  	set have;
  	by usubjid;
	row+1;
 	if ^last.usubjid and avalc = 'Y' then do;
		next=row+1;
		set have (keep = avalc rename=(avalc = _avalc)) point=next;
		if _avalc = 'N' then last = ady;
	end;
  end;
  do until(last.usubjid);
  	set have;
  	by usubjid;
	if ady = last then flag = 'Y';
 	output;
	call missing(flag);
  end;
  call missing(last);
  drop _avalc row last;
run;&lt;/PRE&gt;&lt;P&gt;Can you try this?&lt;/P&gt;</description>
      <pubDate>Thu, 21 Mar 2024 05:47:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921246#M362790</guid>
      <dc:creator>Mazi</dc:creator>
      <dc:date>2024-03-21T05:47:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag the last AVALC = 'Y' record prior to this AVALC='N' record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921313#M362821</link>
      <description>&lt;P&gt;Thank you so much for the coding. It works!!&lt;/P&gt;</description>
      <pubDate>Thu, 21 Mar 2024 15:22:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921313#M362821</guid>
      <dc:creator>SerenaJJ</dc:creator>
      <dc:date>2024-03-21T15:22:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag the last AVALC = 'Y' record prior to this AVALC='N' record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921316#M362823</link>
      <description>Thank you so much for the coding. Based on the specification and the final dataset for QC, they have the third A and second B subjids to be flagged. Here I can get the fifth A and second B flagged. Anyway, really appreciate your help!!</description>
      <pubDate>Thu, 21 Mar 2024 15:29:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921316#M362823</guid>
      <dc:creator>SerenaJJ</dc:creator>
      <dc:date>2024-03-21T15:29:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag the last AVALC = 'Y' record prior to this AVALC='N' record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921320#M362827</link>
      <description>Thank you so much for your help! It works well!!</description>
      <pubDate>Thu, 21 Mar 2024 15:34:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921320#M362827</guid>
      <dc:creator>SerenaJJ</dc:creator>
      <dc:date>2024-03-21T15:34:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag the last AVALC = 'Y' record prior to this AVALC='N' record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921321#M362828</link>
      <description>Thank you so much for your suggestion. The specification is flagged the last AVALC = 'Y' record prior to this AVALC='N' record. If multiple records, then choose the first one. So for subjid level, only one record is kept.</description>
      <pubDate>Thu, 21 Mar 2024 15:37:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921321#M362828</guid>
      <dc:creator>SerenaJJ</dc:creator>
      <dc:date>2024-03-21T15:37:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag the last AVALC = 'Y' record prior to this AVALC='N' record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921322#M362829</link>
      <description>Thank you so much for your reply. The specification is flagged the last AVALC = 'Y' record prior to this AVALC='N' record. If multiple records, then choose the first one. So for subjid level, only one record is kept.</description>
      <pubDate>Thu, 21 Mar 2024 15:38:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921322#M362829</guid>
      <dc:creator>SerenaJJ</dc:creator>
      <dc:date>2024-03-21T15:38:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag the last AVALC = 'Y' record prior to this AVALC='N' record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921323#M362830</link>
      <description>Thank you so much for the solution! It is great!!</description>
      <pubDate>Thu, 21 Mar 2024 15:39:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921323#M362830</guid>
      <dc:creator>SerenaJJ</dc:creator>
      <dc:date>2024-03-21T15:39:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag the last AVALC = 'Y' record prior to this AVALC='N' record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921324#M362831</link>
      <description>Thank you so much for the solution!!!</description>
      <pubDate>Thu, 21 Mar 2024 15:41:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921324#M362831</guid>
      <dc:creator>SerenaJJ</dc:creator>
      <dc:date>2024-03-21T15:41:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag the last AVALC = 'Y' record prior to this AVALC='N' record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921342#M362843</link>
      <description>&lt;P&gt;Thank you so much for all the suggestion and silutions you share with me!!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Mar 2024 16:51:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-last-AVALC-Y-record-prior-to-this-AVALC-N-record/m-p/921342#M362843</guid>
      <dc:creator>SerenaJJ</dc:creator>
      <dc:date>2024-03-21T16:51:01Z</dc:date>
    </item>
  </channel>
</rss>

