<?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: A loop in SAS GUIDE to get information on table according to a matrix table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/A-loop-in-SAS-GUIDE-to-get-information-on-table-according-to-a/m-p/733799#M228649</link>
    <description>EXPECTED OUTPUT FOR %DDC (K1);&lt;BR /&gt;&lt;BR /&gt;TABLE PURCHASES_1 ( PURCHASES_&amp;amp;MASTER_ID):&lt;BR /&gt;CLIENT_ID, CLIENT_NAME, PRODUCT for all customers who purchased "PROD_88T" between "01/01/2021" and "05/01/2021"&lt;BR /&gt;&lt;BR /&gt;TABLE PURCHASES_2 ( PURCHASES_&amp;amp;MASTER_ID):&lt;BR /&gt;CLIENT_ID, CLIENT_NAME, PRODUCT for all customers who purchased " PROD_55X" between "20/12/2020" and "20/02/2021"&lt;BR /&gt;&lt;BR /&gt;TABLE PURCHASES_4 ( PURCHASES_&amp;amp;MASTER_ID):&lt;BR /&gt;CLIENT_ID, CLIENT_NAME, PRODUCT for all customers who purchased " PROD_95Y" between "01/04/2021" and "14/04/2021"</description>
    <pubDate>Wed, 14 Apr 2021 15:20:45 GMT</pubDate>
    <dc:creator>Neeydchi</dc:creator>
    <dc:date>2021-04-14T15:20:45Z</dc:date>
    <item>
      <title>A loop in SAS GUIDE to get information on table according to a matrix table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/A-loop-in-SAS-GUIDE-to-get-information-on-table-according-to-a/m-p/733776#M228641</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to select the customers who made purchases according to the criteria of each of my MASTER_ID -and save it in a table for each of my MASTER_ID&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;I have the following&amp;nbsp; matrix (MATRIX_TABLE):&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;MASTER_ID&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;CAMPAIGNS&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;PRODUCT&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;START_DATE&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;END_DATE&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;K1&lt;/TD&gt;&lt;TD&gt;PROD_88T&lt;/TD&gt;&lt;TD&gt;01/01/2021&lt;/TD&gt;&lt;TD&gt;05/01/2021&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;k1&lt;/TD&gt;&lt;TD&gt;PROD_55X&lt;/TD&gt;&lt;TD&gt;20/12/2020&lt;/TD&gt;&lt;TD&gt;20/02/2021&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;k2&lt;/TD&gt;&lt;TD&gt;PROD_75A&lt;/TD&gt;&lt;TD&gt;15/01/2021&lt;/TD&gt;&lt;TD&gt;15/03/2021&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;K1&lt;/TD&gt;&lt;TD&gt;PROD_95Y&lt;/TD&gt;&lt;TD&gt;01/04/2021&lt;/TD&gt;&lt;TD&gt;14/04/2021&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;OBS: The same &lt;SPAN&gt;campaign&lt;/SPAN&gt;&amp;nbsp;can be repeated in several lines, since the same &lt;SPAN&gt;campaign&lt;/SPAN&gt;&amp;nbsp;can encompass several different products and different start and end dates.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;And I have the following table that lists all the purchases my customers made during my campaigns.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;PURCHASES ( PURCHASES_TABLE)&lt;/STRONG&gt;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;CLIENT_ID&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;CLIENT_NAME&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;PRODUCT&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;AMOUNT_PURCHASE&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;PURCHASE_DATE&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;CLI_1&lt;/TD&gt;&lt;TD&gt;CLIENT 1&lt;/TD&gt;&lt;TD&gt;PROD_95Y&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;TD&gt;01/10/2020&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;CLI_2&lt;/TD&gt;&lt;TD&gt;CLIENT 2&lt;/TD&gt;&lt;TD&gt;PROD_88T&lt;/TD&gt;&lt;TD&gt;300&lt;/TD&gt;&lt;TD&gt;15/02/2021&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;CLI_3&lt;/TD&gt;&lt;TD&gt;CLIENT 3&lt;/TD&gt;&lt;TD&gt;PROD_95Y&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;11/01/2021&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My code runs, but I'm not getting my expected result. I generate three tables (PURCHASES_ &amp;amp; MASTER_ID), but the results are the same in the three tables. That is, it does not select the criteria according to each MASTER_ID&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my attempt to get the purchases of the K1 Campaign:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%global &lt;SPAN&gt;campaign&lt;/SPAN&gt;;

%macro DDC (&lt;SPAN&gt;campaign&lt;/SPAN&gt;);
PROC SQL noprint;

SELECT count(*)
INTO :n_var
FROM MATRIX_TABLE;
QUIT;

%DO I = 1 %TO &amp;amp;n_var;

data _null_;
set WORK.MATRIX_TABLE;

if _n_= &amp;amp;I;
call symput('&lt;SPAN&gt;campaign&lt;/SPAN&gt;', &lt;SPAN&gt;campaign&lt;/SPAN&gt;);
call symput('MASTER_ID', MASTER_ID);

run;

PROC SQL;
CREATE TABLE PURCHASES_&amp;amp;MASTER_ID AS
SELECT t2.CLIENT_ID, T2.CLIENT_NAME, t1.PRODUCT
FROM matrix_table t1, PURCHASES_TABLE T2
WHERE t1.PRODUCT = T2.PRODUCT
and t2.PURCHASE_DATE &amp;gt;= t1.START_DATE
and t2.PURCHASE_DATE &amp;lt;= t1.END_DATE;
QUIT;

%end;

%mend DDC;

%DDC (K1);

&lt;/CODE&gt;&lt;/PRE&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;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Apr 2021 14:57:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/A-loop-in-SAS-GUIDE-to-get-information-on-table-according-to-a/m-p/733776#M228641</guid>
      <dc:creator>Neeydchi</dc:creator>
      <dc:date>2021-04-14T14:57:24Z</dc:date>
    </item>
    <item>
      <title>Re: A loop in SAS GUIDE to get information on table according to a matrix table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/A-loop-in-SAS-GUIDE-to-get-information-on-table-according-to-a/m-p/733785#M228643</link>
      <description>&lt;P&gt;Can you state the criteria in words and show what you expect for the output given the example data?&lt;/P&gt;</description>
      <pubDate>Wed, 14 Apr 2021 15:03:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/A-loop-in-SAS-GUIDE-to-get-information-on-table-according-to-a/m-p/733785#M228643</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-04-14T15:03:09Z</dc:date>
    </item>
    <item>
      <title>Re: A loop in SAS GUIDE to get information on table according to a matrix table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/A-loop-in-SAS-GUIDE-to-get-information-on-table-according-to-a/m-p/733799#M228649</link>
      <description>EXPECTED OUTPUT FOR %DDC (K1);&lt;BR /&gt;&lt;BR /&gt;TABLE PURCHASES_1 ( PURCHASES_&amp;amp;MASTER_ID):&lt;BR /&gt;CLIENT_ID, CLIENT_NAME, PRODUCT for all customers who purchased "PROD_88T" between "01/01/2021" and "05/01/2021"&lt;BR /&gt;&lt;BR /&gt;TABLE PURCHASES_2 ( PURCHASES_&amp;amp;MASTER_ID):&lt;BR /&gt;CLIENT_ID, CLIENT_NAME, PRODUCT for all customers who purchased " PROD_55X" between "20/12/2020" and "20/02/2021"&lt;BR /&gt;&lt;BR /&gt;TABLE PURCHASES_4 ( PURCHASES_&amp;amp;MASTER_ID):&lt;BR /&gt;CLIENT_ID, CLIENT_NAME, PRODUCT for all customers who purchased " PROD_95Y" between "01/04/2021" and "14/04/2021"</description>
      <pubDate>Wed, 14 Apr 2021 15:20:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/A-loop-in-SAS-GUIDE-to-get-information-on-table-according-to-a/m-p/733799#M228649</guid>
      <dc:creator>Neeydchi</dc:creator>
      <dc:date>2021-04-14T15:20:45Z</dc:date>
    </item>
    <item>
      <title>Re: A loop in SAS GUIDE to get information on table according to a matrix table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/A-loop-in-SAS-GUIDE-to-get-information-on-table-according-to-a/m-p/733802#M228652</link>
      <description>&lt;P&gt;First, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; is asking what you want the data sets to look like. Second, your CALL SYMPUT has trailing blanks when I run it, unless I made a mistake somewhere. Third, is there an actual difference between 'k1' and 'K1'? Fourth, is there a reason you just have a numeric identifier for the data sets as opposed to the campaigns identifier?&lt;BR /&gt;&lt;BR /&gt;I'm thinking that this can be streamlined and more data-driven, but there are a lot of unknowns out there.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Apr 2021 15:27:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/A-loop-in-SAS-GUIDE-to-get-information-on-table-according-to-a/m-p/733802#M228652</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2021-04-14T15:27:56Z</dc:date>
    </item>
    <item>
      <title>Re: A loop in SAS GUIDE to get information on table according to a matrix table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/A-loop-in-SAS-GUIDE-to-get-information-on-table-according-to-a/m-p/733807#M228655</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/375668"&gt;@Neeydchi&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;EXPECTED OUTPUT FOR %DDC (K1);&lt;BR /&gt;&lt;BR /&gt;TABLE PURCHASES_1 ( PURCHASES_&amp;amp;MASTER_ID):&lt;BR /&gt;CLIENT_ID, CLIENT_NAME, PRODUCT for all customers who purchased "PROD_88T" between "01/01/2021" and "05/01/2021"&lt;BR /&gt;&lt;BR /&gt;TABLE PURCHASES_2 ( PURCHASES_&amp;amp;MASTER_ID):&lt;BR /&gt;CLIENT_ID, CLIENT_NAME, PRODUCT for all customers who purchased " PROD_55X" between "20/12/2020" and "20/02/2021"&lt;BR /&gt;&lt;BR /&gt;TABLE PURCHASES_4 ( PURCHASES_&amp;amp;MASTER_ID):&lt;BR /&gt;CLIENT_ID, CLIENT_NAME, PRODUCT for all customers who purchased " PROD_95Y" between "01/04/2021" and "14/04/2021"&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Since you are showing dates with quotes then that appears the "date" is a character value and "between" means alphabetically not chronologically. At least from what you show. So between "20/12/2020" and "20/02/2021" does not include '01/01/2021' because '01' is not greater than "20" at the start of the string. If your data has character values for dates then the first step is convert them to SAS data values.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Apr 2021 15:49:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/A-loop-in-SAS-GUIDE-to-get-information-on-table-according-to-a/m-p/733807#M228655</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-04-14T15:49:08Z</dc:date>
    </item>
    <item>
      <title>Re: A loop in SAS GUIDE to get information on table according to a matrix table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/A-loop-in-SAS-GUIDE-to-get-information-on-table-according-to-a/m-p/733809#M228657</link>
      <description>I would have expected to see a filter somewhere in that query for the campaign, K1, but I don't. &lt;BR /&gt;&lt;BR /&gt;Can you show a non macro query that does generate what you want?</description>
      <pubDate>Wed, 14 Apr 2021 15:51:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/A-loop-in-SAS-GUIDE-to-get-information-on-table-according-to-a/m-p/733809#M228657</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-04-14T15:51:42Z</dc:date>
    </item>
  </channel>
</rss>

