<?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: Creating aggregated variable with arrays in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-aggregated-variable-with-arrays/m-p/324609#M271328</link>
    <description>Ah, did not realize including the numeric on the end was necessary. I'll rename the variables and use your second suggestion. Thanks!&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
    <pubDate>Fri, 13 Jan 2017 15:58:21 GMT</pubDate>
    <dc:creator>abmitch95</dc:creator>
    <dc:date>2017-01-13T15:58:21Z</dc:date>
    <item>
      <title>Creating aggregated variable with arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-aggregated-variable-with-arrays/m-p/324568#M271325</link>
      <description>&lt;P&gt;I am prepping a dataset for analysis that contains descriptive variables for each study participant, as well as the participant's last 5 partners. My goal is to create a variable that summarizes the degree of similarity between each participant and the last 5 partners for select characteristics (age, income, education, etc.). Here is the code I am currently working with, using&amp;nbsp;age as an example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data master2;&lt;BR /&gt;set master;&lt;/P&gt;&lt;P&gt;age_diff1 = partner1_age - age;&lt;BR /&gt;age_diff2 = partner2_age - age;&lt;BR /&gt;age_diff3 = partner3_age - age;&lt;BR /&gt;age_diff4 = partner4_age - age;&lt;BR /&gt;age_diff5 = partner5_age - age;&lt;/P&gt;&lt;P&gt;age_diff_agg = sum(age_diff1, age_diff2, age_diff3, age_diff4, age_diff5);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Age difference is calculated for each of the last 5 partners, and then summed to create age_diff_agg (age difference aggregated). Is there&amp;nbsp;a way to do this without having to write out the formula 5 times for each characteristic? The code above is giving me the result I need but it is&amp;nbsp;tedious to repeat the process for 20+ variables. Thanks.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jan 2017 14:30:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-aggregated-variable-with-arrays/m-p/324568#M271325</guid>
      <dc:creator>abmitch95</dc:creator>
      <dc:date>2017-01-13T14:30:49Z</dc:date>
    </item>
    <item>
      <title>Re: Creating aggregated variable with arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-aggregated-variable-with-arrays/m-p/324574#M271326</link>
      <description>&lt;P&gt;You can use arrays. &amp;nbsp;You can either build an array of the age differences, or just sum the partner ages and subtract 5 times the age of the participant, like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data master2;
array age_diff [5] partner1_age-partner5_age;
set master;
age_diff_agg = sum(of age_diff[*]) - dim(age_diff)*age;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Jan 2017 14:50:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-aggregated-variable-with-arrays/m-p/324574#M271326</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-01-13T14:50:56Z</dc:date>
    </item>
    <item>
      <title>Re: Creating aggregated variable with arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-aggregated-variable-with-arrays/m-p/324579#M271327</link>
      <description>&lt;P&gt;Well, you have kind of shot yourself in the foot there. &amp;nbsp;Naming a variable partner1_age - i.e. not having the numeric on the end, makes arraying them harder. &amp;nbsp;You could try this which normalises the relevant data, then sums (no need to worry about how many or uneven elements then):&lt;/P&gt;
&lt;PRE&gt;data have;
  pt=1; age=21; income=20000; partner1_age=34; partner2_age=32; partner3_age=30; partner1_income=12000; partner2_income=10000; partner3_income=24000; 
run;

proc transpose data=have out=t_have;
  by pt age income;
  var partner:;
run;

data want;
  set t_have;
  by pt;
  retain age_sum income_sum;
  if index(_name_,"age")&amp;gt;0 then age_sum=sum(age_sum,col1);
  if index(_name_,"income")&amp;gt;0 then income_sum=sum(income_sum,col1);
  if last.pt then output;
run;&lt;/PRE&gt;
&lt;P&gt;Alternatively you could use arrays, but need to specify them:&lt;/P&gt;
&lt;PRE&gt;/* Same have as above */&lt;BR /&gt;data want;
  set have;
  array ages{3} partner1_age partner2_age partner3_age;
  array incomes{3} partner1_income partner2_income partner3_income;
  age_sum=sum(of ages{*});
  income_sum=sum(of incomes{*});
run;&lt;/PRE&gt;
&lt;P&gt;But, that being said, if you just follow good naming conventions, your code simplifies so much:&lt;/P&gt;
&lt;PRE&gt;data have;&lt;BR /&gt; pt=1; age=21; income=20000; partner_age1=34; partner_age2=32; partner_age3=30; partner_income1=12000; partner_income2=10000; partner_income3=24000; &lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data want;
  set have;
  array partner_age{3};
  array partner_income{3};
  age_sum=sum(of partner_age{*});
  income_sum=sum(of partner_income{*});
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Jan 2017 14:57:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-aggregated-variable-with-arrays/m-p/324579#M271327</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-01-13T14:57:16Z</dc:date>
    </item>
    <item>
      <title>Re: Creating aggregated variable with arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-aggregated-variable-with-arrays/m-p/324609#M271328</link>
      <description>Ah, did not realize including the numeric on the end was necessary. I'll rename the variables and use your second suggestion. Thanks!&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 13 Jan 2017 15:58:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-aggregated-variable-with-arrays/m-p/324609#M271328</guid>
      <dc:creator>abmitch95</dc:creator>
      <dc:date>2017-01-13T15:58:21Z</dc:date>
    </item>
    <item>
      <title>Re: Creating aggregated variable with arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-aggregated-variable-with-arrays/m-p/324611#M271329</link>
      <description>&lt;P&gt;Not required just easier to reference and process in order using SAS. You would see the the advantage very quickly with 100 variables: age_partner1- age_partner100 is ever so much easier to type...&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jan 2017 16:03:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-aggregated-variable-with-arrays/m-p/324611#M271329</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-01-13T16:03:24Z</dc:date>
    </item>
  </channel>
</rss>

