<?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: Using array to sum across columns with condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-array-to-sum-across-columns-with-condition/m-p/751360#M236516</link>
    <description>FYI - if you restructure your data to a long format these types of questions are much easier to answer.</description>
    <pubDate>Wed, 30 Jun 2021 20:09:15 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2021-06-30T20:09:15Z</dc:date>
    <item>
      <title>Using array to sum across columns with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-array-to-sum-across-columns-with-condition/m-p/751347#M236510</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset that includes the frequency that participants did 94 different activities in the past 30 days (1 frequency variable for each activity, each on a 0-4 scale) as well as an additional 94 variables indicating where each activity took place (1=indoors, 2=outdoors, 3=NA if the activity was not done). I would like to aggregate across this information so that I have an aggregate indicator of how often outdoor activities were done, incorporating both the number of outdoor activities as well as how often each of these was done.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am a little rusty on use of arrays. I tried to create arrays for the 94 frequency and 94 activity variables; my ultimate goal is to calculate a SUM of all thearrays, sum across columns frequency variables for which the participant indicated the activity was typically done outdoors (so, a sum function but including only those activities where the corresponding where variable = 2).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is what I have for code below, but based on the aggregate value coming out, I don't think it is working correctly. Any ideas? Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data bl;&lt;BR /&gt;set go.bl;&lt;BR /&gt;*Create an array of all the WHERE variables;&lt;BR /&gt;array actwhere {94} Ac1WatchTVwhere Ac1WatchMovieHomewhere [etc... each of these is a variable that shows the frequency the participate did a given activity, e.g. the first one here where they typically watched TV 1=indoors, 2=outdoors, 3=NA; I'm not going to list out all 94 here because it makes this hard to read]&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;*Create an array of all the FREQ variables;&lt;BR /&gt;array actfreq {94} Ac1WatchTVfreq Ac1WatchMovieHomefreq [etc... again listing all 94 of these variables, which indicate the frequency of each activity on the 0-4 scale]&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;do i = 1 to 94;&lt;/P&gt;&lt;P&gt;if actwhere{i} = 2 then do; *Trying to isolate activities done outside here before summing;&lt;BR /&gt;act_outdoor_freq=sum(of actfreq{i}); *Trying to create a new aggregate variable that is the sum of the frequencies of all those activities done outside;&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Jun 2021 19:10:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-array-to-sum-across-columns-with-condition/m-p/751347#M236510</guid>
      <dc:creator>stephneepa</dc:creator>
      <dc:date>2021-06-30T19:10:27Z</dc:date>
    </item>
    <item>
      <title>Re: Using array to sum across columns with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-array-to-sum-across-columns-with-condition/m-p/751349#M236511</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;act_outdoor_freq=0;
do i = 1 to 94;
    if actwhere{i} = 2 then act_outdoor_freq=sum(act_outdoor_freq,actfreq{i});&amp;nbsp;
end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 30 Jun 2021 19:26:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-array-to-sum-across-columns-with-condition/m-p/751349#M236511</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-06-30T19:26:25Z</dc:date>
    </item>
    <item>
      <title>Re: Using array to sum across columns with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-array-to-sum-across-columns-with-condition/m-p/751360#M236516</link>
      <description>FYI - if you restructure your data to a long format these types of questions are much easier to answer.</description>
      <pubDate>Wed, 30 Jun 2021 20:09:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-array-to-sum-across-columns-with-condition/m-p/751360#M236516</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-06-30T20:09:15Z</dc:date>
    </item>
  </channel>
</rss>

