<?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 have two different sequence ID's within the same grouping of data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-have-two-different-sequence-ID-s-within-the-same-grouping/m-p/923354#M363530</link>
    <description>&lt;P&gt;It appears to me that observations come in clusters of CFLG=1 and CFLG=..&amp;nbsp; For each cluster with CFLG=1 you want a constant NEW_SEQ_VAR value for every obs in the cluster.&amp;nbsp; &amp;nbsp;You want the first such cluster to have NEW_SEQ_VAR=1, the second to have NEW_SEQ_VAR=2, etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And apparently, with each new SUBJECT, you want to reset the first CLFG=1 group to restart with NEW_SEQ_VAR=1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If that's the case, then the BY statement needs only SUBJECT and CFLG, as below (untested in the absence of a working DATA step with sample data):&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 subject cflg notsorted;
  if first.subject then _cflg_group=0;
  if first.cflg=1 and cflg=1 then _cflg_group+1;
  if cflg=1 then new_seq_var=_cflg_group;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The data may be sorted by TERM and START within SUBJECT, but including TERM and START in the BY statement would be problematic.&lt;/P&gt;</description>
    <pubDate>Mon, 08 Apr 2024 01:10:42 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2024-04-08T01:10:42Z</dc:date>
    <item>
      <title>How to have two different sequence ID's within the same grouping of data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-have-two-different-sequence-ID-s-within-the-same-grouping/m-p/923352#M363528</link>
      <description>&lt;P&gt;I'm wanting to create a sequencing variable that can be used to distinguish two different entities within the same grouping (if that makes sense!)&lt;BR /&gt;&lt;BR /&gt;Below is a screenshot of the data I'm using (will try upload some actual datalines when it works) and the column highlighted in yellow is how I would like the sequencing variable to be grouped. So the data is currently grouped by SUBJECT, TERM and START. I'd like the sequencing variable to indicate that there are effectively two different instances within the same group of SUBJECT and TERM.&lt;BR /&gt;&lt;BR /&gt;CFLG indicates where these terms are linked, and as the data below shows there is a linking for the first 3 records, but then doesn't link again till the last two. So would like to indicate that there are two instances within this with the use of the NEW SEQUENCE VAR that I would like to create.&lt;BR /&gt;&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="group seq example.JPG" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/95287i810160528BBACF68/image-size/large?v=v2&amp;amp;px=999" role="button" title="group seq example.JPG" alt="group seq example.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The dataset I'm using is currently as follows (understand this is very barebones at the moment):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data example2;&lt;BR /&gt;set example;&lt;BR /&gt;by subject term start;&lt;BR /&gt;&lt;BR /&gt;if first.subject then newseq=0;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I've tried different variations of if first.cflg or first.term etc, just to absolutely no avail. It's probably the simplest thing I'm missing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any support would be greatly appreciated!&lt;/P&gt;</description>
      <pubDate>Sun, 07 Apr 2024 23:10:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-have-two-different-sequence-ID-s-within-the-same-grouping/m-p/923352#M363528</guid>
      <dc:creator>joshCRF</dc:creator>
      <dc:date>2024-04-07T23:10:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to have two different sequence ID's within the same grouping of data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-have-two-different-sequence-ID-s-within-the-same-grouping/m-p/923353#M363529</link>
      <description>&lt;P&gt;Based on your picture something like below should work (not tested).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data example2;
  set example;
  by subject term start;

  lag_cflg=lag(cflg);

  if first.term then r_newseq=cflg;
  else if cflg=1 and missing(lag(cflg)) then r_newseq+1;

  if cflg=1 then newseq=r_newseq;

  drop lag_cflg r_newseq;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 Apr 2024 00:57:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-have-two-different-sequence-ID-s-within-the-same-grouping/m-p/923353#M363529</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-04-08T00:57:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to have two different sequence ID's within the same grouping of data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-have-two-different-sequence-ID-s-within-the-same-grouping/m-p/923354#M363530</link>
      <description>&lt;P&gt;It appears to me that observations come in clusters of CFLG=1 and CFLG=..&amp;nbsp; For each cluster with CFLG=1 you want a constant NEW_SEQ_VAR value for every obs in the cluster.&amp;nbsp; &amp;nbsp;You want the first such cluster to have NEW_SEQ_VAR=1, the second to have NEW_SEQ_VAR=2, etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And apparently, with each new SUBJECT, you want to reset the first CLFG=1 group to restart with NEW_SEQ_VAR=1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If that's the case, then the BY statement needs only SUBJECT and CFLG, as below (untested in the absence of a working DATA step with sample data):&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 subject cflg notsorted;
  if first.subject then _cflg_group=0;
  if first.cflg=1 and cflg=1 then _cflg_group+1;
  if cflg=1 then new_seq_var=_cflg_group;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The data may be sorted by TERM and START within SUBJECT, but including TERM and START in the BY statement would be problematic.&lt;/P&gt;</description>
      <pubDate>Mon, 08 Apr 2024 01:10:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-have-two-different-sequence-ID-s-within-the-same-grouping/m-p/923354#M363530</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-04-08T01:10:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to have two different sequence ID's within the same grouping of data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-have-two-different-sequence-ID-s-within-the-same-grouping/m-p/923357#M363531</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*That would be better if you post some data by SAS code.*/
data have;
input subject id cflg;
cards;
1 1 1
1 2 1
1 3 1
1 4 .
1 5 .
1 6 .
1 7 .
1 8 .
1 9 .
1 10 .
1 11 1
1 12 1
2 1 1
2 2 1
2 3 1
2 4 .
2 5 .
;
data want(drop=count);
 set have;
 by subject;
 if first.subject then count=0;
 if first.subject or (cflg=1 and missing(lag(cflg)) ) then count+1;
 if cflg=1 then want=count;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 Apr 2024 02:00:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-have-two-different-sequence-ID-s-within-the-same-grouping/m-p/923357#M363531</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-04-08T02:00:25Z</dc:date>
    </item>
  </channel>
</rss>

