<?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 Create number counts based on different IDs and Classes? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-number-counts-based-on-different-IDs-and-Classes/m-p/845581#M334296</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm looking to create a number count based on the different IDs and Dataset.&amp;nbsp; My sample dataset and my pursuing result are listed below. Thanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data datain;
      infile datalines delimiter='/';
  input ID : $10.  Class : 2.0;
datalines;
	THY/1/
	THY/1/
	THY/3/
	THY/3/
	THY/3/
	THY/3/
	THY/5/
	OUW/2/
	OUW/3/
	OUW/3/
	OUW/4/
	OUW/4/
	OUW/4/
	OUW/4/
	OUW/4/
;
run;

data dataout;
	infile datalines delimiter='/';
	input ID : $10.  Class : 2.0 Count : 2.0;
datalines;
	THY/1/2/
	THY/3/4/
	THY/5/1/
	OUW/2/1/
	OUW/3/2/
	OUW/4/5/
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 21 Nov 2022 23:51:05 GMT</pubDate>
    <dc:creator>ybz12003</dc:creator>
    <dc:date>2022-11-21T23:51:05Z</dc:date>
    <item>
      <title>Create number counts based on different IDs and Classes?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-number-counts-based-on-different-IDs-and-Classes/m-p/845581#M334296</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm looking to create a number count based on the different IDs and Dataset.&amp;nbsp; My sample dataset and my pursuing result are listed below. Thanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data datain;
      infile datalines delimiter='/';
  input ID : $10.  Class : 2.0;
datalines;
	THY/1/
	THY/1/
	THY/3/
	THY/3/
	THY/3/
	THY/3/
	THY/5/
	OUW/2/
	OUW/3/
	OUW/3/
	OUW/4/
	OUW/4/
	OUW/4/
	OUW/4/
	OUW/4/
;
run;

data dataout;
	infile datalines delimiter='/';
	input ID : $10.  Class : 2.0 Count : 2.0;
datalines;
	THY/1/2/
	THY/3/4/
	THY/5/1/
	OUW/2/1/
	OUW/3/2/
	OUW/4/5/
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Nov 2022 23:51:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-number-counts-based-on-different-IDs-and-Classes/m-p/845581#M334296</guid>
      <dc:creator>ybz12003</dc:creator>
      <dc:date>2022-11-21T23:51:05Z</dc:date>
    </item>
    <item>
      <title>Re: Create number counts based on different IDs and Classes?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-number-counts-based-on-different-IDs-and-Classes/m-p/845582#M334297</link>
      <description>So basically just add the count of a ID/Class combo to each row?&lt;BR /&gt;&lt;BR /&gt;This demonstrates the average but it's trivial to change it to count.&lt;BR /&gt;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas&lt;/A&gt;</description>
      <pubDate>Mon, 21 Nov 2022 23:53:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-number-counts-based-on-different-IDs-and-Classes/m-p/845582#M334297</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-11-21T23:53:00Z</dc:date>
    </item>
    <item>
      <title>Re: Create number counts based on different IDs and Classes?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-number-counts-based-on-different-IDs-and-Classes/m-p/845584#M334298</link>
      <description>&lt;P&gt;If the data are already grouped by ID/CLASS, then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data datain;
      infile datalines delimiter='/';
  input ID : $10.  Class : 2.0;
datalines;
	THY/1/
	THY/1/
	THY/3/
	THY/3/
	THY/3/
	THY/3/
	THY/5/
	OUW/2/
	OUW/3/
	OUW/3/
	OUW/4/
	OUW/4/
	OUW/4/
	OUW/4/
	OUW/4/
;
run;

data want;
  set datain;
  by id class notsorted;
  if last.class;
  n=coalesce(dif(_n_),_n_);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The automatic variable _N_ is the iteration number of the data step, i.e. it counts the number of times the code in the data step is executed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this case, and in most cases, it is equivalent to the observation number.&amp;nbsp; So the "count" of an ID/CLASS group is just the value of _N_ at the end of a group minus the value of _N_ at the end of the preceding group (calcuated here by the &lt;EM&gt;&lt;STRONG&gt;dif&lt;/STRONG&gt;&lt;/EM&gt; function).&amp;nbsp; In the case of the first group the count is just _N_ itself.&lt;/P&gt;</description>
      <pubDate>Tue, 22 Nov 2022 00:31:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-number-counts-based-on-different-IDs-and-Classes/m-p/845584#M334298</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-11-22T00:31:48Z</dc:date>
    </item>
    <item>
      <title>Re: Create number counts based on different IDs and Classes?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-number-counts-based-on-different-IDs-and-Classes/m-p/845619#M334318</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table dataout as
  select
    id,
    class,
    count(*) as count
  from datain
  group by id, class
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 22 Nov 2022 07:37:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-number-counts-based-on-different-IDs-and-Classes/m-p/845619#M334318</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-11-22T07:37:04Z</dc:date>
    </item>
  </channel>
</rss>

