<?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 How to continue a sequence of numbers from known start, by groups. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-continue-a-sequence-of-numbers-from-known-start-by-groups/m-p/653568#M196328</link>
    <description>&lt;P&gt;I have data where i have a broken sequence of numbers (seq_have) where i want to continue the sequence from largest number in the existing sequence for each ID, for the observations with missing sequence numbers. The order of the observations without sequence numbers is arbitrary, as long as it doesn't overlap and is larger then the largest number of the existing sequence inside the group.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The solution i have come up with doesn't seem very elegant, and I'm worried it will be prone to errors once implemented at a larger scale.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any thoughts on the likelihood of errors that could occur, or how to improve the solution?&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;The example:&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data example;
input ID $ seq_have data;
cards;
1 1 111
1 2 222
1 . 333
2 . 444
2 2 222
2 3 333
3 . 123
3 . 123
3 . 123
4 4 444
4 . 555
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;Attempted solution:&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
 create table int1 as select *, max(seq_have) as seq_max from example group by id;
quit;

proc sort data=int1;
 by id seq_have;
run;

data int2;
 set int1;
 by id ;
 seq_new +1;
 if first.id then seq_new = 1;
run;

data result;
set int2;
 if seq_have eq . then 
  seq_res = sum(seq_new,seq_max);
 if seq_have not eq . then seq_res = seq_have;
run;

proc sort data=result;
 by id seq_res;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 05 Jun 2020 12:39:53 GMT</pubDate>
    <dc:creator>SteinOk</dc:creator>
    <dc:date>2020-06-05T12:39:53Z</dc:date>
    <item>
      <title>How to continue a sequence of numbers from known start, by groups.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-continue-a-sequence-of-numbers-from-known-start-by-groups/m-p/653568#M196328</link>
      <description>&lt;P&gt;I have data where i have a broken sequence of numbers (seq_have) where i want to continue the sequence from largest number in the existing sequence for each ID, for the observations with missing sequence numbers. The order of the observations without sequence numbers is arbitrary, as long as it doesn't overlap and is larger then the largest number of the existing sequence inside the group.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The solution i have come up with doesn't seem very elegant, and I'm worried it will be prone to errors once implemented at a larger scale.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any thoughts on the likelihood of errors that could occur, or how to improve the solution?&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;The example:&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data example;
input ID $ seq_have data;
cards;
1 1 111
1 2 222
1 . 333
2 . 444
2 2 222
2 3 333
3 . 123
3 . 123
3 . 123
4 4 444
4 . 555
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;Attempted solution:&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
 create table int1 as select *, max(seq_have) as seq_max from example group by id;
quit;

proc sort data=int1;
 by id seq_have;
run;

data int2;
 set int1;
 by id ;
 seq_new +1;
 if first.id then seq_new = 1;
run;

data result;
set int2;
 if seq_have eq . then 
  seq_res = sum(seq_new,seq_max);
 if seq_have not eq . then seq_res = seq_have;
run;

proc sort data=result;
 by id seq_res;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jun 2020 12:39:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-continue-a-sequence-of-numbers-from-known-start-by-groups/m-p/653568#M196328</guid>
      <dc:creator>SteinOk</dc:creator>
      <dc:date>2020-06-05T12:39:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to continue a sequence of numbers from known start, by groups.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-continue-a-sequence-of-numbers-from-known-start-by-groups/m-p/653611#M196333</link>
      <description>&lt;P&gt;Use a DOW loop to compute the max seq_have in each group, then in subsequent loop use max_seq_have as basis for computing needed seq_have.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;data want;
  do until (last.id);
    set have;
    by id;
    max_seq_have = max(max_seq_have, seq_have);
  end;

  do until (last.id);
    set have;
    if missing(seq_have) then do;
      _k = sum(_k, 1); 
      seq_have = sum (max_seq_have, _k);
    end;
    output;

  drop _k max_seq_have;
  end;&lt;/PRE&gt;</description>
      <pubDate>Fri, 05 Jun 2020 14:51:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-continue-a-sequence-of-numbers-from-known-start-by-groups/m-p/653611#M196333</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-06-05T14:51:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to continue a sequence of numbers from known start, by groups.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-continue-a-sequence-of-numbers-from-known-start-by-groups/m-p/653669#M196338</link>
      <description>&lt;P&gt;If you have access to a database like Oracle, then I recommend the use of database sequences to ensure you keep sequences clean. &lt;BR /&gt;This is is especially useful is the numbers being sequenced are id columns and the tables may get records added by multiple program which may potentially run in parallel.&amp;nbsp; &amp;nbsp;The database guarantees that no numbers are duplicated - which can happen if concurrently executing SAS programs all get the max value from a table and deduce the next number.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;I recently created a sas macro that uses Oracle passthru to initialise such Oracle sequences from the current maximum value (+ 1) &lt;BR /&gt;This use Oracle passthrough. &lt;BR /&gt;Once the sequences are established another macro gets the next sequence val.&lt;BR /&gt;The heart of the macro has code like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;  
   connect to Oracle (&amp;amp;connection_info);
 
 /* not shown here: code to check if the  sequence exists and create 
    it if needed */

   select nextval into :_netxval from
      connection to Oracle (select &amp;amp;sequence..nextval from dual);
  disconnect from Oracle;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The performance of identifying the maximum value depends on the number of records in the table.&amp;nbsp; &amp;nbsp;Oracle sequences (and I have done the same in DB2) keep an underlying table with the sequence name and next value.&amp;nbsp; Queries to it locks the row, provides the next value, and then&amp;nbsp; unlocks the row, thereby guaranteeing uniqueness.&amp;nbsp; &amp;nbsp;Oracle always does this in a fraction of a second.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jun 2020 15:36:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-continue-a-sequence-of-numbers-from-known-start-by-groups/m-p/653669#M196338</guid>
      <dc:creator>DavePrinsloo</dc:creator>
      <dc:date>2020-06-05T15:36:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to continue a sequence of numbers from known start, by groups.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-continue-a-sequence-of-numbers-from-known-start-by-groups/m-p/653678#M196342</link>
      <description>&lt;P&gt;Assuming the input lay out is what you have and the data is ordered by id things are little simpler. You have the following scenarios&lt;/P&gt;
&lt;P&gt;1. What if the by group starts with a missing value and the first non miss value in the by group is 1?&lt;/P&gt;
&lt;P&gt;2. The first value is missing but all the other values are okay&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assumption: Data is already ordered by id and everything is in place&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data example;
input ID $ seq_have data;
cards;
1 1 111
1 2 222
1 . 333
2 . 444
2 2 222
2 3 333
3 . 123
3 . 123
3 . 123
4 4 444
4 . 555
;
run;

data example2;
set example;
by id;
retain have_seq;
if first.id then do;
	if missing(seq_have) then do;
		have_seq=1;
	end;
	else do;
          have_seq=seq_have;
    end;
end;
else do;
if ^missing(seq_have) then have_seq=seq_have;
	if missing(seq_have) then do;
		have_seq=have_seq+1;
		*have_seq = seq_have;
	end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jun 2020 15:40:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-continue-a-sequence-of-numbers-from-known-start-by-groups/m-p/653678#M196342</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2020-06-05T15:40:49Z</dc:date>
    </item>
  </channel>
</rss>

