<?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 Loop through one dataset and find data in next data set in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-one-dataset-and-find-data-in-next-data-set/m-p/569965#M160678</link>
    <description>&lt;P&gt;I have a dataset with unique id's.&lt;/P&gt;&lt;P&gt;I know there are multiple instances in the 2nd table of each id.&lt;/P&gt;&lt;P&gt;I would like to do a loop where I go through each ID in Dataset A,&lt;/P&gt;&lt;P&gt;Find records matching records in Dataset B, and append that data to the same row for that particular ID, in a new dataset that I created, where each set of dates found in Dataset B can be appended in one row of Dataset C.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would need to incorporate a data step and a proc sql step together, but not sure how do this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to create a dataset that has ID, statedate1, enddate1, startdate2, enddates2, etc...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you in advance&lt;/P&gt;</description>
    <pubDate>Fri, 28 Jun 2019 19:43:19 GMT</pubDate>
    <dc:creator>shoeGirl32</dc:creator>
    <dc:date>2019-06-28T19:43:19Z</dc:date>
    <item>
      <title>Loop through one dataset and find data in next data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-one-dataset-and-find-data-in-next-data-set/m-p/569965#M160678</link>
      <description>&lt;P&gt;I have a dataset with unique id's.&lt;/P&gt;&lt;P&gt;I know there are multiple instances in the 2nd table of each id.&lt;/P&gt;&lt;P&gt;I would like to do a loop where I go through each ID in Dataset A,&lt;/P&gt;&lt;P&gt;Find records matching records in Dataset B, and append that data to the same row for that particular ID, in a new dataset that I created, where each set of dates found in Dataset B can be appended in one row of Dataset C.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would need to incorporate a data step and a proc sql step together, but not sure how do this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to create a dataset that has ID, statedate1, enddate1, startdate2, enddates2, etc...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you in advance&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jun 2019 19:43:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-one-dataset-and-find-data-in-next-data-set/m-p/569965#M160678</guid>
      <dc:creator>shoeGirl32</dc:creator>
      <dc:date>2019-06-28T19:43:19Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through one dataset and find data in next data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-one-dataset-and-find-data-in-next-data-set/m-p/569971#M160679</link>
      <description>If you can mock up some input data and expected output someone can help with code. Wordy questions get wordy answers, data gets code &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;I think an append and then sorting it out after may be a better approach, but I'll wait for some examples.</description>
      <pubDate>Fri, 28 Jun 2019 20:02:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-one-dataset-and-find-data-in-next-data-set/m-p/569971#M160679</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-28T20:02:02Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through one dataset and find data in next data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-one-dataset-and-find-data-in-next-data-set/m-p/569973#M160680</link>
      <description>&lt;P&gt;Ok so :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data Set A: ID's&lt;/P&gt;&lt;P&gt;1234&lt;/P&gt;&lt;P&gt;12345&lt;/P&gt;&lt;P&gt;132456&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Date Set B: Date Detail&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;StartDate&amp;nbsp; &amp;nbsp;Endate&lt;/P&gt;&lt;P&gt;1234&amp;nbsp; &amp;nbsp;1/1/2019&amp;nbsp; &amp;nbsp;1/20/2019&lt;/P&gt;&lt;P&gt;1234&amp;nbsp; &amp;nbsp;2/20/2019&amp;nbsp; 3/1/2019&lt;/P&gt;&lt;P&gt;12345 3/1/2019&amp;nbsp; &amp;nbsp;3/31/2019&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;etc...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;so i want to find EACH ID from Data Set A, that is in data set B&lt;/P&gt;&lt;P&gt;and create a record in a new dataset (Data Set C) that takes the ID, and then appends start Date1 enddate1 startDate2 Enddate2 across in 1 record.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jun 2019 20:12:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-one-dataset-and-find-data-in-next-data-set/m-p/569973#M160680</guid>
      <dc:creator>shoeGirl32</dc:creator>
      <dc:date>2019-06-28T20:12:51Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through one dataset and find data in next data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-one-dataset-and-find-data-in-next-data-set/m-p/569978#M160684</link>
      <description>Please show your exact expected output.</description>
      <pubDate>Fri, 28 Jun 2019 20:28:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-one-dataset-and-find-data-in-next-data-set/m-p/569978#M160684</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-28T20:28:29Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through one dataset and find data in next data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-one-dataset-and-find-data-in-next-data-set/m-p/569980#M160686</link>
      <description>Data Set C:&lt;BR /&gt;&lt;BR /&gt;ID#1 Start1 End1 start2 End2 start3 end3&lt;BR /&gt;ID#2 start1 end1 start 2 end2&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 28 Jun 2019 20:35:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-one-dataset-and-find-data-in-next-data-set/m-p/569980#M160686</guid>
      <dc:creator>shoeGirl32</dc:creator>
      <dc:date>2019-06-28T20:35:19Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through one dataset and find data in next data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-one-dataset-and-find-data-in-next-data-set/m-p/569982#M160687</link>
      <description>&lt;P&gt;Ok, I don't think this is that complicated.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. First get subset of interest&lt;/P&gt;
&lt;P&gt;2. Transpose data&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table step1 as
select * from tableB
where ID in (select ID from tableA);
order by ID;
quit;

*transpose start_date;
proc transpose data=step1 out=step2 prefix=StartDate;
by id;
var startDate;
run;
proc transpose data=step1 out=step3 prefix=EndDate;
by id;
var EndDate;
run;

data want;
merge step2 step3; 
by ID;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 28 Jun 2019 20:42:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-one-dataset-and-find-data-in-next-data-set/m-p/569982#M160687</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-28T20:42:59Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through one dataset and find data in next data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-one-dataset-and-find-data-in-next-data-set/m-p/570021#M160709</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/279349"&gt;@shoeGirl32&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;Procedurally speaking, you need to:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;determine the size N of the longest group by ID in data set TWO&lt;/LI&gt;
&lt;LI&gt;based on it, create a variable list&amp;nbsp;startDate1 endDate1 ... startdateN endDateN, in this order&lt;/LI&gt;
&lt;LI&gt;read data set ONE and loop through the records for the matching IDs in data set TWO to populate the variable list&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;There many ways to execute this plan. Here's one, based purely on using the DATA step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one ;                                                         
  input id ;                                                       
  cards ;                                                          
4                                                                  
3                                                                  
1                                                                  
0                                                                  
2                                                                  
run ;                                                              
data two ;                                                         
  input id startDate endDate ;                                     
  cards ;                                                          
3 31 32                                                            
2 21 22                                                            
3 33 34                                                            
2 23 24                                                            
1 11 12                                                            
3 35 36                                                            
run ;                                                              
                                                                   
data _null_ ;                                                      
  dcl hash h () ;                                                  
  h.definekey ("id") ;                                             
  h.definedata ("q") ;                                             
  h.definedone () ;                                                
  dcl hiter hi ("h") ;                                             
  do until (z) ;                                                   
    set two end = z ;                                              
    if h.find() ne 0 then q = 1 ;                                  
    else                  q + 1 ;                                  
    h.replace() ;                                                  
  end ;                                                            
  do while (hi.next() = 0) ;                                       
    qmax = qmax max q ;                                            
  end ;                                                            
  length s $ 32767 ;                                               
  do q = 1 to qmax ;                                               
    s = catx (" ", s, cats ("startDate", q), cats ("endDate", q)) ;
  end ;                                                            
  call symputx ("s", s) ;                                          
run ;                                                              
                                                                   
data want (drop = startDate endDate q) ;                             
  if _n_ = 1 then do ;                                             
    if 0 then set two ;                                            
    dcl hash h (dataset:"two", multidata:"y") ;                    
    h.definekey ("id") ;                                           
    h.definedata ("startDate", "endDate") ;                        
    h.definedone () ;                                              
  end ;                                                            
  set one ;                                                        
  array dd [*] &amp;amp;s ;                                                
  do q = 0 by 2 while (h.do_over() = 0) ;                          
    dd [q + 1] = startDate ;                                       
    dd [q + 2] = endDate ;                                         
  end ;                                                            
  if q ;                                                           
run ;                                                              
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Sat, 29 Jun 2019 03:24:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-one-dataset-and-find-data-in-next-data-set/m-p/570021#M160709</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-06-29T03:24:13Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through one dataset and find data in next data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-one-dataset-and-find-data-in-next-data-set/m-p/570095#M160724</link>
      <description>&lt;P&gt;Thank you very much.&amp;nbsp; This worked very nicely.&lt;/P&gt;&lt;P&gt;One question though, how do i get my field names to alternate like startdate1, enddate1, start2, end2 etc instead of&amp;nbsp;&lt;/P&gt;&lt;P&gt;start1 start2 start3 end1 end2 end3 etc?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you,&lt;/P&gt;&lt;P&gt;shoeGirl&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 30 Jun 2019 12:04:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-one-dataset-and-find-data-in-next-data-set/m-p/570095#M160724</guid>
      <dc:creator>shoeGirl32</dc:creator>
      <dc:date>2019-06-30T12:04:15Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through one dataset and find data in next data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-one-dataset-and-find-data-in-next-data-set/m-p/570111#M160732</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/279349"&gt;@shoeGirl32&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;If you had looked at my solution, you'd have realized that this is what the DATA _NULL_ step does; It first determined the maximum number of the elements in the variable list and then auto-composes it in the required order.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are spooked by the hash object approach and prefer something more traditional, you can achieve the same as shown below. But no matter which approach you take, you will need to employ some method of auto-composing the list in the requisite variable order.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one ;                                                         
  input id ;                                                       
  cards ;                                                          
4                                                                  
3                                                                  
1                                                                  
0                                                                  
2                                                                  
run ;                                                              
                                                                   
data two ;                                                         
  input id startDate endDate ;                                     
  cards ;                                                          
3 31 32                                                            
2 21 22                                                            
3 33 34                                                            
2 23 24                                                            
1 11 12                                                            
3 35 36                                                            
run ;                                                              
                                                                   
proc sql ;                                                         
  create table sone as select * from one order id ;                
  create table stwo as select * from two order id ;                
quit ;                                                             
                                                                   
data _null_ ;                                                      
  do q = 1 by 1 until (last.id) ;                                  
    set stwo end = z ;                                             
    by id ;                                                        
  end ;                                                            
  _iorc_ = _iorc_ max q ;                                          
  if z ;                                                           
  length s $ 32767 ;                                               
  do q = 1 to _iorc_ ;                                             
    s = catx (" ", s, cats ("startDate", q), cats ("endDate", q)) ;
  end ;                                                            
  call symputx ("s", s) ;                                          
run ;                                                              
                                                                   
data want ;                                                        
  do _n_ = 0 by 2 until (last.id) ;                                
    merge sone (in = one) stwo (in = two) ;                        
    by id ;                                                        
    if one and two ;                                               
    array dd [*] &amp;amp;s ;                                              
    dd [_n_ + 1] = startDate ;                                     
    dd [_n_ + 2] = endDate ;                                       
  end ;                                                            
run ;                                                              
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 30 Jun 2019 16:15:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-one-dataset-and-find-data-in-next-data-set/m-p/570111#M160732</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-06-30T16:15:32Z</dc:date>
    </item>
  </channel>
</rss>

