<?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: Set One row Category to the Sum of Another in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Set-One-row-Category-to-the-Sum-of-Another/m-p/441111#M69245</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/5059"&gt;@DavidPhillips2&lt;/a&gt;&amp;nbsp;&amp;nbsp; Your program is not the shortest, but more importantly, it must have a problem somewhere.&amp;nbsp; I ran it and it produces a sum of salaries of 80,000 for contract length 9, but I see only 3 observations with contract length 9&amp;nbsp; (10000, 20000, and 20000).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As to shortest, I believe my single data step qualifies.&amp;nbsp; And now, then I edited it, it actually works.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 01 Mar 2018 04:05:20 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2018-03-01T04:05:20Z</dc:date>
    <item>
      <title>Set One row Category to the Sum of Another</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Set-One-row-Category-to-the-Sum-of-Another/m-p/439865#M69168</link>
      <description>&lt;P&gt;I have two sets of rows in this table.&amp;nbsp; The general rows and a redundant total row.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a simple way to do this?&amp;nbsp; I can think of a few that involve 10 lines of code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data merged_SAL; set merged_SAL;&lt;BR /&gt; if CONTRACT_LENGTH = ('9 10 11 12') then do;&lt;BR /&gt;SALARY_OUTLAYS = /*sum of salary_outlays where CONTRACT_LENGTH in ('9', '10', '11', '12') */&lt;BR /&gt; end;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Feb 2018 21:28:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Set-One-row-Category-to-the-Sum-of-Another/m-p/439865#M69168</guid>
      <dc:creator>DavidPhillips2</dc:creator>
      <dc:date>2018-02-23T21:28:23Z</dc:date>
    </item>
    <item>
      <title>Re: Set One row Category to the Sum of Another</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Set-One-row-Category-to-the-Sum-of-Another/m-p/439868#M69169</link>
      <description>&lt;P&gt;Note in an example like this:&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; test&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token statement"&gt;infile&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;cards&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;input&lt;/SPAN&gt; A1 A2 A3 A4 A5&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token keyword"&gt;cards&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;SPAN class="token data string"&gt;
2 2 5 1 0
&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;

&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; sums&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
 &lt;SPAN class="token keyword"&gt;set&lt;/SPAN&gt; test&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;

 &lt;SPAN class="token comment"&gt;/* method 1: array of all numerics */&lt;/SPAN&gt;
 &lt;SPAN class="token statement"&gt;array&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;n&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;{&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;}&lt;/SPAN&gt; _numeric_&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
 sum_allnum&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;sum&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;of &lt;SPAN class="token function"&gt;n&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;[&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;]&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;

 &lt;SPAN class="token comment"&gt;/*method 2: variable range in data set */&lt;/SPAN&gt;
 sum_range&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;sum&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;of a1&lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt;a5&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The author is summing across columns.&amp;nbsp; I am suming across rows.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Feb 2018 21:31:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Set-One-row-Category-to-the-Sum-of-Another/m-p/439868#M69169</guid>
      <dc:creator>DavidPhillips2</dc:creator>
      <dc:date>2018-02-23T21:31:29Z</dc:date>
    </item>
    <item>
      <title>Re: Set One row Category to the Sum of Another</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Set-One-row-Category-to-the-Sum-of-Another/m-p/439890#M69170</link>
      <description>&lt;P&gt;Here is one way with SQL. You haven't really explained your problem completely - it would help if you supplied sample input and output data - so I'm guessing this is close to what you want. If you don't want sums by contract_length take out the GROUP BY.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table want as
  select A.*
           ,B.salary_outlays_sum
  from merged_sal as A
  left join
  (select contract_length
             ,sum(salary_outlays) as salary_outlays_sum
   from  merged_sal
  where contract_length in ('9','10','11','12')
  group by contract_length
  ) as B
  on A.contract_length = B.contract_length
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Feb 2018 23:36:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Set-One-row-Category-to-the-Sum-of-Another/m-p/439890#M69170</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2018-02-23T23:36:43Z</dc:date>
    </item>
    <item>
      <title>Re: Set One row Category to the Sum of Another</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Set-One-row-Category-to-the-Sum-of-Another/m-p/439996#M69179</link>
      <description>&lt;P&gt;You are summing over rows.&amp;nbsp; That much is obvious.&amp;nbsp; Are you saying you want to PRODUCE a redundant ROW, which is the sum of the contract_lengths between 9 and 12?&amp;nbsp; Or are you producing a new column with row&amp;nbsp;sums for contact lengths 9, 10, 11, and 12, respectively.&amp;nbsp; I.e. if the individual contract is 9, you want to add a&amp;nbsp;SALARY_OUTLAYS_SUM variable summing&amp;nbsp;all length 9 contracts.&amp;nbsp; That's what &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13976"&gt;@SASKiwi&lt;/a&gt;&amp;nbsp;did in SQL.&amp;nbsp; Here's how in a data step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set sal_merged (in=prelim where=(9&amp;lt;=contract_length&amp;lt;=12))
      sal_merged (in=inkeep);
  array tmpsum {9:12} _temporary_;
  if prelim then tmpsum{contract_length}+salary_outlays;
  if inkeep;
  if (9&amp;lt;=contract_length&amp;lt;=12) then salary_outlays_sum=tmpsum{contract_length};
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I assume that contract_length is a numeric variable.&amp;nbsp; The SET statement reads the data set twice, first to generate sums in a temporary array, and the second to output while retrieve sums from the same array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 04:06:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Set-One-row-Category-to-the-Sum-of-Another/m-p/439996#M69179</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-03-01T04:06:12Z</dc:date>
    </item>
    <item>
      <title>Re: Set One row Category to the Sum of Another</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Set-One-row-Category-to-the-Sum-of-Another/m-p/440187#M69189</link>
      <description>&lt;P&gt;Sample data is below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data sal_merged;&lt;/P&gt;
&lt;P&gt;infile datalines;&lt;/P&gt;
&lt;P&gt;input identifer contract_length salary_outlays ;&lt;/P&gt;
&lt;P&gt;return;&lt;/P&gt;
&lt;P&gt;datalines;&lt;/P&gt;
&lt;P&gt;1 9 10000&lt;/P&gt;
&lt;P&gt;2 9 20000&lt;/P&gt;
&lt;P&gt;3 10 10000&lt;/P&gt;
&lt;P&gt;4 11 30000&lt;/P&gt;
&lt;P&gt;5 12 10000&lt;/P&gt;
&lt;P&gt;6 9 20000&lt;/P&gt;
&lt;P&gt;7 11 30000&lt;/P&gt;
&lt;P&gt;8 10 30000&lt;/P&gt;
&lt;P&gt;9 12 20000&lt;/P&gt;
&lt;P&gt;10 13 40000&lt;/P&gt;
&lt;P&gt;11 13 40000&lt;/P&gt;
&lt;P&gt;12 13 20000&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I’m trying to sum the values of contact lengths 9-12 as the value for contract length 13.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2018 14:58:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Set-One-row-Category-to-the-Sum-of-Another/m-p/440187#M69189</guid>
      <dc:creator>DavidPhillips2</dc:creator>
      <dc:date>2018-02-26T14:58:12Z</dc:date>
    </item>
    <item>
      <title>Re: Set One row Category to the Sum of Another</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Set-One-row-Category-to-the-Sum-of-Another/m-p/440273#M69196</link>
      <description>&lt;P&gt;This works.&amp;nbsp; Is there a shorter solution?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data sal_merged;&lt;BR /&gt; infile datalines;&lt;BR /&gt; input identifer contract_length salary_outlays gender $1.;&lt;BR /&gt;return;&lt;BR /&gt;datalines;&lt;BR /&gt;1 9 10000 M&lt;BR /&gt;2 9 20000 M&lt;BR /&gt;3 10 10000 M&lt;BR /&gt;4 11 30000 M&lt;BR /&gt;5 12 10000 M&lt;BR /&gt;6 9 20000 F&lt;BR /&gt;7 11 30000 F&lt;BR /&gt;8 10 30000 F&lt;BR /&gt;9 12 20000 F&lt;BR /&gt;10 13 . F&lt;BR /&gt;11 13 . M&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt; create table tempSalaryOutlays as&lt;BR /&gt; select gender&lt;BR /&gt; ,sum(salary_outlays) as salary_outlays_sum&lt;BR /&gt; from sal_merged&lt;BR /&gt; where contract_length in (9,10,11,12)&lt;BR /&gt; group by gender;&lt;BR /&gt; &lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;proc sort data=sal_merged;&lt;BR /&gt; by gender;&lt;BR /&gt;run;&lt;BR /&gt;proc sort data=tempSalaryOutlays;&lt;BR /&gt; by gender;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data sal_merged; merge sal_merged (in=x) tempSalaryOutlays;&lt;BR /&gt; by gender;&lt;BR /&gt; if contract_length = 13 then salary_outlays = salary_outlays_sum;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2018 17:33:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Set-One-row-Category-to-the-Sum-of-Another/m-p/440273#M69196</guid>
      <dc:creator>DavidPhillips2</dc:creator>
      <dc:date>2018-02-26T17:33:34Z</dc:date>
    </item>
    <item>
      <title>Re: Set One row Category to the Sum of Another</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Set-One-row-Category-to-the-Sum-of-Another/m-p/441111#M69245</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/5059"&gt;@DavidPhillips2&lt;/a&gt;&amp;nbsp;&amp;nbsp; Your program is not the shortest, but more importantly, it must have a problem somewhere.&amp;nbsp; I ran it and it produces a sum of salaries of 80,000 for contract length 9, but I see only 3 observations with contract length 9&amp;nbsp; (10000, 20000, and 20000).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As to shortest, I believe my single data step qualifies.&amp;nbsp; And now, then I edited it, it actually works.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 04:05:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Set-One-row-Category-to-the-Sum-of-Another/m-p/441111#M69245</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-03-01T04:05:20Z</dc:date>
    </item>
    <item>
      <title>Re: Set One row Category to the Sum of Another</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Set-One-row-Category-to-the-Sum-of-Another/m-p/441207#M69249</link>
      <description>&lt;P&gt;The total is correct.&amp;nbsp; Note, the program updates salary outlays.&amp;nbsp; You can drop the salary_outlays_sum column at the end if you want to.&amp;nbsp; I left it in for edit check reasons.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I modified your program slightly to update contract length 13 only.&amp;nbsp; I'm trying to figure out how to use your program to do the same split out by gender like the code I posted earlier.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;At the end of the below code is a dataset that includes gender.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data sal_merged;&lt;/P&gt;
&lt;P&gt;infile datalines;&lt;/P&gt;
&lt;P&gt;input identifer contract_length salary_outlays ;&lt;/P&gt;
&lt;P&gt;return;&lt;/P&gt;
&lt;P&gt;datalines;&lt;/P&gt;
&lt;P&gt;1 9 10000&lt;/P&gt;
&lt;P&gt;2 9 20000&lt;/P&gt;
&lt;P&gt;3 10 10000&lt;/P&gt;
&lt;P&gt;4 11 30000&lt;/P&gt;
&lt;P&gt;5 12 10000&lt;/P&gt;
&lt;P&gt;6 9 20000&lt;/P&gt;
&lt;P&gt;7 11 30000&lt;/P&gt;
&lt;P&gt;8 10 30000&lt;/P&gt;
&lt;P&gt;9 12 20000&lt;/P&gt;
&lt;P&gt;10 13 40000&lt;/P&gt;
&lt;P&gt;11 13 40000&lt;/P&gt;
&lt;P&gt;12 13 20000&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt; set sal_merged (in=prelim where=(9&amp;lt;=contract_length&amp;lt;=12))&lt;BR /&gt; sal_merged (in=inkeep);&lt;BR /&gt; array tmpsum {9:12} _temporary_;&lt;BR /&gt; if prelim then tmpsum{contract_length}+salary_outlays;&lt;BR /&gt; if inkeep;&lt;BR /&gt; if (contract_length=13) then salary_outlays=tmpsum{9} + tmpsum{10} + tmpsum{11} +tmpsum{12};&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data sal_merged;&lt;BR /&gt;infile datalines;&lt;BR /&gt;input identifer contract_length salary_outlays gender $1.;&lt;BR /&gt;return;&lt;BR /&gt;datalines;&lt;BR /&gt;1 9 10000 M&lt;BR /&gt;2 9 20000 M&lt;BR /&gt;3 10 10000 M&lt;BR /&gt;4 11 30000 M&lt;BR /&gt;5 12 10000 M&lt;BR /&gt;6 9 20000 F&lt;BR /&gt;7 11 30000 F&lt;BR /&gt;8 10 30000 F&lt;BR /&gt;9 12 20000 F&lt;BR /&gt;10 13 . F&lt;BR /&gt;11 13 . M&lt;BR /&gt;;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 15:02:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Set-One-row-Category-to-the-Sum-of-Another/m-p/441207#M69249</guid>
      <dc:creator>DavidPhillips2</dc:creator>
      <dc:date>2018-03-01T15:02:45Z</dc:date>
    </item>
    <item>
      <title>Re: Set One row Category to the Sum of Another</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Set-One-row-Category-to-the-Sum-of-Another/m-p/441565#M69256</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/5059"&gt;@DavidPhillips2&lt;/a&gt;&amp;nbsp;&amp;nbsp; Ah, it was my misinterpretation.&amp;nbsp; Thanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As to modification for gender ... If the dataset is sorted by gender, you can utilize the BY statement (untested):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; set salmerged (where=(9&amp;lt;=contract_length&amp;lt;=12) in=prelim)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; salmerged;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; by descending gender;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; array tmpsum{9:12} _temporary_;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; if first.gender then call missing(of tmpsum{*});&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; if prelim then&amp;nbsp;tmpsum{contract_length}+salary_outlays;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;if inkeep;&lt;BR /&gt;&amp;nbsp; if (contract_length=13) then salary_outlays=sum(of tmpsum{*});&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Mar 2018 13:53:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Set-One-row-Category-to-the-Sum-of-Another/m-p/441565#M69256</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-03-02T13:53:56Z</dc:date>
    </item>
  </channel>
</rss>

