<?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: Define a filter with variable in data step with do loop- SAS in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Define-a-filter-with-variable-in-data-step-with-do-loop-SAS/m-p/531923#M5948</link>
    <description>&lt;P&gt;Sorry, you will need to be clearer in what you want.&amp;nbsp; Start by posting test data in the form of a datastep.&lt;/P&gt;
&lt;P&gt;Dataset ID Customer is never used in the post?&lt;/P&gt;
&lt;P&gt;If you just want first to last then:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  length db $200;
  retain db;
  by id;
  if first.id then db=cats("_",put(date,date9.),"_[PLACEHOLDER]_",put(id,best.),"_F";
  if last.id then do;
    db=tranwrd(db,"[PLACEHOLDER]",put(date,mmyy5.));
    output;
  end;
run;

  &lt;/PRE&gt;</description>
    <pubDate>Fri, 01 Feb 2019 09:56:24 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2019-02-01T09:56:24Z</dc:date>
    <item>
      <title>Define a filter with variable in data step with do loop- SAS</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Define-a-filter-with-variable-in-data-step-with-do-loop-SAS/m-p/531917#M5947</link>
      <description>&lt;P&gt;Good Morning, i've this problem.&lt;/P&gt;&lt;P&gt;there are 2 dataset&lt;/P&gt;&lt;P&gt;Dataset "ID Customer" where i have this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;id       |  Customer Name   |
-----------------------------
123456   | Michael One      |
123123   | George Two       |
123789   | James Three      |&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and the second dataset named "transaction":&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;id       |  Transaction | Date
-----------------------------------
123456   | Fuel         | 01NOV2018
123456   | Fuel         | 03NOV2018
123123   | Fuel         | 10NOV2018
123456   | Fuel         | 25NOV2018
123123   | Fuel         | 13NOV2018
123456   | Fuel         | 10DEC2018
123789   | Fuel         | 1NOV2018
123123   | Fuel         | 30NOV2018
123789   | Fuel         | 15DEC2018&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;the results that i want is to create 3 db like a 3 customer id that i've in the first Dataset named:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;_01NOV2018_15NOV_123456_F
_01NOV2018_15NOV_123123_F
_01NOV2018_15NOV_123789_F&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;that contains:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;For  _01NOV2018_15NOV_123456_F :
id       |  Transaction | Date
-----------------------------------
123456   | Fuel         | 01NOV2018
123456   | Fuel         | 03NOV2018

For _01NOV2018_15NOV_123123_F :

id       |  Transaction | Date
-----------------------------------
123123   | Fuel         | 10NOV2018
123123   | Fuel         | 13NOV2018&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;CODE&gt;For _01NOV2018_15NOV_123789_F&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE&gt;empty&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;I need to create a variable for a clause where in data step... how can i make this?&lt;/P&gt;&lt;P&gt;thanks for help! :)`&lt;/P&gt;</description>
      <pubDate>Fri, 01 Feb 2019 09:29:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Define-a-filter-with-variable-in-data-step-with-do-loop-SAS/m-p/531917#M5947</guid>
      <dc:creator>FRAFLUTE</dc:creator>
      <dc:date>2019-02-01T09:29:14Z</dc:date>
    </item>
    <item>
      <title>Re: Define a filter with variable in data step with do loop- SAS</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Define-a-filter-with-variable-in-data-step-with-do-loop-SAS/m-p/531923#M5948</link>
      <description>&lt;P&gt;Sorry, you will need to be clearer in what you want.&amp;nbsp; Start by posting test data in the form of a datastep.&lt;/P&gt;
&lt;P&gt;Dataset ID Customer is never used in the post?&lt;/P&gt;
&lt;P&gt;If you just want first to last then:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  length db $200;
  retain db;
  by id;
  if first.id then db=cats("_",put(date,date9.),"_[PLACEHOLDER]_",put(id,best.),"_F";
  if last.id then do;
    db=tranwrd(db,"[PLACEHOLDER]",put(date,mmyy5.));
    output;
  end;
run;

  &lt;/PRE&gt;</description>
      <pubDate>Fri, 01 Feb 2019 09:56:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Define-a-filter-with-variable-in-data-step-with-do-loop-SAS/m-p/531923#M5948</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2019-02-01T09:56:24Z</dc:date>
    </item>
    <item>
      <title>Re: Define a filter with variable in data step with do loop- SAS</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Define-a-filter-with-variable-in-data-step-with-do-loop-SAS/m-p/531930#M5949</link>
      <description>&lt;P&gt;Some questions on the why:&lt;/P&gt;
&lt;UL style="list-style-position: inside;"&gt;
&lt;LI&gt;Do you really want to create a new table for each id you have in the first table?&lt;/LI&gt;
&lt;LI&gt;How many ids will your first table have, just a few, or several 1000s?&lt;/LI&gt;
&lt;LI&gt;What are the new tables created for each id used for?&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Fri, 01 Feb 2019 10:46:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Define-a-filter-with-variable-in-data-step-with-do-loop-SAS/m-p/531930#M5949</guid>
      <dc:creator>BrunoMueller</dc:creator>
      <dc:date>2019-02-01T10:46:05Z</dc:date>
    </item>
    <item>
      <title>Re: Define a filter with variable in data step with do loop- SAS</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Define-a-filter-with-variable-in-data-step-with-do-loop-SAS/m-p/532049#M5968</link>
      <description>&lt;P&gt;Hi RW9, thanks for your support!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sorry for the imprecision... Datase ID Customer is a read only dataset where i want to read the customer ID that will be a variable for a filter in the second dataset "transaction".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example: i want that sas read the first customer id (123456) and make a filter in the second dataset (where customer id =123456 and data between 01NOV2018 and 15NOV2018) to create a export file with the name _01NOV2018_15NOV_123456_F.&lt;/P&gt;&lt;P&gt;This filter will have to repeat for all customer id that is present in the first dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to produce 1 file for all customer id that is in the first dataset for a data range that&amp;nbsp; could be the previous month of the estraction.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot!!&lt;/P&gt;</description>
      <pubDate>Fri, 01 Feb 2019 15:46:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Define-a-filter-with-variable-in-data-step-with-do-loop-SAS/m-p/532049#M5968</guid>
      <dc:creator>FRAFLUTE</dc:creator>
      <dc:date>2019-02-01T15:46:57Z</dc:date>
    </item>
    <item>
      <title>Re: Define a filter with variable in data step with do loop- SAS</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Define-a-filter-with-variable-in-data-step-with-do-loop-SAS/m-p/532052#M5970</link>
      <description>&lt;P&gt;Thnks for the interest!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- Yes, i need to create a new table for each id that i have in the first table cause i need to send the estraction to all of these customer&lt;/P&gt;&lt;P&gt;- The id are not a lot. They could be maximun 100 id&lt;/P&gt;&lt;P&gt;- The new table is all the transaction of an id customer in the data range (the data range could be the previous month).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Fri, 01 Feb 2019 15:50:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Define-a-filter-with-variable-in-data-step-with-do-loop-SAS/m-p/532052#M5970</guid>
      <dc:creator>FRAFLUTE</dc:creator>
      <dc:date>2019-02-01T15:50:26Z</dc:date>
    </item>
    <item>
      <title>Re: Define a filter with variable in data step with do loop- SAS</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Define-a-filter-with-variable-in-data-step-with-do-loop-SAS/m-p/532097#M5976</link>
      <description>&lt;PRE&gt;data customers;
infile cards dlm='|';
attrib
  id length=8
  name length=$20
;
input id name ;
datalines;
123456   | Michael One      |
123123   | George Two       |
123789   | James Three      |
run;
data transactions;
infile cards dlm='|';
attrib
  id length=8
  transaction length=$10
  date length=8 format=date9. informat=date9.
;
input id transaction date;
datalines;
123456   | Fuel         | 01NOV2018
123456   | Fuel         | 03NOV2018
123123   | Fuel         | 10NOV2018
123456   | Fuel         | 25NOV2018
123123   | Fuel         | 13NOV2018
123456   | Fuel         | 10DEC2018
123789   | Fuel         | 1NOV2018
123123   | Fuel         | 30NOV2018
123789   | Fuel         | 15DEC2018
run;&lt;/PRE&gt;&lt;P&gt;For simplify your work this is the input code. My process will be able to read all of the customer id in the first data set (customers) and produce a dataset named _01NOV2018_30NOV2018_123456_F (&lt;EM&gt;first day previuous month&lt;/EM&gt;_&lt;EM&gt;last day previous month&lt;/EM&gt;_&lt;EM&gt;customer id&lt;/EM&gt;_F) with all of the transaction for each customer id in the previous month.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Feb 2019 16:59:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Define-a-filter-with-variable-in-data-step-with-do-loop-SAS/m-p/532097#M5976</guid>
      <dc:creator>FRAFLUTE</dc:creator>
      <dc:date>2019-02-01T16:59:28Z</dc:date>
    </item>
    <item>
      <title>Re: Define a filter with variable in data step with do loop- SAS</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Define-a-filter-with-variable-in-data-step-with-do-loop-SAS/m-p/532517#M6028</link>
      <description>&lt;P&gt;You might want to use the this pattern&amp;nbsp;&lt;A href="https://blogs.sas.com/content/sasdummy/2012/03/20/sas-program-by-processing/" target="_blank"&gt;https://blogs.sas.com/content/sasdummy/2012/03/20/sas-program-by-processing/&lt;/A&gt; by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4"&gt;@ChrisHemedinger&lt;/a&gt;&amp;nbsp;, if your second table is not to big, this is a good approach.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course you need to adapt it to your needs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also have a look at this post as well&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Splitting-up-dataset-based-on-unique-variable/td-p/347689" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Splitting-up-dataset-based-on-unique-variable/td-p/347689&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Feb 2019 08:51:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Define-a-filter-with-variable-in-data-step-with-do-loop-SAS/m-p/532517#M6028</guid>
      <dc:creator>BrunoMueller</dc:creator>
      <dc:date>2019-02-04T08:51:07Z</dc:date>
    </item>
    <item>
      <title>Re: Define a filter with variable in data step with do loop- SAS</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Define-a-filter-with-variable-in-data-step-with-do-loop-SAS/m-p/532805#M6090</link>
      <description>&lt;P&gt;Great! It work! This is the action that i mean...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've costructed this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data customers;
infile cards dlm='|';
attrib
  id length=8
  name length=$20
;
input id name ;
datalines;
123456   | Michael One      |
123123   | George Two       |
123789   | James Three      |
run;

data transactions;
infile cards dlm='|';
attrib
  id length=8
  transaction length=$10
  date length=8 format=date9. informat=date9.
;
input id transaction date;
datalines;
123456   | Fuel         | 01NOV2018
123456   | Fuel         | 03NOV2018
123123   | Fuel         | 10NOV2018
123456   | Fuel         | 25NOV2018
123123   | Fuel         | 13NOV2018
123456   | Fuel         | 10DEC2018
123789   | Fuel         | 1NOV2018
123123   | Fuel         | 30NOV2018
123789   | Fuel         | 15DEC2018
run;

 proc sql noprint;
	  select strip(put(count(distinct id),15.)) into :varCount from transactions;
	  select distinct id into :varVal1- :varVal&amp;amp;varCount  from transactions;
	  %let today=%sysfunc(today(),date9.);
	  %let data_i=%sysfunc(intnx(month,"&amp;amp;today"d,-3,b),date9.);
	  %let data_f=%sysfunc(intnx(month,"&amp;amp;today"d,-3,e),date9.);
  quit;
%put &amp;amp;varCount &amp;amp;data_i &amp;amp;data_f;

%macro ReportOnEachType;
  %do index = 1 %to &amp;amp;varCount;
  proc sql;	
		create table pippo as
		select * 
		from transactions
		where id=&amp;amp;&amp;amp;varVal&amp;amp;index and date between intnx('month',today(),-3,'b') and intnx('month',today(),-3,'e');
  quit;
 %end;
%mend;
%ReportOnEachType;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;With this i can repeat the actions for all of the record of the customer dataset but i need to insert a variable data_i and data_f in the beetween and i need to give a personalized name to the table (now i called it "PIPPO" and for this in the result i see only the last result with the name PIPPO).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can i do?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot!&lt;/P&gt;</description>
      <pubDate>Mon, 04 Feb 2019 23:07:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Define-a-filter-with-variable-in-data-step-with-do-loop-SAS/m-p/532805#M6090</guid>
      <dc:creator>FRAFLUTE</dc:creator>
      <dc:date>2019-02-04T23:07:56Z</dc:date>
    </item>
    <item>
      <title>Re: Define a filter with variable in data step with do loop- SAS</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Define-a-filter-with-variable-in-data-step-with-do-loop-SAS/m-p/532877#M6098</link>
      <description>&lt;P&gt;I am sorry, but this really is not the way to program.&amp;nbsp; You are effectively re-programming the system, which will result in code which is hugely resource hungry and code which is difficult to maintain.&amp;nbsp; The proper process would be:&lt;/P&gt;
&lt;P&gt;1) Merge the two datasets&lt;/P&gt;
&lt;P&gt;2) Assign group values to each observation&lt;/P&gt;
&lt;P&gt;3) Program a set of manipulations using by group processing with the group values assigned in 2)&lt;/P&gt;
&lt;P&gt;4) Report the data using a by group defined in 2).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is never a need, nor is it ever a good idea to start putting data into macro, using macro loops etc.&amp;nbsp; It just multiplies the resouce cost of your code, and makes it unmaintainable.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Feb 2019 10:28:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Define-a-filter-with-variable-in-data-step-with-do-loop-SAS/m-p/532877#M6098</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2019-02-05T10:28:59Z</dc:date>
    </item>
    <item>
      <title>Re: Define a filter with variable in data step with do loop- SAS</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Define-a-filter-with-variable-in-data-step-with-do-loop-SAS/m-p/532960#M6118</link>
      <description>&lt;P&gt;I have made some changes to your code, see below.&lt;/P&gt;
&lt;P&gt;The number of values from the select distinct does not need to be counted, there is a new syntax with the open end in the INTO.&lt;/P&gt;
&lt;P&gt;Showed an example on how to put together the name of the new table, it is now based on start and end date plus the id.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Question why are you not taking the values from the first table (customers), is this one needed at all? What if a customer does not have any transactions?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You said you need to send each table, do you mean by email (SAS can send emails directly), and what is the format?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data customers;
infile cards dlm='|';
attrib
  id length=8
  name length=$20
;
input id name ;
datalines;
123456   | Michael One
123123   | George Two
123789   | James Three
;

data transactions;
infile cards dlm='|';
attrib
  id length=8
  transaction length=$10
  date length=8 format=date9. informat=date9.
;
input id transaction date;
datalines;
123456   | Fuel         | 01NOV2018
123456   | Fuel         | 03dec2018
123123   | Fuel         | 10dec2018
123456   | Fuel         | 25dec2018
123123   | Fuel         | 13NOV2018
123456   | Fuel         | 10DEC2018
123789   | Fuel         | 1NOV2018
123123   | Fuel         | 30NOV2018
123789   | Fuel         | 15DEC2018
;

 proc sql noprint;
    select distinct id into :varVal1-  from transactions;
    %let varCount = &amp;amp;sqlobs;
quit;
%put NOTE: &amp;amp;=varCount;


%let someDate = 05jan2019;
%let prevMonthStart = %sysfunc(intnx(month, %sysevalf("&amp;amp;someDate"d), -1, B), date9.);
%let prevMonthEnd = %sysfunc(intnx(month, %sysevalf("&amp;amp;someDate"d), -1, E), date9.);

%macro ReportOnEachType;
  %do index = 1 %to &amp;amp;varCount;
  %let id = &amp;amp;&amp;amp;varVal&amp;amp;index;
  %put NOTE: processing &amp;amp;id;
  
  proc sql feedback;
    create table _&amp;amp;prevMonthStart._&amp;amp;prevMonthEnd._&amp;amp;id._F as
    select *
    from transactions
    where id=&amp;amp;id and date between "&amp;amp;prevMonthStart"d and "&amp;amp;prevMonthEnd"d;
  quit;
 %end;
%mend;

%ReportOnEachType;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Feb 2019 15:29:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Define-a-filter-with-variable-in-data-step-with-do-loop-SAS/m-p/532960#M6118</guid>
      <dc:creator>BrunoMueller</dc:creator>
      <dc:date>2019-02-05T15:29:28Z</dc:date>
    </item>
  </channel>
</rss>

