<?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: variable counting in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/variable-counting/m-p/387558#M92922</link>
    <description>&lt;P&gt;Apart from PROC SQL, if you wish to perform the action using DATA STEP, you may proceed like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data All_Units_Results;&lt;BR /&gt;input fault_code$;&lt;BR /&gt;datalines;&lt;BR /&gt;20-1802&lt;BR /&gt;20-1803&lt;BR /&gt;20-1805&lt;BR /&gt;20-1803&lt;BR /&gt;20-1803&lt;BR /&gt;20-1802&lt;BR /&gt;20-1801&lt;BR /&gt;;&lt;BR /&gt;proc sort data=all_units_results;&lt;/P&gt;&lt;P&gt;by fault_code;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data Fault_Count;&lt;BR /&gt;set All_Units_Results;&lt;/P&gt;&lt;P&gt;by fault_code;&lt;BR /&gt;if first.fault_code then fault_count=0;&lt;BR /&gt;fault_count+1;&lt;BR /&gt;if last.fault_code then output;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;</description>
    <pubDate>Sat, 12 Aug 2017 06:17:36 GMT</pubDate>
    <dc:creator>viditmalhotra89</dc:creator>
    <dc:date>2017-08-12T06:17:36Z</dc:date>
    <item>
      <title>variable counting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/variable-counting/m-p/387453#M92898</link>
      <description>&lt;P&gt;I tried the following code without success. Fault_Count is a new table and&amp;nbsp;All_Units_Results is an existing table.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Fault_Count ;
set All_Units_Results(keep= fault_code);
if first.fault_code then fault_count=0;
fault_count=fault_count+1;
*if last.fault_code then output;


run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;An example of Fault_Count follows. I'm trying to get a count of each individual fault occurance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;20-1802&lt;BR /&gt;20-1803&lt;BR /&gt;20-1805&lt;BR /&gt;20-1803&lt;BR /&gt;20-1803&lt;BR /&gt;20-1802&lt;BR /&gt;20-1801&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The output should look something like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;20-1801 &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;20-1802 &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;20-1803 &amp;nbsp; &amp;nbsp; 3&lt;/P&gt;&lt;P&gt;20-1805 &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The above code produces:&lt;/P&gt;&lt;P&gt;20-1802 &amp;nbsp; .&lt;BR /&gt;20-1803 &amp;nbsp; .&lt;BR /&gt;20-1805 &amp;nbsp; .&lt;BR /&gt;20-1803 &amp;nbsp; .&lt;BR /&gt;20-1803 &amp;nbsp; .&lt;BR /&gt;20-1802 &amp;nbsp; .&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;capam&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2017 18:19:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/variable-counting/m-p/387453#M92898</guid>
      <dc:creator>capam</dc:creator>
      <dc:date>2017-08-11T18:19:04Z</dc:date>
    </item>
    <item>
      <title>Re: variable counting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/variable-counting/m-p/387456#M92899</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table fault_count as
select fault_code, count(*)
from all_units_results
group by fault_count;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In your code, you missed a retain statement for variable fault_count. Alternatively, you could use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;fault_count + 1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;as increment statement. SAS will then implicitly retain the variable.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2017 18:29:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/variable-counting/m-p/387456#M92899</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-08-11T18:29:26Z</dc:date>
    </item>
    <item>
      <title>Re: variable counting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/variable-counting/m-p/387458#M92901</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input fault_code $;
cards;
20-1802
20-1803
20-1805
20-1803
20-1803
20-1802
20-1801
;
run;

proc sort data=have; by fault_code; run;

data want;
set have;
by fault_code;
if first.fault_code then fault_count=1;
else fault_count+1;
if last.fault_code;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Aug 2017 18:37:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/variable-counting/m-p/387458#M92901</guid>
      <dc:creator>PBsas</dc:creator>
      <dc:date>2017-08-11T18:37:11Z</dc:date>
    </item>
    <item>
      <title>Re: variable counting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/variable-counting/m-p/387459#M92902</link>
      <description>&lt;P&gt;Thanks Kurt,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried the following code - notice group by is corrected.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table fault_count as
select fault_code, count(*)
from all_units_results
group by fault_code;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;It works well. Thank you very much.&lt;/P&gt;&lt;P&gt;capam&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2017 18:40:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/variable-counting/m-p/387459#M92902</guid>
      <dc:creator>capam</dc:creator>
      <dc:date>2017-08-11T18:40:15Z</dc:date>
    </item>
    <item>
      <title>Re: variable counting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/variable-counting/m-p/387460#M92903</link>
      <description>Kurt,&lt;BR /&gt;&lt;BR /&gt;How do you give the output a label. Currently it comes back as _TEMG001. Thanks.&lt;BR /&gt;</description>
      <pubDate>Fri, 11 Aug 2017 18:45:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/variable-counting/m-p/387460#M92903</guid>
      <dc:creator>capam</dc:creator>
      <dc:date>2017-08-11T18:45:41Z</dc:date>
    </item>
    <item>
      <title>Re: variable counting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/variable-counting/m-p/387461#M92904</link>
      <description>&lt;P&gt;&lt;SPAN class="token procnames"&gt;proc&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;sql&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;BR /&gt;create &lt;SPAN class="token statement"&gt;table&lt;/SPAN&gt; fault_count as&lt;BR /&gt;&lt;SPAN class="token statement"&gt;select&lt;/SPAN&gt; fault_code&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;count&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;) as fault_count&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="token keyword"&gt;from&lt;/SPAN&gt; all_units_results&lt;BR /&gt;&lt;SPAN class="token keyword"&gt;group&lt;/SPAN&gt; &lt;SPAN class="token statement"&gt;by&lt;/SPAN&gt; fault_code&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="token procnames"&gt;quit&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2017 18:47:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/variable-counting/m-p/387461#M92904</guid>
      <dc:creator>PBsas</dc:creator>
      <dc:date>2017-08-11T18:47:53Z</dc:date>
    </item>
    <item>
      <title>Re: variable counting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/variable-counting/m-p/387463#M92905</link>
      <description>&lt;P&gt;Please change the soultion to the Kurt's post. He was the first to solve your question.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2017 18:55:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/variable-counting/m-p/387463#M92905</guid>
      <dc:creator>PBsas</dc:creator>
      <dc:date>2017-08-11T18:55:42Z</dc:date>
    </item>
    <item>
      <title>Re: variable counting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/variable-counting/m-p/387558#M92922</link>
      <description>&lt;P&gt;Apart from PROC SQL, if you wish to perform the action using DATA STEP, you may proceed like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data All_Units_Results;&lt;BR /&gt;input fault_code$;&lt;BR /&gt;datalines;&lt;BR /&gt;20-1802&lt;BR /&gt;20-1803&lt;BR /&gt;20-1805&lt;BR /&gt;20-1803&lt;BR /&gt;20-1803&lt;BR /&gt;20-1802&lt;BR /&gt;20-1801&lt;BR /&gt;;&lt;BR /&gt;proc sort data=all_units_results;&lt;/P&gt;&lt;P&gt;by fault_code;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data Fault_Count;&lt;BR /&gt;set All_Units_Results;&lt;/P&gt;&lt;P&gt;by fault_code;&lt;BR /&gt;if first.fault_code then fault_count=0;&lt;BR /&gt;fault_count+1;&lt;BR /&gt;if last.fault_code then output;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Sat, 12 Aug 2017 06:17:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/variable-counting/m-p/387558#M92922</guid>
      <dc:creator>viditmalhotra89</dc:creator>
      <dc:date>2017-08-12T06:17:36Z</dc:date>
    </item>
    <item>
      <title>Re: variable counting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/variable-counting/m-p/387691#M92971</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/154084"&gt;@capam&lt;/a&gt; wrote:&lt;BR /&gt;Kurt,&lt;BR /&gt;&lt;BR /&gt;How do you give the output a label. Currently it comes back as _TEMG001. Thanks.&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;See &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/53881"&gt;@PBsas&lt;/a&gt;'s suggestion. And start working through the documentation for proc sql. SQL is the "swiss army knife" for databases, just as the data step is for SAS datasets.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Aug 2017 06:38:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/variable-counting/m-p/387691#M92971</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-08-14T06:38:10Z</dc:date>
    </item>
  </channel>
</rss>

