<?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 Using array to count observations per group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-array-to-count-observations-per-group/m-p/735301#M229062</link>
    <description>&lt;P&gt;Using SAS 9.4&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My data is all character (60 variables) except for the ID and&amp;nbsp;the characters are either "checked" or "unchecked" as the response.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have an array (see below) that I am trying to count every time a variable is "checked" by ID. The data set is unique so it is only one row per ID that I am trying to count. When I run the code the count continues down each row of the data set. I would appreciate any insight into how to correct the code. Thank you&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data array(drop=i);
	set check;
    array b _character_;
	by id;
    do i = 1 to dim(b);
        if b[i] =: 'C' then do;
            if first.id then count + 1;
        end;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 125px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/58328i39B5CCA14A0FAEFE/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It should be&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; Count&lt;/P&gt;
&lt;P&gt;7162&amp;nbsp; &amp;nbsp; 2&lt;/P&gt;
&lt;P&gt;7168&amp;nbsp; &amp;nbsp; 4&lt;/P&gt;
&lt;P&gt;7169&amp;nbsp; &amp;nbsp; 4&lt;/P&gt;
&lt;P&gt;7170&amp;nbsp; &amp;nbsp; 2&lt;/P&gt;
&lt;P&gt;etc.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 19 Apr 2021 15:53:03 GMT</pubDate>
    <dc:creator>GS2</dc:creator>
    <dc:date>2021-04-19T15:53:03Z</dc:date>
    <item>
      <title>Using array to count observations per group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-array-to-count-observations-per-group/m-p/735301#M229062</link>
      <description>&lt;P&gt;Using SAS 9.4&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My data is all character (60 variables) except for the ID and&amp;nbsp;the characters are either "checked" or "unchecked" as the response.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have an array (see below) that I am trying to count every time a variable is "checked" by ID. The data set is unique so it is only one row per ID that I am trying to count. When I run the code the count continues down each row of the data set. I would appreciate any insight into how to correct the code. Thank you&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data array(drop=i);
	set check;
    array b _character_;
	by id;
    do i = 1 to dim(b);
        if b[i] =: 'C' then do;
            if first.id then count + 1;
        end;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 125px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/58328i39B5CCA14A0FAEFE/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It should be&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; Count&lt;/P&gt;
&lt;P&gt;7162&amp;nbsp; &amp;nbsp; 2&lt;/P&gt;
&lt;P&gt;7168&amp;nbsp; &amp;nbsp; 4&lt;/P&gt;
&lt;P&gt;7169&amp;nbsp; &amp;nbsp; 4&lt;/P&gt;
&lt;P&gt;7170&amp;nbsp; &amp;nbsp; 2&lt;/P&gt;
&lt;P&gt;etc.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Apr 2021 15:53:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-array-to-count-observations-per-group/m-p/735301#M229062</guid>
      <dc:creator>GS2</dc:creator>
      <dc:date>2021-04-19T15:53:03Z</dc:date>
    </item>
    <item>
      <title>Re: Using array to count observations per group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-array-to-count-observations-per-group/m-p/735312#M229066</link>
      <description>&lt;P&gt;Maybe something like?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=check; by id; run;
proc transpose data=check out=tcheck;
	var var1-var60;
	by id;
run;

data tcheck2;
	set tcheck;
	by id;
	if first.id then counter=1*(col1='checked');
	else counter+1*(col1='checked');
	if last.id then output;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 19 Apr 2021 16:07:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-array-to-count-observations-per-group/m-p/735312#M229066</guid>
      <dc:creator>cminard</dc:creator>
      <dc:date>2021-04-19T16:07:10Z</dc:date>
    </item>
    <item>
      <title>Re: Using array to count observations per group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-array-to-count-observations-per-group/m-p/735335#M229079</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/194348"&gt;@GS2&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Using SAS 9.4&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My data is all character (60 variables) except for the ID and&amp;nbsp;the characters are either "checked" or "unchecked" as the response.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have an array (see below) that I am trying to count every time a variable is "checked" by ID. The data set is unique so it is only one row per ID that I am trying to count. When I run the code the count continues down each row of the data set. I would appreciate any insight into how to correct the code. Thank you&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data array(drop=i);
	set check;
    array b _character_;
	by id;
    do i = 1 to dim(b);
        if b[i] =: 'C' then do;
            if first.id then count + 1;
        end;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;When ever you use the construct&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt; then count + 1;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;you have told SAS implicitly to retain the value. So possibly you just need to use: then count = sum(count,1);&lt;/P&gt;
&lt;P&gt;Use of sum this way means that you do not need to initialize a value for count.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Apr 2021 18:02:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-array-to-count-observations-per-group/m-p/735335#M229079</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-04-19T18:02:01Z</dc:date>
    </item>
  </channel>
</rss>

