<?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: sum values of one variable depending on values of another variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sum-values-of-one-variable-depending-on-values-of-another/m-p/116400#M24010</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;data one;&lt;/P&gt;&lt;P&gt;&amp;nbsp; input upc week units last_qtr_start last_qtr_end;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;10001 1150 10 1127 1149&lt;/P&gt;&lt;P&gt;10001 1151 20 1128 1150&lt;/P&gt;&lt;P&gt;10001 1154 15 1141 1153&lt;/P&gt;&lt;P&gt;10001 1156 12 1143 1155&lt;/P&gt;&lt;P&gt;10002 1300 20 1287 1299&lt;/P&gt;&lt;P&gt;10002 1301 25 1288 1300&lt;/P&gt;&lt;P&gt;10002 1305 20 1292 1304&lt;/P&gt;&lt;P&gt;10002 1330 20 1317 1329 0&lt;/P&gt;&lt;P&gt;10002 1340 25 1327 1339 20&lt;/P&gt;&lt;P&gt;10002 1350 20 1337 1349 25&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp; create table want as&lt;/P&gt;&lt;P&gt;&amp;nbsp; select a.*, coalesce(sum(b.units),0) as last_qtr_sum from one a&lt;/P&gt;&lt;P&gt;left join one b&lt;/P&gt;&lt;P&gt;on a.upc=b.upc&lt;/P&gt;&lt;P&gt;&amp;nbsp; and a.last_qtr_start&amp;lt;=b.week&amp;lt;=a.last_qtr_end&lt;/P&gt;&lt;P&gt;group by a.upc, a.week, a.units, a.last_qtr_start, a.last_qtr_end&lt;/P&gt;&lt;P&gt;;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;UPDATE: Oops, Reeza beats me on Proc SQL version. Here is an alternative data step Hash approach:&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if _n_=1 then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if 0 then set one (rename=(week=_week units=_units) keep=upc week units);&lt;/P&gt;&lt;P&gt;declare hash h(dataset:'one(rename=(week=_week units=_units) keep=upc week units)', multidata:'y');&lt;/P&gt;&lt;P&gt;h.definekey('upc');&lt;/P&gt;&lt;P&gt;h.definedata(all:'y');&lt;/P&gt;&lt;P&gt;h.definedone();&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set one;&lt;/P&gt;&lt;P&gt;&amp;nbsp; do _rc=h.find() by 0 while (_rc=0);&lt;/P&gt;&lt;P&gt;&amp;nbsp; last_qtr_sum=sum(last_qtr_sum,(last_qtr_start &amp;lt;= _week &amp;lt;= last_qtr_end)*_units,0);&lt;/P&gt;&lt;P&gt;_rc=h.find_next();&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;drop _:;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 18 Oct 2013 18:04:43 GMT</pubDate>
    <dc:creator>Haikuo</dc:creator>
    <dc:date>2013-10-18T18:04:43Z</dc:date>
    <item>
      <title>sum values of one variable depending on values of another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-values-of-one-variable-depending-on-values-of-another/m-p/116395#M24005</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a dataset as below, and for each UPC each week, I need to calculate the sum of units of the last quarter, meaning weeks between last_qtr_start and last_qtr_end. I thought proc sql would work for this, but after hours of trying, I didn't get any luck.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: white;"&gt;Any help is greatly appreciated!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;STRONG style="color: navy; background: white; font-size: 10.0pt; font-family: 'Courier New';"&gt;data&lt;/STRONG&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: white;"&gt; one;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&amp;nbsp; &lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: blue; background: white;"&gt;input&lt;/SPAN&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: white;"&gt; upc week units last_qtr_start last_qtr_end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: blue; background: white;"&gt;datalines&lt;/SPAN&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;10001 1150 10 1127 1149&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;10001 1151 20 1128 1150&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;10001 1154 15 1141 1153&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;10001 1156 12 1143 1155&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;10002 1300 20 1287 1299&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;10002 1301 25 1288 1300&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;10002 1305 20 1292 1304&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;STRONG style="color: navy; background: white; font-size: 10.0pt; font-family: 'Courier New';"&gt;run&lt;/STRONG&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: white;"&gt;And the output should look like:&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;STRONG style="color: navy; background: white; font-size: 10.0pt; font-family: 'Courier New';"&gt;data&lt;/STRONG&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: white;"&gt; one;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&amp;nbsp; &lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: blue; background: white;"&gt;input&lt;/SPAN&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: white;"&gt; upc week units last_qtr_start last_qtr_end last_qtr_sum;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: blue; background: white;"&gt;datalines&lt;/SPAN&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;10001 1150 10 1127 1149 0&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;10001 1151 20 1128 1150 10&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;10001 1154 15 1141 1153 30&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;10001 1156 12 1143 1155 45&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;10002 1300 20 1287 1299 0&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;10002 1301 25 1288 1300 25&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;10002 1305 20 1292 1304 45&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;STRONG style="color: navy; background: white; font-size: 10.0pt; font-family: 'Courier New';"&gt;run&lt;/STRONG&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: white;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 18 Oct 2013 15:44:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-values-of-one-variable-depending-on-values-of-another/m-p/116395#M24005</guid>
      <dc:creator>gooday</dc:creator>
      <dc:date>2013-10-18T15:44:05Z</dc:date>
    </item>
    <item>
      <title>Re: sum values of one variable depending on values of another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-values-of-one-variable-depending-on-values-of-another/m-p/116396#M24006</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;data one;&lt;/P&gt;&lt;P&gt;&amp;nbsp; input upc week units last_qtr_start last_qtr_end;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;10001 1150 10 1127 1149&lt;/P&gt;&lt;P&gt;10001 1151 20 1128 1150&lt;/P&gt;&lt;P&gt;10001 1154 15 1141 1153&lt;/P&gt;&lt;P&gt;10001 1156 12 1143 1155&lt;/P&gt;&lt;P&gt;10002 1300 20 1287 1299&lt;/P&gt;&lt;P&gt;10002 1301 25 1288 1300&lt;/P&gt;&lt;P&gt;10002 1305 20 1292 1304&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set one;&lt;/P&gt;&lt;P&gt;&amp;nbsp; by upc notsorted;&lt;/P&gt;&lt;P&gt;&amp;nbsp; _var+units;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if first.upc then last_qtr_sum=0;else last_qtr_sum=_var-units;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if last.upc then _var=0;&lt;/P&gt;&lt;P&gt;&amp;nbsp; drop _var;&lt;/P&gt;&lt;P&gt;&amp;nbsp; run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 18 Oct 2013 16:03:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-values-of-one-variable-depending-on-values-of-another/m-p/116396#M24006</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2013-10-18T16:03:35Z</dc:date>
    </item>
    <item>
      <title>Re: sum values of one variable depending on values of another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-values-of-one-variable-depending-on-values-of-another/m-p/116397#M24007</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The code of Hai.Kuo works well, i just gave an alternative one&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set one;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; retain val;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; by upc;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if first.upc then val=units;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else val+units;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if first.upc then&amp;nbsp; last_qtr_sum=0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else last_qtr_sum=val-units;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; drop val;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;a correction in the output, it should be 20 i believe instead of 25, please check&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;10002 1301 25 1288 1300 &lt;SPAN style="text-decoration: line-through;"&gt;25&lt;/SPAN&gt; 20 &lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Jagadish&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 18 Oct 2013 16:25:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-values-of-one-variable-depending-on-values-of-another/m-p/116397#M24007</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2013-10-18T16:25:22Z</dc:date>
    </item>
    <item>
      <title>Re: sum values of one variable depending on values of another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-values-of-one-variable-depending-on-values-of-another/m-p/116398#M24008</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New';"&gt;Thank so much Jagadish and Hai.Kuo! Sorry that i didn't explain the question clearly. For the same upc and week, the last_qtr_sum calculation is conditioned by &lt;SPAN style="color: black; background: white;"&gt;&lt;SPAN&gt;last_qtr_start last_qtr_end, i.e., last_qtr_sum is the sum of units of the same upc but weeks have to be between last_qtr_start and last_qtr_end. &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: black; background: white; font-size: 11.0pt; font-family: 'Courier New';"&gt;i modified my example by adding 3 rows at the bottom to show the difference. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: black; background: white; font-size: 11.0pt; font-family: 'Courier New';"&gt;btw, your correction of the output is correct,&lt;/SPAN&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New';"&gt;Jagadish, thank you!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New';"&gt;data one;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New';"&gt;&amp;nbsp; input upc week units last_qtr_start last_qtr_end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New';"&gt;datalines;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New';"&gt;10001 1150 10 1127 1149&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New';"&gt;10001 1151 20 1128 1150&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New';"&gt;10001 1154 15 1141 1153&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New';"&gt;10001 1156 12 1143 1155&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New';"&gt;10002 1300 20 1287 1299&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New';"&gt;10002 1301 25 1288 1300&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New';"&gt;10002 1305 20 1292 1304&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New';"&gt;10002 1330 20 1317 1329&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New';"&gt;10002 1340 25 1327 1339&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New';"&gt;10002 1350 20 1337 1349&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New';"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New';"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New'; color: black; background: white;"&gt;And the output should look like:&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;STRONG style="color: navy; background: white; font-size: 11.0pt; font-family: 'Courier New';"&gt;data&lt;/STRONG&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New'; color: black; background: white;"&gt; two;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New';"&gt;&amp;nbsp; &lt;SPAN style="color: blue; background: white;"&gt;input&lt;/SPAN&gt;&lt;SPAN style="color: black; background: white;"&gt; upc week units last_qtr_start last_qtr_end last_qtr_sum;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New'; color: blue; background: white;"&gt;datalines&lt;/SPAN&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;10001 1150 10 1127 1149 0&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;10001 1151 20 1128 1150 10&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;10001 1154 15 1141 1153 30&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;10001 1156 12 1143 1155 45&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;10002 1300 20 1287 1299 0&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;10002 1301 25 1288 1300 20&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;10002 1305 20 1292 1304 45&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New';"&gt;10002 1330 20 1317 1329 0&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New';"&gt;10002 1340 25 1327 1339 20&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New';"&gt;10002 1350 20 1337 1349 25&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;STRONG style="color: navy; background: white; font-size: 11.0pt; font-family: 'Courier New';"&gt;run&lt;/STRONG&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 18 Oct 2013 17:32:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-values-of-one-variable-depending-on-values-of-another/m-p/116398#M24008</guid>
      <dc:creator>gooday</dc:creator>
      <dc:date>2013-10-18T17:32:24Z</dc:date>
    </item>
    <item>
      <title>Re: sum values of one variable depending on values of another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-values-of-one-variable-depending-on-values-of-another/m-p/116399#M24009</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Highly inefficient but this may work:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; create table want as &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; select distinct a.*, coalesce(sum(b.units), 0) as total_units&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; from one as a&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; left join one as b&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; on b.week between a.last_qtr_start and a.last_qtr_end&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; group by a.upc, a.week;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 18 Oct 2013 18:00:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-values-of-one-variable-depending-on-values-of-another/m-p/116399#M24009</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2013-10-18T18:00:15Z</dc:date>
    </item>
    <item>
      <title>Re: sum values of one variable depending on values of another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-values-of-one-variable-depending-on-values-of-another/m-p/116400#M24010</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;data one;&lt;/P&gt;&lt;P&gt;&amp;nbsp; input upc week units last_qtr_start last_qtr_end;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;10001 1150 10 1127 1149&lt;/P&gt;&lt;P&gt;10001 1151 20 1128 1150&lt;/P&gt;&lt;P&gt;10001 1154 15 1141 1153&lt;/P&gt;&lt;P&gt;10001 1156 12 1143 1155&lt;/P&gt;&lt;P&gt;10002 1300 20 1287 1299&lt;/P&gt;&lt;P&gt;10002 1301 25 1288 1300&lt;/P&gt;&lt;P&gt;10002 1305 20 1292 1304&lt;/P&gt;&lt;P&gt;10002 1330 20 1317 1329 0&lt;/P&gt;&lt;P&gt;10002 1340 25 1327 1339 20&lt;/P&gt;&lt;P&gt;10002 1350 20 1337 1349 25&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp; create table want as&lt;/P&gt;&lt;P&gt;&amp;nbsp; select a.*, coalesce(sum(b.units),0) as last_qtr_sum from one a&lt;/P&gt;&lt;P&gt;left join one b&lt;/P&gt;&lt;P&gt;on a.upc=b.upc&lt;/P&gt;&lt;P&gt;&amp;nbsp; and a.last_qtr_start&amp;lt;=b.week&amp;lt;=a.last_qtr_end&lt;/P&gt;&lt;P&gt;group by a.upc, a.week, a.units, a.last_qtr_start, a.last_qtr_end&lt;/P&gt;&lt;P&gt;;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;UPDATE: Oops, Reeza beats me on Proc SQL version. Here is an alternative data step Hash approach:&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if _n_=1 then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if 0 then set one (rename=(week=_week units=_units) keep=upc week units);&lt;/P&gt;&lt;P&gt;declare hash h(dataset:'one(rename=(week=_week units=_units) keep=upc week units)', multidata:'y');&lt;/P&gt;&lt;P&gt;h.definekey('upc');&lt;/P&gt;&lt;P&gt;h.definedata(all:'y');&lt;/P&gt;&lt;P&gt;h.definedone();&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set one;&lt;/P&gt;&lt;P&gt;&amp;nbsp; do _rc=h.find() by 0 while (_rc=0);&lt;/P&gt;&lt;P&gt;&amp;nbsp; last_qtr_sum=sum(last_qtr_sum,(last_qtr_start &amp;lt;= _week &amp;lt;= last_qtr_end)*_units,0);&lt;/P&gt;&lt;P&gt;_rc=h.find_next();&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;drop _:;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 18 Oct 2013 18:04:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-values-of-one-variable-depending-on-values-of-another/m-p/116400#M24010</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2013-10-18T18:04:43Z</dc:date>
    </item>
    <item>
      <title>Re: sum values of one variable depending on values of another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-values-of-one-variable-depending-on-values-of-another/m-p/116401#M24011</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks so much Reeza. This is very helpful! In the left join step, we do need to specify a.upc=b.upc, otherwise the last_qtr_sum will be messed up.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks again!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 18 Oct 2013 19:20:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-values-of-one-variable-depending-on-values-of-another/m-p/116401#M24011</guid>
      <dc:creator>gooday</dc:creator>
      <dc:date>2013-10-18T19:20:50Z</dc:date>
    </item>
    <item>
      <title>Re: sum values of one variable depending on values of another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-values-of-one-variable-depending-on-values-of-another/m-p/116402#M24012</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks so much Hai Kuo! I've applied your sql version and it worked great! You rock!!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 18 Oct 2013 19:21:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-values-of-one-variable-depending-on-values-of-another/m-p/116402#M24012</guid>
      <dc:creator>gooday</dc:creator>
      <dc:date>2013-10-18T19:21:47Z</dc:date>
    </item>
  </channel>
</rss>

