<?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: Do Until Loop Help in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Do-Until-Loop-Help/m-p/44256#M11681</link>
    <description>Your message seem to be incomplete.&lt;BR /&gt;
&lt;BR /&gt;
Please give us a sample of the resulting dataset.&lt;BR /&gt;
&lt;BR /&gt;
Cheers from Portugal&lt;BR /&gt;
&lt;BR /&gt;
Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;</description>
    <pubDate>Thu, 21 Jan 2010 16:40:50 GMT</pubDate>
    <dc:creator>DanielSantos</dc:creator>
    <dc:date>2010-01-21T16:40:50Z</dc:date>
    <item>
      <title>Do Until Loop Help</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Do-Until-Loop-Help/m-p/44255#M11680</link>
      <description>I have two datasets: Paid Items and Payments&lt;BR /&gt;
Here are the samples:&lt;BR /&gt;
&lt;BR /&gt;
Payments:&lt;BR /&gt;
key  py_amt&lt;BR /&gt;
1       25.00&lt;BR /&gt;
2       25.00&lt;BR /&gt;
&lt;BR /&gt;
Paid Items:&lt;BR /&gt;
key   oblgid   pi_amt&lt;BR /&gt;
1        123      25.00&lt;BR /&gt;
2         124     25.00&lt;BR /&gt;
&lt;BR /&gt;
Here is what I have...it partially works, but what I need it to do is mark the observation in Paid Items as found as well.  I need the oblgid in the output and it keeps looping back to the first ob in Paid Items when it goes to the next observation in the payments.&lt;BR /&gt;
&lt;BR /&gt;
Here is the code:&lt;BR /&gt;
&lt;BR /&gt;
data py_key;&lt;BR /&gt;
	set payments;&lt;BR /&gt;
by key;&lt;BR /&gt;
retain first1;&lt;BR /&gt;
if first.key then first1=_n_;&lt;BR /&gt;
if last.key then &lt;BR /&gt;
	do;&lt;BR /&gt;
		last1=_n_;&lt;BR /&gt;
		output;&lt;BR /&gt;
	end;&lt;BR /&gt;
	run;&lt;BR /&gt;
data pi_key;&lt;BR /&gt;
	set paid_items;&lt;BR /&gt;
by key;&lt;BR /&gt;
retain first2;&lt;BR /&gt;
if first.key then first2=_n_;&lt;BR /&gt;
if last.key then&lt;BR /&gt;
	do;&lt;BR /&gt;
		last2=_n_;&lt;BR /&gt;
		output;&lt;BR /&gt;
	end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data keys (keep=key first1 last1 first2 last2);&lt;BR /&gt;
	merge py_key(in=a) pi_key (in=b);&lt;BR /&gt;
by key;&lt;BR /&gt;
if a;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data final;&lt;BR /&gt;
	set keys;&lt;BR /&gt;
do i=first1 to last1;&lt;BR /&gt;
	set payments point=i;&lt;BR /&gt;
	if first2 ne . then &lt;BR /&gt;
	do j=first2 to last2 until(found);&lt;BR /&gt;
		set paid_items point=j;&lt;BR /&gt;
		if py_amt = pi_amt then do;&lt;BR /&gt;
&lt;BR /&gt;
		found=1;&lt;BR /&gt;
		end;&lt;BR /&gt;
		else if py_amt &amp;lt;= pi_amt then do;&lt;BR /&gt;
		found=1;&lt;BR /&gt;
	end;&lt;BR /&gt;
end;&lt;BR /&gt;
if first2 = '.' then delete;&lt;BR /&gt;
&lt;BR /&gt;
output;&lt;BR /&gt;
&lt;BR /&gt;
end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Can't seem to figure it out...</description>
      <pubDate>Thu, 21 Jan 2010 15:30:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Do-Until-Loop-Help/m-p/44255#M11680</guid>
      <dc:creator>redfishJAX</dc:creator>
      <dc:date>2010-01-21T15:30:46Z</dc:date>
    </item>
    <item>
      <title>Re: Do Until Loop Help</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Do-Until-Loop-Help/m-p/44256#M11681</link>
      <description>Your message seem to be incomplete.&lt;BR /&gt;
&lt;BR /&gt;
Please give us a sample of the resulting dataset.&lt;BR /&gt;
&lt;BR /&gt;
Cheers from Portugal&lt;BR /&gt;
&lt;BR /&gt;
Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;</description>
      <pubDate>Thu, 21 Jan 2010 16:40:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Do-Until-Loop-Help/m-p/44256#M11681</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2010-01-21T16:40:50Z</dc:date>
    </item>
    <item>
      <title>Re: Do Until Loop Help</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Do-Until-Loop-Help/m-p/44257#M11682</link>
      <description>found it difficult to understand your need. can you simply give several lines of original data, and also several lines of the wanted results?</description>
      <pubDate>Thu, 21 Jan 2010 17:57:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Do-Until-Loop-Help/m-p/44257#M11682</guid>
      <dc:creator>abdullala</dc:creator>
      <dc:date>2010-01-21T17:57:07Z</dc:date>
    </item>
    <item>
      <title>Re: Do Until Loop Help</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Do-Until-Loop-Help/m-p/44258#M11683</link>
      <description>Sorry about that...for some reason it won't allow me to post the entire text...&lt;BR /&gt;
&lt;BR /&gt;
data payments;&lt;BR /&gt;
	input key_id $ amt;&lt;BR /&gt;
datalines;&lt;BR /&gt;
abc	50&lt;BR /&gt;
abc	50&lt;BR /&gt;
;&lt;BR /&gt;
data paid_items;&lt;BR /&gt;
	input key_id $ oblgid $ amt;&lt;BR /&gt;
datalines;&lt;BR /&gt;
abc	50	123&lt;BR /&gt;
abc	50	124&lt;BR /&gt;
&lt;BR /&gt;
This is the result I am looking for:&lt;BR /&gt;
&lt;BR /&gt;
data result;&lt;BR /&gt;
abc	123	50&lt;BR /&gt;
abc	124	50&lt;BR /&gt;
&lt;BR /&gt;
This is the code I have so far, but it loops back through the observations for paid_items for every &lt;BR /&gt;
observation not found in the payments.  I want to mark the paid_item observation as found also so it does not find&lt;BR /&gt;
that match again.&lt;BR /&gt;
&lt;BR /&gt;
data py_key;&lt;BR /&gt;
	set payments;&lt;BR /&gt;
by key_id;&lt;BR /&gt;
retain first1;&lt;BR /&gt;
if first.key_id then first1=_n_;&lt;BR /&gt;
if last.key_id then &lt;BR /&gt;
	do;&lt;BR /&gt;
		last1=_n_;&lt;BR /&gt;
		output;&lt;BR /&gt;
	end;&lt;BR /&gt;
	run;&lt;BR /&gt;
&lt;BR /&gt;
data pi_key;&lt;BR /&gt;
	set paid_items;&lt;BR /&gt;
by key_id;&lt;BR /&gt;
retain first2;&lt;BR /&gt;
if first.key_id then first2=_n_;&lt;BR /&gt;
if last.key_id then&lt;BR /&gt;
	do;&lt;BR /&gt;
		last2=_n_;&lt;BR /&gt;
		output;&lt;BR /&gt;
	end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data keys (keep=key_id first1 last1 first2 last2);&lt;BR /&gt;
	merge py_key(in=a) pi_key (in=b);&lt;BR /&gt;
by key_id;&lt;BR /&gt;
if a;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data final;&lt;BR /&gt;
	set keys;&lt;BR /&gt;
do i=first2 to last2;&lt;BR /&gt;
	set paid_items point=i;&lt;BR /&gt;
	if first1 ne . then &lt;BR /&gt;
	do j=first1 to last1 until(found);&lt;BR /&gt;
		set payments point=j;&lt;BR /&gt;
		if  py_amt le pi_amt then do;&lt;BR /&gt;
		found=1;&lt;BR /&gt;
	end;&lt;BR /&gt;
end;&lt;BR /&gt;
if first1 = '.' then delete;&lt;BR /&gt;
output;&lt;BR /&gt;
end;&lt;BR /&gt;
run;</description>
      <pubDate>Thu, 21 Jan 2010 17:58:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Do-Until-Loop-Help/m-p/44258#M11683</guid>
      <dc:creator>redfishJAX</dc:creator>
      <dc:date>2010-01-21T17:58:20Z</dc:date>
    </item>
    <item>
      <title>Re: Do Until Loop Help</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Do-Until-Loop-Help/m-p/44259#M11684</link>
      <description>Hi.&lt;BR /&gt;
&lt;BR /&gt;
I'm sorry, but still I'm not quite sure about what you are trying to do.&lt;BR /&gt;
&lt;BR /&gt;
From your example and looking at your code, it seems to me you are trying to do a N-to-N match by KEY_ID using, is this correct?&lt;BR /&gt;
&lt;BR /&gt;
If so, N-to-N match is not feasible through MERGE (at least, in a easy way), but you can achieve that with a simple SQL inner join, like this:&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
create table RESULT as&lt;BR /&gt;
select a.KEY_ID, a.AMT, b.OBLGID&lt;BR /&gt;
from PAYMENTS as a inner join PAID_ITEMS as b on a.KEY_ID = b.KEY_ID;&lt;BR /&gt;
quit;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
Cheers from Portugal&lt;BR /&gt;
&lt;BR /&gt;
Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;</description>
      <pubDate>Fri, 22 Jan 2010 08:31:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Do-Until-Loop-Help/m-p/44259#M11684</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2010-01-22T08:31:19Z</dc:date>
    </item>
  </channel>
</rss>

