<?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: Create Index (ID) and increment in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-Index-ID-and-increment/m-p/663114#M197894</link>
    <description>&lt;P&gt;If you might have deleted records, something like this.&lt;/P&gt;
&lt;P&gt;Not the most efficient but compact.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data BASE;
  X=1;
  do INDEX=1 to 50;
   output;
  end;
run;
data ADD;
  X=2; output;
  X=3; output;
run;
data BASE;
  set BASE(in=B) ADD;
  if B then MAX=max(MAX,INDEX);
  else MAX+1;
  INDEX=coalesce(INDEX,MAX);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 18 Jun 2020 10:46:37 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2020-06-18T10:46:37Z</dc:date>
    <item>
      <title>Create Index (ID) and increment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Index-ID-and-increment/m-p/663109#M197890</link>
      <description>&lt;DIV class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center"&gt;Hello,&lt;/DIV&gt;&lt;DIV class="votecell post-layout--left"&gt;&lt;DIV class="js-voting-container grid fd-column ai-stretch gs4 fc-black-200"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class="postcell post-layout--right"&gt;&lt;DIV class="post-text"&gt;&lt;P&gt;I have a very simple table with sale information.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Name | Value | Index&lt;BR /&gt;AAC | 1000 | 1&lt;BR /&gt;BTR | 500 | 2&lt;BR /&gt;GRS | 250 | 3&lt;BR /&gt;AAC | 100 | 4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I add a new column Name Index. And I run the first time the following code&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;DATA BSP;
Index = _N_;
SET BSP;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This works fine for the first time. But now I add more and more sales items and the new line should be get a new indexnumber. The highest index + 1 .... The old sales should keep the indexnumber. But if I run the code below all new lines get the index = 1. What is wrong with the code.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;proc sql noprint;
  select max(Index) into :max_ID from WORK.BSP;
quit;

DATA work.BSP;
    SET work.BSP;
  RETAIN new_Id &amp;amp;max_ID;    
  IF Index = . THEN DO;
        new_ID + 1;
        index = new_id;
END;
RUN;&lt;BR /&gt;&lt;BR /&gt;Thanks, &lt;BR /&gt;Sascha&lt;/CODE&gt;&lt;/PRE&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Thu, 18 Jun 2020 10:25:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Index-ID-and-increment/m-p/663109#M197890</guid>
      <dc:creator>SaschaD</dc:creator>
      <dc:date>2020-06-18T10:25:15Z</dc:date>
    </item>
    <item>
      <title>Re: Create Index (ID) and increment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Index-ID-and-increment/m-p/663112#M197892</link>
      <description>&lt;P&gt;I don't see any data being added. Is that done in a prior step?&lt;/P&gt;
&lt;P&gt;How is your logic not working?&lt;/P&gt;
&lt;P&gt;If you never delete records, this is simple:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data BASE;
  X=1;
  do INDEX=1 to 50;
   output;
  end;
run;
data ADD;
  X=2;
run;
data BASE;
  set BASE ADD(in=A);
  if A then INDEX=_N_;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jun 2020 10:39:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Index-ID-and-increment/m-p/663112#M197892</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-06-18T10:39:51Z</dc:date>
    </item>
    <item>
      <title>Re: Create Index (ID) and increment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Index-ID-and-increment/m-p/663113#M197893</link>
      <description>&lt;P&gt;I think the problem is that you do not drop the NEW_ID variable. So if you run the same code multiple times, the calculated NEW_ID value will be overwritten by the value on the old dataset.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jun 2020 10:42:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Index-ID-and-increment/m-p/663113#M197893</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-06-18T10:42:43Z</dc:date>
    </item>
    <item>
      <title>Re: Create Index (ID) and increment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Index-ID-and-increment/m-p/663114#M197894</link>
      <description>&lt;P&gt;If you might have deleted records, something like this.&lt;/P&gt;
&lt;P&gt;Not the most efficient but compact.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data BASE;
  X=1;
  do INDEX=1 to 50;
   output;
  end;
run;
data ADD;
  X=2; output;
  X=3; output;
run;
data BASE;
  set BASE(in=B) ADD;
  if B then MAX=max(MAX,INDEX);
  else MAX+1;
  INDEX=coalesce(INDEX,MAX);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jun 2020 10:46:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Index-ID-and-increment/m-p/663114#M197894</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-06-18T10:46:37Z</dc:date>
    </item>
  </channel>
</rss>

