<?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 conditionally sum up certain number of cells in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-sum-up-certain-number-of-cells/m-p/553630#M153965</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Attr_F1 Attr_F2 Attr_F3 obs_len;
datalines;
1 2 3 3
1 2 3 2
;
run;

data want;
set have;
array t(999);
array a(*) attr_:;
call pokelong(peekclong(addrlong(a(1)),obs_len*8),addrlong(t(1)),obs_len*8);
want=sum(of t(*));
drop t:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 24 Apr 2019 13:16:36 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2019-04-24T13:16:36Z</dc:date>
    <item>
      <title>How to conditionally sum up certain number of cells</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-sum-up-certain-number-of-cells/m-p/553550#M153950</link>
      <description>&lt;P&gt;I have got some data like this:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Attr_F1&lt;/TD&gt;&lt;TD&gt;Attr_F2&lt;/TD&gt;&lt;TD&gt;Attr_F3&lt;/TD&gt;&lt;TD&gt;obs_len&lt;/TD&gt;&lt;TD&gt;want&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;sum( of Attr_F1 - Attr_F3)&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;sum( of Attr_F1 - Attr_F2)&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to get the sum() of first several&amp;nbsp;columns according to the value of the fourth column(obs_len).&lt;/P&gt;&lt;P&gt;For example: if obs_len =2, then I would like to sum up Attr_F1 and Attr_F2, etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you help me on this? Hopefully a data step to solve it. Thanks a lot.&lt;/P&gt;</description>
      <pubDate>Wed, 24 Apr 2019 08:12:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-sum-up-certain-number-of-cells/m-p/553550#M153950</guid>
      <dc:creator>Desmond</dc:creator>
      <dc:date>2019-04-24T08:12:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to conditionally sum up certain number of cells</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-sum-up-certain-number-of-cells/m-p/553552#M153951</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/271721"&gt;@Desmond&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
array Attr_F[3];
do _n_=1 to obs_len;
  want=sum(want,Attr_F[_n_]);
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 24 Apr 2019 08:22:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-sum-up-certain-number-of-cells/m-p/553552#M153951</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-04-24T08:22:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to conditionally sum up certain number of cells</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-sum-up-certain-number-of-cells/m-p/553559#M153953</link>
      <description>&lt;P&gt;Hi Thanks a lot for your reply.&lt;/P&gt;&lt;P&gt;However I got this error:&lt;/P&gt;&lt;P&gt;ERROR: Invalid DO loop control information, either the INITIAL or TO expression is missing or the BY expression is missing,&lt;BR /&gt;&amp;nbsp;zero,&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; or invalid.&lt;/P&gt;</description>
      <pubDate>Wed, 24 Apr 2019 09:03:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-sum-up-certain-number-of-cells/m-p/553559#M153953</guid>
      <dc:creator>Desmond</dc:creator>
      <dc:date>2019-04-24T09:03:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to conditionally sum up certain number of cells</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-sum-up-certain-number-of-cells/m-p/553563#M153956</link>
      <description>&lt;P&gt;The code DOES work, see this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Attr_F1 Attr_F2 Attr_F3 obs_len;
datalines;
1 2 3 3
1 2 3 2
;
run;

data want;
set have;
array Attr_F[3];
do _n_=1 to obs_len;
  want=sum(want,Attr_F[_n_]);
end;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Log:&lt;/P&gt;
&lt;PRE&gt;24         data have;
25         input Attr_F1 Attr_F2 Attr_F3 obs_len;
26         datalines;

NOTE: The data set WORK.HAVE has 2 observations and 4 variables.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.00 seconds
      
29         ;

30         run;
31         
32         data want;
33         set have;
34         array Attr_F[3];
35         do _n_=1 to obs_len;
36           want=sum(want,Attr_F[_n_]);
37         end;
38         run;

NOTE: There were 2 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.WANT has 2 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds
      

39         
40         proc print data=want noobs;
41         run;

NOTE: There were 2 observations read from the data set WORK.WANT.
NOTE: The PROCEDURE PRINT printed page 1.
2                                                          Das SAS System                            08:14 Wednesday, April 24, 2019

NOTE: PROZEDUR PRINT used (Total process time):
      real time           0.02 seconds
      cpu time            0.00 seconds
&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Attr_F1    Attr_F2    Attr_F3    obs_len    want

   1          2          3          3         6 
   1          2          3          2         3 
&lt;/PRE&gt;
&lt;P&gt;This once again illustrates the utmost importance of properly posting example data in a &lt;U&gt;data step with datalines&lt;/U&gt;.&lt;/P&gt;</description>
      <pubDate>Wed, 24 Apr 2019 09:19:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-sum-up-certain-number-of-cells/m-p/553563#M153956</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-04-24T09:19:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to conditionally sum up certain number of cells</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-sum-up-certain-number-of-cells/m-p/553596#M153959</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/271721"&gt;@Desmond&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi Thanks a lot for your reply.&lt;/P&gt;
&lt;P&gt;However I got this error:&lt;/P&gt;
&lt;P&gt;ERROR: Invalid DO loop control information, either the INITIAL or TO expression is missing or the BY expression is missing,&lt;BR /&gt;&amp;nbsp;zero,&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; or invalid.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So, it appears that you have an observation with a missing value of OBS_LEN. &lt;EM&gt;You&lt;/EM&gt; need to decide how to define the value of variable WANT in this case -- and also in other special cases not covered by your sample data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, you could define:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;If OBS_LEN is missing or all values of ATTR_F[*] are missing, WANT should be missing as well.&lt;/LI&gt;
&lt;LI&gt;Otherwise, if OBS_LEN is zero or negative, WANT should be set to 0 ("empty sum").&lt;/LI&gt;
&lt;LI&gt;Missing values involved in the sum are treated as zeros, unless all summands are missing (in which case WANT is set to missing).&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;A more robust DATA step implementing the above rules could look like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
array Attr_F[3];
if obs_len&amp;lt;=.z | ~n(of Attr_F[*]) then want=.;
else if obs_len&amp;lt;=0 then want=0;
else do _n_=1 to obs_len;
  if Attr_F[_n_]&amp;gt;.z then want=sum(want,Attr_F[_n_]);
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 24 Apr 2019 12:15:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-sum-up-certain-number-of-cells/m-p/553596#M153959</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-04-24T12:15:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to conditionally sum up certain number of cells</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-sum-up-certain-number-of-cells/m-p/553630#M153965</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Attr_F1 Attr_F2 Attr_F3 obs_len;
datalines;
1 2 3 3
1 2 3 2
;
run;

data want;
set have;
array t(999);
array a(*) attr_:;
call pokelong(peekclong(addrlong(a(1)),obs_len*8),addrlong(t(1)),obs_len*8);
want=sum(of t(*));
drop t:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 24 Apr 2019 13:16:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-sum-up-certain-number-of-cells/m-p/553630#M153965</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-04-24T13:16:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to conditionally sum up certain number of cells</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-sum-up-certain-number-of-cells/m-p/553836#M154045</link>
      <description>&lt;P&gt;Hi That's exactly the case.&lt;/P&gt;&lt;P&gt;Thanks a lot!! best!&lt;/P&gt;</description>
      <pubDate>Thu, 25 Apr 2019 02:35:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-sum-up-certain-number-of-cells/m-p/553836#M154045</guid>
      <dc:creator>Desmond</dc:creator>
      <dc:date>2019-04-25T02:35:47Z</dc:date>
    </item>
  </channel>
</rss>

