<?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: Pulling data from individual date ranges in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Pulling-data-from-individual-date-ranges/m-p/742645#M38831</link>
    <description>&lt;P&gt;Thanks Kurt offer the data step,which save my time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data customers;
infile datalines dsd;
input CustomerID :$4. Name :$20. LastYear :mmddyy10. OneYearLater :mmddyy10.;
format lastyear oneyearlater yymmdd10.;
datalines;
1001,Bob Smith,01/17/18,01/17/17
1002,Candice Jones,02/21/18,02/21/17
1003,Jim Stevens,03/31/18,03/31/17
1004,Jane Do,04/16/16,04/16/15
;

data purchases;
input CustomerID :$4. DatePurchased :mmddyy10. PurchaseAmount :dollar5.;
format datepurchased yymmdd10. purchaseamount dollar5.;
datalines;
1001  06/19/17  $35
1001  02/01/17  $10
1001  03/01/18  $25
1003  04/19/17  $15
;
data want;
 if _n_=1 then do;
  if 0 then set purchases;
  declare hash h(dataset:'purchases',hashexp:20);
  h.definekey('CustomerID','DatePurchased');
  h.definedata('PurchaseAmount');
  h.definedone();
 end;
set customers;
TotalPurchased=0;
do i=OneYearLater to LastYear;
  if h.find(key:CustomerID,key:i)=0 then TotalPurchased+PurchaseAmount;
end;
drop i DatePurchased PurchaseAmount;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 20 May 2021 13:07:17 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2021-05-20T13:07:17Z</dc:date>
    <item>
      <title>Pulling data from individual date ranges</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Pulling-data-from-individual-date-ranges/m-p/742475#M38821</link>
      <description>&lt;P&gt;I wish to pull data for individual date ranges. For example, suppose I have this table:&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;CustomerID&lt;/TD&gt;&lt;TD&gt;Name&lt;/TD&gt;&lt;TD&gt;LastYear&lt;/TD&gt;&lt;TD&gt;OneYearLater&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1001&lt;/TD&gt;&lt;TD&gt;Bob Smith&lt;/TD&gt;&lt;TD&gt;01/17/18&lt;/TD&gt;&lt;TD&gt;01/17/17&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1002&lt;/TD&gt;&lt;TD&gt;Candice Jones&lt;/TD&gt;&lt;TD&gt;02/21/18&lt;/TD&gt;&lt;TD&gt;02/21/17&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1003&lt;/TD&gt;&lt;TD&gt;Jim Stevens&lt;/TD&gt;&lt;TD&gt;03/31/18&lt;/TD&gt;&lt;TD&gt;03/31/17&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1004&lt;/TD&gt;&lt;TD&gt;Jane Do&lt;/TD&gt;&lt;TD&gt;04/16/16&lt;/TD&gt;&lt;TD&gt;04/16/15&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;And I want to join this table:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;CustomerID&lt;/TD&gt;&lt;TD&gt;DatePurchased&lt;/TD&gt;&lt;TD&gt;PurchaseAmount&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1001&lt;/TD&gt;&lt;TD&gt;06/19/17&lt;/TD&gt;&lt;TD&gt;$35&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1001&lt;/TD&gt;&lt;TD&gt;02/01/17&lt;/TD&gt;&lt;TD&gt;$10&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1001&lt;/TD&gt;&lt;TD&gt;03/01/18&lt;/TD&gt;&lt;TD&gt;$25&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1003&lt;/TD&gt;&lt;TD&gt;04/19/17&lt;/TD&gt;&lt;TD&gt;$15&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;To get this table:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;CustomerID&lt;/TD&gt;&lt;TD&gt;Name&lt;/TD&gt;&lt;TD&gt;TotalPurchased&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1001&lt;/TD&gt;&lt;TD&gt;Bob Smith&lt;/TD&gt;&lt;TD&gt;$45&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1003&lt;/TD&gt;&lt;TD&gt;Jim Stevens&lt;/TD&gt;&lt;TD&gt;$15&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;How would I do so?&lt;/P&gt;</description>
      <pubDate>Wed, 19 May 2021 17:39:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Pulling-data-from-individual-date-ranges/m-p/742475#M38821</guid>
      <dc:creator>mmaleta851</dc:creator>
      <dc:date>2021-05-19T17:39:37Z</dc:date>
    </item>
    <item>
      <title>Re: Pulling data from individual date ranges</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Pulling-data-from-individual-date-ranges/m-p/742486#M38822</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data customers;
infile datalines dsd;
input CustomerID :$4. Name :$20. LastYear :mmddyy10. OneYearLater :mmddyy10.;
format lastyear oneyearlater yymmdd10.;
datalines;
1001,Bob Smith,01/17/18,01/17/17
1002,Candice Jones,02/21/18,02/21/17
1003,Jim Stevens,03/31/18,03/31/17
1004,Jane Do,04/16/16,04/16/15
;

data purchases;
input CustomerID :$4. DatePurchased :mmddyy10. PurchaseAmount :dollar5.;
format datepurchased yymmdd10. purchaseamount dollar5.;
datalines;
1001  06/19/17  $35
1001  02/01/17  $10
1001  03/01/18  $25
1003  04/19/17  $15
;

proc sql;
create table want as
  select
    c.customerid,
    c.name,
    sum(p.purchaseamount) as totalpurchased format=dollar5.
  from customers c
  inner join purchases p
  on c.customerid = p.customerid and p.datepurchased between c.lastyear and c.oneyearlater
  group by c.customerid, c.name
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note how I presented datasets in data steps with datalines for quick creation on anybody's SAS session. Please do so in the future.&lt;/P&gt;</description>
      <pubDate>Wed, 19 May 2021 18:34:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Pulling-data-from-individual-date-ranges/m-p/742486#M38822</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-05-19T18:34:04Z</dc:date>
    </item>
    <item>
      <title>Re: Pulling data from individual date ranges</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Pulling-data-from-individual-date-ranges/m-p/742645#M38831</link>
      <description>&lt;P&gt;Thanks Kurt offer the data step,which save my time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data customers;
infile datalines dsd;
input CustomerID :$4. Name :$20. LastYear :mmddyy10. OneYearLater :mmddyy10.;
format lastyear oneyearlater yymmdd10.;
datalines;
1001,Bob Smith,01/17/18,01/17/17
1002,Candice Jones,02/21/18,02/21/17
1003,Jim Stevens,03/31/18,03/31/17
1004,Jane Do,04/16/16,04/16/15
;

data purchases;
input CustomerID :$4. DatePurchased :mmddyy10. PurchaseAmount :dollar5.;
format datepurchased yymmdd10. purchaseamount dollar5.;
datalines;
1001  06/19/17  $35
1001  02/01/17  $10
1001  03/01/18  $25
1003  04/19/17  $15
;
data want;
 if _n_=1 then do;
  if 0 then set purchases;
  declare hash h(dataset:'purchases',hashexp:20);
  h.definekey('CustomerID','DatePurchased');
  h.definedata('PurchaseAmount');
  h.definedone();
 end;
set customers;
TotalPurchased=0;
do i=OneYearLater to LastYear;
  if h.find(key:CustomerID,key:i)=0 then TotalPurchased+PurchaseAmount;
end;
drop i DatePurchased PurchaseAmount;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 May 2021 13:07:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Pulling-data-from-individual-date-ranges/m-p/742645#M38831</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-05-20T13:07:17Z</dc:date>
    </item>
    <item>
      <title>Re: Pulling data from individual date ranges</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Pulling-data-from-individual-date-ranges/m-p/742683#M38835</link>
      <description>My apologies. I am new to SAS and don't have a lot of experience creating data steps. The data I am using comes from predefined data that is stored in tables in a database.</description>
      <pubDate>Thu, 20 May 2021 15:37:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Pulling-data-from-individual-date-ranges/m-p/742683#M38835</guid>
      <dc:creator>mmaleta851</dc:creator>
      <dc:date>2021-05-20T15:37:57Z</dc:date>
    </item>
    <item>
      <title>Re: Pulling data from individual date ranges</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Pulling-data-from-individual-date-ranges/m-p/742688#M38836</link>
      <description>&lt;P&gt;Creating datasets on the fly in a data step is a very useful SAS skill on its own. Study the two examples I gave you, as they show how to read not only simple strings and numbers, but also strings containing blanks or special numeric values(dates).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Providing data in this way is helpful in getting quick and correct answers to your questions. The less time we need to spend on setting up data for testing, the more time we have to solve issues.&lt;/P&gt;</description>
      <pubDate>Thu, 20 May 2021 15:48:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Pulling-data-from-individual-date-ranges/m-p/742688#M38836</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-05-20T15:48:52Z</dc:date>
    </item>
  </channel>
</rss>

