<?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 a count variable by group? in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/create-a-count-variable-by-group/m-p/815006#M81826</link>
    <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/240551"&gt;@richart&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This Communities Library article is answering your question :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How do I add a row number to a table in SAS code?&lt;BR /&gt;Posted 11-18-2015 08:14 AM | by Patrick (126287 views)&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-add-a-row-number-to-a-table-in-SAS-code/ta-p/235215" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-add-a-row-number-to-a-table-in-SAS-code/ta-p/235215&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
    <pubDate>Wed, 25 May 2022 12:23:33 GMT</pubDate>
    <dc:creator>sbxkoenk</dc:creator>
    <dc:date>2022-05-25T12:23:33Z</dc:date>
    <item>
      <title>create a count variable by group?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/create-a-count-variable-by-group/m-p/814975#M81823</link>
      <description>&lt;P&gt;Hi, say I have a dataset which descends by year. I want to create a group variable (want) such that it counts 1, 2, 3... etc. per year like so:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have;
   infile datalines delimiter='	'; 
   input year	want;
   datalines;                      
2020	1
2020	1
2019	2
2019	2
2019	2
2018	3
2018	3
2017	4
2017	4
2017	4
;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;any help would be appreciated. Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 25 May 2022 01:08:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/create-a-count-variable-by-group/m-p/814975#M81823</guid>
      <dc:creator>richart</dc:creator>
      <dc:date>2022-05-25T01:08:55Z</dc:date>
    </item>
    <item>
      <title>Re: create a count variable by group?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/create-a-count-variable-by-group/m-p/814976#M81824</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by year;

if _n_=1 the want2=0;
if first.year then want2+1;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/240551"&gt;@richart&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi, say I have a dataset which descends by year. I want to create a group variable (want) such that it counts 1, 2, 3... etc. per year like so:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
   infile datalines delimiter='	'; 
   input year	want;
   datalines;                      
2020	1
2020	1
2019	2
2019	2
2019	2
2018	3
2018	3
2017	4
2017	4
2017	4
;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;any help would be appreciated. Thanks!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 May 2022 01:30:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/create-a-count-variable-by-group/m-p/814976#M81824</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-05-25T01:30:01Z</dc:date>
    </item>
    <item>
      <title>Re: create a count variable by group?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/create-a-count-variable-by-group/m-p/814977#M81825</link>
      <description>&lt;P&gt;Just use a retained variable. It is simplified by using the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1dfiqj146yi2cn1maeju9wo7ijs.htm" target="_self"&gt;SUM statement&lt;/A&gt;.&amp;nbsp; Since the years are sorted in descending order include the DESCENDING keyword in the BY statement.&amp;nbsp; Take advantage of the fact that SAS creates the boolean FIRST. by group flags using 1 for TRUE and 0 for FALSE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input year @@;
datalines;
2020 2020 2019 2019 2019 2018 2018 2017 2017 2017
;

data want;
  set have;
  by descending year;
  group+first.year;
run;

proc print;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Obs    year    group

  1    2020      1
  2    2020      1
  3    2019      2
  4    2019      2
  5    2019      2
  6    2018      3
  7    2018      3
  8    2017      4
  9    2017      4
 10    2017      4
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 May 2022 02:14:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/create-a-count-variable-by-group/m-p/814977#M81825</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-05-25T02:14:25Z</dc:date>
    </item>
    <item>
      <title>Re: create a count variable by group?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/create-a-count-variable-by-group/m-p/815006#M81826</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/240551"&gt;@richart&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This Communities Library article is answering your question :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How do I add a row number to a table in SAS code?&lt;BR /&gt;Posted 11-18-2015 08:14 AM | by Patrick (126287 views)&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-add-a-row-number-to-a-table-in-SAS-code/ta-p/235215" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-add-a-row-number-to-a-table-in-SAS-code/ta-p/235215&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Wed, 25 May 2022 12:23:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/create-a-count-variable-by-group/m-p/815006#M81826</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2022-05-25T12:23:33Z</dc:date>
    </item>
    <item>
      <title>Re: create a count variable by group?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/create-a-count-variable-by-group/m-p/815319#M81827</link>
      <description>&lt;P&gt;thanks, this works too but I think you need to add 'descending' before year.. else it wasn't working for me&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2022 22:40:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/create-a-count-variable-by-group/m-p/815319#M81827</guid>
      <dc:creator>richart</dc:creator>
      <dc:date>2022-05-26T22:40:34Z</dc:date>
    </item>
  </channel>
</rss>

