<?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: Loop through one table, use data to manipulate another table in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Loop-through-one-table-use-data-to-manipulate-another-table/m-p/455961#M70056</link>
    <description>&lt;P&gt;Review the guidance on how to post a new question.&amp;nbsp; Provide test data for each dataset in the form of a datastep, you can use this post to help:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;We cannot see your data, nor guess what its like.&lt;/P&gt;
&lt;P&gt;Next, post example using the test data, of what you want to see out at the end, again, we cannot guess this.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general, a datastep or procedure is already a loop, so you should not really need to program a step by step, just logic to apply to all steps.&amp;nbsp; Also, its generally not a good idea to split data up, then merge it back together again.&lt;/P&gt;
&lt;P&gt;For instance, all data within a date range:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table want as 
  select b.*
  from   b
  right join a
  on       b.varname=a.name
  and     b.date between a.begin_date and a.end_date;
quit;&lt;/PRE&gt;</description>
    <pubDate>Fri, 20 Apr 2018 15:11:49 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2018-04-20T15:11:49Z</dc:date>
    <item>
      <title>Loop through one table, use data to manipulate another table</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loop-through-one-table-use-data-to-manipulate-another-table/m-p/455930#M70055</link>
      <description>&lt;P&gt;I have Table A which consists of: Name, Begin-Date, End-Date and I have table B which consists of: Name, Date.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to loop through Table B one row at a time, saving the name (VarDate) and date (VarDate) as variables.&amp;nbsp; Using those saved variables, I want to create 3 tables.&amp;nbsp; Table 1 is all records from Table A that are not VarName and the VarDAte is not between the Begin-Date and End-Date.&amp;nbsp; The other 2 tables are identical: the record from Table A that is VarName with the Date between Begin-Date and End-Date.&amp;nbsp; I will then Change the End-Date in table 2 to the VarDAte and the Begin-Date in Table 3 to VarDAte and union all 3 tables.&amp;nbsp; Then go to the next row in Table B and do the same thing all over again.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I find I need to do it this way because Table B may list the same Name with multiple dates and I need the end file to show:&lt;/P&gt;&lt;P&gt;Name Begin-Date Date1&lt;/P&gt;&lt;P&gt;Name Date1 Date2&lt;/P&gt;&lt;P&gt;Name Date2 End-Date&lt;/P&gt;&lt;P&gt;as an example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help with the coding or other suggestions is appreciated.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Apr 2018 14:33:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loop-through-one-table-use-data-to-manipulate-another-table/m-p/455930#M70055</guid>
      <dc:creator>Wrathofmath</dc:creator>
      <dc:date>2018-04-20T14:33:06Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through one table, use data to manipulate another table</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loop-through-one-table-use-data-to-manipulate-another-table/m-p/455961#M70056</link>
      <description>&lt;P&gt;Review the guidance on how to post a new question.&amp;nbsp; Provide test data for each dataset in the form of a datastep, you can use this post to help:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;We cannot see your data, nor guess what its like.&lt;/P&gt;
&lt;P&gt;Next, post example using the test data, of what you want to see out at the end, again, we cannot guess this.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general, a datastep or procedure is already a loop, so you should not really need to program a step by step, just logic to apply to all steps.&amp;nbsp; Also, its generally not a good idea to split data up, then merge it back together again.&lt;/P&gt;
&lt;P&gt;For instance, all data within a date range:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table want as 
  select b.*
  from   b
  right join a
  on       b.varname=a.name
  and     b.date between a.begin_date and a.end_date;
quit;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Apr 2018 15:11:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loop-through-one-table-use-data-to-manipulate-another-table/m-p/455961#M70056</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-04-20T15:11:49Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through one table, use data to manipulate another table</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loop-through-one-table-use-data-to-manipulate-another-table/m-p/456042#M70061</link>
      <description>&lt;P&gt;data sashelp.file1;&lt;BR /&gt;infile datalines dsd truncover;&lt;BR /&gt;input Name:$13. BeginDate:mmddyy10. EndDate:mmddyy10.;&lt;BR /&gt;datalines4;&lt;BR /&gt;Bob,05/03/2007,07/18/2016&lt;BR /&gt;Carol,06/07/2008,08/09/2010&lt;BR /&gt;Evan, 03/02/2004,12/24/2016&lt;BR /&gt;Trevor, 06/26/2009,06/26/2010&lt;BR /&gt;Tyler, 07/17/2007, 09/28/2011&lt;BR /&gt;;;;;&lt;/P&gt;&lt;P&gt;data sashelp.file2;&lt;BR /&gt;infile datalines dsd truncover;&lt;BR /&gt;input Name:$13. Date:mmddyy10. ;&lt;BR /&gt;datalines4;&lt;BR /&gt;Evan, 07/08/2007&lt;BR /&gt;Evan, 02/02/2010&lt;BR /&gt;Tyler, 05/11/2009&lt;BR /&gt;;;;;&lt;/P&gt;&lt;P&gt;data sashelp.file3;&lt;BR /&gt;infile datalines dsd truncover;&lt;BR /&gt;input Name:$13. BeginDate:mmddyy10. EndDate:mmddyy10.;&lt;BR /&gt;datalines4;&lt;BR /&gt;Bob,05/03/2007,07/18/2016&lt;BR /&gt;Carol,06/07/2008,08/09/2010&lt;BR /&gt;Evan, 03/02/2004,7/8/2007&lt;BR /&gt;Evan, 7/8/2007,2/2/2010&lt;BR /&gt;Evan, 2/2/2010,12/24/2016&lt;BR /&gt;Trevor, 06/26/2009,06/26/2010&lt;BR /&gt;Tyler, 07/17/2007, 5/11/2009&lt;BR /&gt;Tyler, 5/11/2009, 09/28/2011&lt;BR /&gt;;;;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I couldn't really get the program to work but I think this should at least get you where you need to be.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Essentially, I am trying to break up file 1 with information from file 2.&amp;nbsp; For every name and date in File 2, I need to locate the row in File 1, break it up into 2 data rows, and replace the BeginDate in one row with the new date and EndDate in the other row with the new Date.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried to use data steps but I cannot see how it will work in this instance because, for example Evan, needs to have the split done for the first date in File 2 before going to the second split.&amp;nbsp; But maybe there is an advanced data set expression that can handle what I need it to do.&amp;nbsp; Thanks for you help.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Apr 2018 19:09:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loop-through-one-table-use-data-to-manipulate-another-table/m-p/456042#M70061</guid>
      <dc:creator>Wrathofmath</dc:creator>
      <dc:date>2018-04-20T19:09:57Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through one table, use data to manipulate another table</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loop-through-one-table-use-data-to-manipulate-another-table/m-p/456422#M70086</link>
      <description>&lt;P&gt;I don't have time to write the whole process, but to get your started:&lt;/P&gt;
&lt;PRE&gt;data _null_;
  set file2;
  call execute('data want'||strip(put(_n_,best.))||'; 
                  set file1;
                  if name="'||strip(name)||'" and... then do;
                    ...;
                  end;
                run;');
run;&lt;/PRE&gt;
&lt;P&gt;This will generate a datastep for each row in file2, which will then get executed after the data _null_ is finished.&amp;nbsp; So your effectively using file2 to generate all the code needed.&amp;nbsp; This is an advanced topic however.&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>Mon, 23 Apr 2018 08:15:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loop-through-one-table-use-data-to-manipulate-another-table/m-p/456422#M70086</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-04-23T08:15:13Z</dc:date>
    </item>
  </channel>
</rss>

