<?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 add a rownumber to a table in SAS code? in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-add-a-rownumber-to-a-table-in-SAS-code/m-p/234357#M16942</link>
    <description>&lt;P&gt;Your code doesn't seem to be correct. &amp;nbsp;There is a space after the period in first., which shouldn't work, and you have no retain statement. &amp;nbsp;Can you also post example test data (as a datastep, just a couple of lines of data) and required output, so we can see clearly what you want. &amp;nbsp;At a guess:&lt;/P&gt;
&lt;PRE&gt;data mytable;
  set mytable;
  by var1 var2;
  retain n;
  if first.var2 then n=1;
  else n=n+1;
run;&lt;/PRE&gt;
&lt;P&gt;This will create an n variable as a count within var2. &amp;nbsp;Note needs to be sorted first.&lt;/P&gt;</description>
    <pubDate>Thu, 12 Nov 2015 09:38:43 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2015-11-12T09:38:43Z</dc:date>
    <item>
      <title>How to add a rownumber to a table in SAS code?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-add-a-rownumber-to-a-table-in-SAS-code/m-p/234355#M16941</link>
      <description>&lt;P&gt;I want to add a rownumber to a table. In sql you can do a partition by to add a rownumber and start counting the rownumber on several BY variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have found this code:&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;Data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; myTable;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;Set&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt; myTable;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;By&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt; Var1;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;if&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; &lt;FONT color="#008080" face="Courier New" size="3"&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;first.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt; Var1 &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt; n=&lt;/FONT&gt;&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;else&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt; n+&lt;/FONT&gt;&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt;How can I add a rownumber using more than 1 variable?&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Nov 2015 09:28:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-add-a-rownumber-to-a-table-in-SAS-code/m-p/234355#M16941</guid>
      <dc:creator>AnneBouman</dc:creator>
      <dc:date>2015-11-12T09:28:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to add a rownumber to a table in SAS code?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-add-a-rownumber-to-a-table-in-SAS-code/m-p/234357#M16942</link>
      <description>&lt;P&gt;Your code doesn't seem to be correct. &amp;nbsp;There is a space after the period in first., which shouldn't work, and you have no retain statement. &amp;nbsp;Can you also post example test data (as a datastep, just a couple of lines of data) and required output, so we can see clearly what you want. &amp;nbsp;At a guess:&lt;/P&gt;
&lt;PRE&gt;data mytable;
  set mytable;
  by var1 var2;
  retain n;
  if first.var2 then n=1;
  else n=n+1;
run;&lt;/PRE&gt;
&lt;P&gt;This will create an n variable as a count within var2. &amp;nbsp;Note needs to be sorted first.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Nov 2015 09:38:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-add-a-rownumber-to-a-table-in-SAS-code/m-p/234357#M16942</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2015-11-12T09:38:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to add a rownumber to a table in SAS code?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-add-a-rownumber-to-a-table-in-SAS-code/m-p/234495#M16945</link>
      <description>&lt;P&gt;Not sure that I understand what you mean by "&lt;SPAN&gt;using more than 1 variable&lt;/SPAN&gt;".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If it's simply about the row number then SAS has an automatic variable _n_ which gives you exactly that. If you want to store this number permanently in a new variable then it's as simple as:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set sashelp.class;
  rownum=_n_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you're more after creating&amp;nbsp;a group identifier then something like below should work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by var1 var2;  /* data set have must be sorted by var1 var2 */
  if first.var2 then group_id+1;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;SAS syntax "group_id+1" as used in above code retains the variable so you don't need an additional retain statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or if you're after a counter within a group:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by var1 var2;  /* data set have must be sorted by var1 var2 */
  if first.var2 then 
    do;
      group_id+1;
      counter_in_group=1;
    end;
  else counter_in_group+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Nov 2015 12:53:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-add-a-rownumber-to-a-table-in-SAS-code/m-p/234495#M16945</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2015-11-18T12:53:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to add a rownumber to a table in SAS code?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-add-a-rownumber-to-a-table-in-SAS-code/m-p/234497#M16946</link>
      <description>&lt;P&gt;SAS will generate FIRST. and LAST. flag variables for every variable in your BY statement.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If it takes more than one variable to uniquely identify a group then you would just want to use flags for the last of them.&lt;/P&gt;
&lt;P&gt;For example this program will generate a unique id that increments for each new by group and also an sequence number that starts over at one within each group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ; 
  set have ;
  by state city ;
  city_id + first.city ;
  if first.city then seqno=0;
  seqno+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 12 Nov 2015 21:26:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-add-a-rownumber-to-a-table-in-SAS-code/m-p/234497#M16946</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2015-11-12T21:26:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to add a rownumber to a table in SAS code?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-add-a-rownumber-to-a-table-in-SAS-code/m-p/235195#M16988</link>
      <description>&lt;P&gt;Thanks for your answer. This is exactly what I needed.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Nov 2015 10:19:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-add-a-rownumber-to-a-table-in-SAS-code/m-p/235195#M16988</guid>
      <dc:creator>AnneBouman</dc:creator>
      <dc:date>2015-11-18T10:19:44Z</dc:date>
    </item>
  </channel>
</rss>

