<?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: Code to multiply two variables and sum result for all records in each month in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Code-to-multiply-two-variables-and-sum-result-for-all-records-in/m-p/629028#M185959</link>
    <description>&lt;P&gt;I like to create a new variable, because it can also be used in a data step BY or in a SQL GROUP BY and yield the same groups. But that's just me.&lt;/P&gt;</description>
    <pubDate>Tue, 03 Mar 2020 06:29:44 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-03-03T06:29:44Z</dc:date>
    <item>
      <title>Code to multiply two variables and sum result for all records in each month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-to-multiply-two-variables-and-sum-result-for-all-records-in/m-p/627886#M185440</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset (called "ac.test") that has three million records and there are three variables: DATE, VALUE_1 and VALUE_2, all of which are numeric variables. Is there any code that could help to efficiently create a new variable "Value_1_times_2" by calculating VALUE_1 multiplied by VALUE_2 for all records for each month (the screenshot below only part of the dataset, with dates from June 2017-January 2019 in the complete dataset), then summing up all of the individual accounts for that month using proc summary?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;Steps:&lt;/U&gt;&lt;/P&gt;&lt;P&gt;1. &lt;U&gt;Create new variable, "Value_1_times_2"&lt;/U&gt;.&amp;nbsp;Value_1_times_2 = Value_1*Value_2.&lt;/P&gt;&lt;P&gt;2. &lt;U&gt;Sum up all of&amp;nbsp;"Value_1_times_2" for that month&lt;/U&gt; e.g. in the screenshot below, &lt;U&gt;proc summary&lt;/U&gt; would show that the sum of all&amp;nbsp;"Value_1_times_2" for August 2018 (note that all records for each month only have the end date of the month) i.e. 46.&lt;/P&gt;&lt;P&gt;3. Repeat the process/loop for &lt;U&gt;all months&lt;/U&gt; in the dataset in order to show that the sum of&amp;nbsp;"Value_1_times_2" in August 2018 is 46, 94 in September&amp;nbsp;2018, 53 in October&amp;nbsp;2018, 79 in November&amp;nbsp;2018 and 77 in December&amp;nbsp;2018 etc.?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SAS multiply two values 2.JPG" style="width: 477px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/36440i997B1E14BFB6A9F4/image-size/large?v=v2&amp;amp;px=999" role="button" title="SAS multiply two values 2.JPG" alt="SAS multiply two values 2.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2020 14:52:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-to-multiply-two-variables-and-sum-result-for-all-records-in/m-p/627886#M185440</guid>
      <dc:creator>jeremy4</dc:creator>
      <dc:date>2020-02-27T14:52:53Z</dc:date>
    </item>
    <item>
      <title>Re: Code to multiply two variables and sum result for all records in each month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-to-multiply-two-variables-and-sum-result-for-all-records-in/m-p/627891#M185442</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/266226"&gt;@jeremy4&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does the following code meet your expectations?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
	input Date:date9. Value_1 Value_2;
	format Date date9.;
	datalines;
30AUG2018 3 8
30AUG2018 2 7
30AUG2018 4 2
30SEP2018 8 8
30SEP2018 3 3
30SEP2018 7 3
30OCT2018 4 9
30OCT2018 1 5
30OCT2018 2 6
30NOV2018 5 10
30NOV2018 2 4
30NOV2018 3 7
31DEC2018 2 8
31DEC2018 5 9
31DEC2018 8 2
;
run;

data want;
	set test;
	by Date;
	Value_1_times_2 = Value_1 * Value_2;
run;

proc means data=want noprint;
	var Value_1_times_2;
	class Date;
	ways 1;
	output out=want_stat (drop=_:) sum=sum_Value_1_times_2;
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 d’écran 2020-02-27 à 14.23.59.png" style="width: 272px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/36435i6BBB5C52826D31EE/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Capture d’écran 2020-02-27 à 14.23.59.png" alt="Capture d’écran 2020-02-27 à 14.23.59.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture d’écran 2020-02-27 à 14.24.11.png" style="width: 197px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/36436iE4FA1661C5FDB255/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Capture d’écran 2020-02-27 à 14.24.11.png" alt="Capture d’écran 2020-02-27 à 14.24.11.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2020 13:24:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-to-multiply-two-variables-and-sum-result-for-all-records-in/m-p/627891#M185442</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-02-27T13:24:47Z</dc:date>
    </item>
    <item>
      <title>Re: Code to multiply two variables and sum result for all records in each month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-to-multiply-two-variables-and-sum-result-for-all-records-in/m-p/627894#M185443</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    set ac.test;
    value_1_times_2 = value1*value2;
run;
proc summary data=have nway;
    class month;
    var value_1_times_2;
    output out=_sum_ sum=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;No looping needed. This assumes that, as shown in your sample data set, the month values are always the last day of the month. Otherwise, if there are other days in the data set that are not the last day of the month, a simple modification of the code will fix that to get monthly results.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2020 13:28:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-to-multiply-two-variables-and-sum-result-for-all-records-in/m-p/627894#M185443</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-02-27T13:28:23Z</dc:date>
    </item>
    <item>
      <title>Re: Code to multiply two variables and sum result for all records in each month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-to-multiply-two-variables-and-sum-result-for-all-records-in/m-p/627898#M185446</link>
      <description>&lt;P&gt;What&amp;nbsp; you are asking for is the weighted sum of value1, weighted by value2&amp;nbsp; (or value2 weighted by value1) classified&amp;nbsp; by date.&amp;nbsp; PROC SUMMARY can do this, without doing a multiplication step;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary  data=have;
  class date;
  var value1;
  weight value2;
  output out=want sum=value1_time_value2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Feb 2020 13:50:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-to-multiply-two-variables-and-sum-result-for-all-records-in/m-p/627898#M185446</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-02-27T13:50:01Z</dc:date>
    </item>
    <item>
      <title>Re: Code to multiply two variables and sum result for all records in each month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-to-multiply-two-variables-and-sum-result-for-all-records-in/m-p/627910#M185451</link>
      <description>&lt;P&gt;I would add nway, so we only get the group sums:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have nway;
class date;
var value1;
weight value2;
output
  out=want (drop=_type_ _freq_)
  sum=value1_times_2
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Feb 2020 14:15:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-to-multiply-two-variables-and-sum-result-for-all-records-in/m-p/627910#M185451</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-02-27T14:15:25Z</dc:date>
    </item>
    <item>
      <title>Re: Code to multiply two variables and sum result for all records in each month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-to-multiply-two-variables-and-sum-result-for-all-records-in/m-p/627931#M185458</link>
      <description>&lt;P&gt;Thanks for your help! Just as a reference, please can you tell me how the code would change if the dates were not all from the end of the month (i.e. the best way make sure that different dates belong to that one month and in the proc summary, have the results grouped by that one month)?&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2020 15:12:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-to-multiply-two-variables-and-sum-result-for-all-records-in/m-p/627931#M185458</guid>
      <dc:creator>jeremy4</dc:creator>
      <dc:date>2020-02-27T15:12:40Z</dc:date>
    </item>
    <item>
      <title>Re: Code to multiply two variables and sum result for all records in each month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-to-multiply-two-variables-and-sum-result-for-all-records-in/m-p/627950#M185467</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/266226"&gt;@jeremy4&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for your help! Just as a reference, please can you tell me how the code would change if the dates were not all from the end of the month (i.e. the best way make sure that different dates belong to that one month and in the proc summary, have the results grouped by that one month)?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You create a new variable that either contains the last day of a given month&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;period = intnx('month',date,0,'e');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or the period as string&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;period = put(date,yymmn6.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and use that as class variable in proc summary.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2020 15:53:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-to-multiply-two-variables-and-sum-result-for-all-records-in/m-p/627950#M185467</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-02-27T15:53:13Z</dc:date>
    </item>
    <item>
      <title>Re: Code to multiply two variables and sum result for all records in each month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-to-multiply-two-variables-and-sum-result-for-all-records-in/m-p/627954#M185469</link>
      <description>&lt;P&gt;With all due respect to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;, I am not fond of his solution. I prefer to apply a format to the variable DATE. Such as:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;format date yymm.;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Feb 2020 15:59:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-to-multiply-two-variables-and-sum-result-for-all-records-in/m-p/627954#M185469</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-02-27T15:59:27Z</dc:date>
    </item>
    <item>
      <title>Re: Code to multiply two variables and sum result for all records in each month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-to-multiply-two-variables-and-sum-result-for-all-records-in/m-p/629028#M185959</link>
      <description>&lt;P&gt;I like to create a new variable, because it can also be used in a data step BY or in a SQL GROUP BY and yield the same groups. But that's just me.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Mar 2020 06:29:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-to-multiply-two-variables-and-sum-result-for-all-records-in/m-p/629028#M185959</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-03-03T06:29:44Z</dc:date>
    </item>
  </channel>
</rss>

