<?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: Flag first record if there 5 consecutive same values in by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Flag-first-record-if-there-5-consecutive-same-values-in-by-group/m-p/920301#M362459</link>
    <description>Thanks for your reply! I have one more situtation, if we have multiple consecutive 5 2's in a same group of parcat1 and avisit and usubjid then we need to flag only first record instead of flagging every first record in each consecutive group. How can we do that, I can provide example data if I am confusing.</description>
    <pubDate>Thu, 14 Mar 2024 15:29:03 GMT</pubDate>
    <dc:creator>chinna0369</dc:creator>
    <dc:date>2024-03-14T15:29:03Z</dc:date>
    <item>
      <title>Flag first record if there 5 consecutive same values in by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-first-record-if-there-5-consecutive-same-values-in-by-group/m-p/920034#M362370</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have provided the data below. If there are five consecutive 2's in aval after sorting by usubjid parcat1 avisit paramcd then we need to flag first record from that group as Y. Can you please help me with this logic.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;
input avisit $1-9	parcat1 $10-16	USUBJID	$17-25 paramcd $26 aval 28;
datalines;
Baseline	SOCIAL	10007001 A	2
Baseline	SOCIAL	10007001 B 0
Baseline	SOCIAL	10007001 C 2
Baseline	SOCIAL	10007001 D	2
Baseline	SOCIAL	10007001 E	2
Baseline	SOCIAL	10007001 F	2
Baseline	SOCIAL	10007001 G	2
Baseline	SOCIAL	10007001 H	2
Baseline	SOCIAL	10007001 I	0
Baseline	SOCIAL	10007001 J	1
Baseline	SOCIAL	10007001 K	1
Baseline	SOCIAL	10007001 L	2
Baseline	SOCIAL	10007001 M	1
Baseline	SOCIAL	10007001 N	1
Baseline	SOCIAL	10007001 O	1
Baseline	SOCIAL	10007001 P	1
Baseline	SOCIAL	10007001 Q	0
Baseline	SOCIAL	10007001 R	0
Baseline	SOCIAL	10007001 S	0
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data want;
input avisit $1-9	parcat1 $10-16	USUBJID	$17-25 paramcd $26 aval 28 flag $30;
datalines;
Baseline	SOCIAL	10007001 A	2 
Baseline	SOCIAL	10007001 B 0 
Baseline	SOCIAL	10007001 C 2 Y
Baseline	SOCIAL	10007001 D	2 
Baseline	SOCIAL	10007001 E	2
Baseline	SOCIAL	10007001 F	2
Baseline	SOCIAL	10007001 G	2
Baseline	SOCIAL	10007001 H	2
Baseline	SOCIAL	10007001 I	0
Baseline	SOCIAL	10007001 J	1
Baseline	SOCIAL	10007001 K	1
Baseline	SOCIAL	10007001 L	2
Baseline	SOCIAL	10007001 M	1
Baseline	SOCIAL	10007001 N	1
Baseline	SOCIAL	10007001 O	1
Baseline	SOCIAL	10007001 P	1
Baseline	SOCIAL	10007001 Q	0
Baseline	SOCIAL	10007001 R	0
Baseline	SOCIAL	10007001 S	0
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Mar 2024 22:19:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-first-record-if-there-5-consecutive-same-values-in-by-group/m-p/920034#M362370</guid>
      <dc:creator>chinna0369</dc:creator>
      <dc:date>2024-03-12T22:19:07Z</dc:date>
    </item>
    <item>
      <title>Re: Flag first record if there 5 consecutive same values in by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-first-record-if-there-5-consecutive-same-values-in-by-group/m-p/920038#M362374</link>
      <description>&lt;P&gt;I assume you mean you want to sort by PARAMCD but analyze by AVAL.&amp;nbsp; If so you need to use the NOTSORTED keyword on the BY statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You should be able to just use two DO loops.&amp;nbsp; One to count and the second to re-read and create the new FLAG variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input avisit :$9. parcat1 $ USUBJID $ paramcd $ aval ;
datalines;
Baseline SOCIAL 10007001 A 2
Baseline SOCIAL 10007001 B 0
Baseline SOCIAL 10007001 C 2
Baseline SOCIAL 10007001 D 2
Baseline SOCIAL 10007001 E 2
Baseline SOCIAL 10007001 F 2
Baseline SOCIAL 10007001 G 2
Baseline SOCIAL 10007001 H 2
Baseline SOCIAL 10007001 I 0
Baseline SOCIAL 10007001 J 1
Baseline SOCIAL 10007001 K 1
Baseline SOCIAL 10007001 L 2
Baseline SOCIAL 10007001 M 1
Baseline SOCIAL 10007001 N 1
Baseline SOCIAL 10007001 O 1
Baseline SOCIAL 10007001 P 1
Baseline SOCIAL 10007001 Q 0
Baseline SOCIAL 10007001 R 0
Baseline SOCIAL 10007001 S 0
;

data want ;
  do n=1 by 1 until(last.aval);
     set have;
     by avisit parcat1 USUBJID aval notsorted;
  end;
  do until(last.aval);
     set have;
     by avisit parcat1 USUBJID aval notsorted;
     if first.aval and n&amp;gt;4 and aval=2 then flag='Y';
     else flag='N';
     output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Obs    n     avisit     parcat1    USUBJID     paramcd    aval    flag

  1    1    Baseline    SOCIAL     10007001       A         2      N
  2    1    Baseline    SOCIAL     10007001       B         0      N
  3    6    Baseline    SOCIAL     10007001       C         2      Y
  4    6    Baseline    SOCIAL     10007001       D         2      N
  5    6    Baseline    SOCIAL     10007001       E         2      N
  6    6    Baseline    SOCIAL     10007001       F         2      N
  7    6    Baseline    SOCIAL     10007001       G         2      N
  8    6    Baseline    SOCIAL     10007001       H         2      N
  9    1    Baseline    SOCIAL     10007001       I         0      N
 10    2    Baseline    SOCIAL     10007001       J         1      N
 11    2    Baseline    SOCIAL     10007001       K         1      N
 12    1    Baseline    SOCIAL     10007001       L         2      N
 13    4    Baseline    SOCIAL     10007001       M         1      N
 14    4    Baseline    SOCIAL     10007001       N         1      N
 15    4    Baseline    SOCIAL     10007001       O         1      N
 16    4    Baseline    SOCIAL     10007001       P         1      N
 17    3    Baseline    SOCIAL     10007001       Q         0      N
 18    3    Baseline    SOCIAL     10007001       R         0      N
 19    3    Baseline    SOCIAL     10007001       S         0      N
&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Mar 2024 23:01:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-first-record-if-there-5-consecutive-same-values-in-by-group/m-p/920038#M362374</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-03-12T23:01:06Z</dc:date>
    </item>
    <item>
      <title>Re: Flag first record if there 5 consecutive same values in by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-first-record-if-there-5-consecutive-same-values-in-by-group/m-p/920301#M362459</link>
      <description>Thanks for your reply! I have one more situtation, if we have multiple consecutive 5 2's in a same group of parcat1 and avisit and usubjid then we need to flag only first record instead of flagging every first record in each consecutive group. How can we do that, I can provide example data if I am confusing.</description>
      <pubDate>Thu, 14 Mar 2024 15:29:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-first-record-if-there-5-consecutive-same-values-in-by-group/m-p/920301#M362459</guid>
      <dc:creator>chinna0369</dc:creator>
      <dc:date>2024-03-14T15:29:03Z</dc:date>
    </item>
    <item>
      <title>Re: Flag first record if there 5 consecutive same values in by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-first-record-if-there-5-consecutive-same-values-in-by-group/m-p/920952#M362722</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;, thanks for your reply! what if we need to check if there consecutive paramcd is there or not? I mean we need to flag if there are consecutive paramcd's without missing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Adithya&lt;/P&gt;</description>
      <pubDate>Tue, 19 Mar 2024 17:34:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-first-record-if-there-5-consecutive-same-values-in-by-group/m-p/920952#M362722</guid>
      <dc:creator>chinna0369</dc:creator>
      <dc:date>2024-03-19T17:34:47Z</dc:date>
    </item>
  </channel>
</rss>

