<?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: How to merge two observations in a single Dataset conditionally in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-two-observations-in-a-single-Dataset-conditionally/m-p/62245#M13554</link>
    <description>remaining code.. after generatePvalues&lt;BR /&gt;
generatePValues(&amp;amp;OuputDS, AlphaLevel);  /*refresh p-values*/&lt;BR /&gt;
%mend merge;&lt;BR /&gt;
&lt;BR /&gt;
%macro showResults(InputDS,OutputDS);&lt;BR /&gt;
generatePValues(&amp;amp;InputDS);&lt;BR /&gt;
proc sql noprint; select min(pVal) into:MinPVal from &amp;amp;InputDS;&lt;BR /&gt;
proc sql noprint; select count(*) into:TotalObsThisTime from &amp;amp;InputDS;&lt;BR /&gt;
proc sql noprint; select count(*)+1 into:TotalObsLastTime from &amp;amp;InputDS; /* merge only if the number of observation changed in last iteration of while loop*/&lt;BR /&gt;
%do %while (&amp;amp;minPVal &amp;lt; &amp;amp;AlphaLevel and &amp;amp; TotalObs ~= &amp;amp;TotalObsLastTime);&lt;BR /&gt;
%let TotalObsLastTime = TotalObsThisTime;&lt;BR /&gt;
%merge(InputDS, &amp;amp;MinPVal, &amp;amp;OutputDS);&lt;BR /&gt;
proc sql noprint; select count(*) into:temp1 from &amp;amp;OutputDS;&lt;BR /&gt;
%let  TotalObsThisTime = &amp;amp;temp1;&lt;BR /&gt;
proc sql noprint; select min(pVal) into:temp2 from &amp;amp;OutputDS;&lt;BR /&gt;
%let minPVal = &amp;amp;temp2;&lt;BR /&gt;
%end;&lt;BR /&gt;
%mend showResults;&lt;BR /&gt;
&lt;BR /&gt;
Now when I call showresults()... the problem is it should stop in the next iteration after it reaches the merging logic abs(pValue -MinPVal)&amp;lt;.0001 first time.&lt;BR /&gt;
but it doesn't.</description>
    <pubDate>Wed, 19 Nov 2008 12:47:54 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2008-11-19T12:47:54Z</dc:date>
    <item>
      <title>How to merge two observations in a single Dataset conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-two-observations-in-a-single-Dataset-conditionally/m-p/62244#M13553</link>
      <description>Here is the exact problem:&lt;BR /&gt;
&lt;BR /&gt;
I have a Data Set containing some observations (say 10).&lt;BR /&gt;
I have to merge two observation if they are similar. &lt;BR /&gt;
&lt;BR /&gt;
Now limitations are:&lt;BR /&gt;
We can merge only consecutive observation and&lt;BR /&gt;
In one step only one merge should happen on one dataset (i.e if obs# 1 and 2 are similar, and obs# 4 and 5 are similar, we can't merge them in single step), so basically as soon as one merging is done I should get out of the merging step, and again run the merging procedure on the new data set obtained by first merging.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
The technique i tried to follow is: I look for rows which are more similar. Say 1 and 2 are more similar (have less "p-value") than 4 and 5. Then I merge 1 and 2 first, otherwise I merge 4 and 5 first. Problem is that some times "p-values" are similar for more than 1 pair of observations. Then I have to merge any 2 observations (1 pair) and again run the merging procedure on new dataset.&lt;BR /&gt;
&lt;BR /&gt;
Dataset Contains following fields:&lt;BR /&gt;
GoodCount, TotalCount, pValue&lt;BR /&gt;
&lt;BR /&gt;
Here is the Code which I tried: (If you have a solution for my problem you can ignore the code and just tell me the solution, otherwise please tell me where i m wrong)&lt;BR /&gt;
&lt;BR /&gt;
%macro generatePValues(InputDS);&lt;BR /&gt;
/* some code here to regenerate p values every time we call this macro, works fine*/&lt;BR /&gt;
%mend;&lt;BR /&gt;
&lt;BR /&gt;
%macro merge(InputDs, minPVal);&lt;BR /&gt;
proc sql noprint; select count(*) into:flag from &amp;amp;InputDS;&lt;BR /&gt;
&lt;BR /&gt;
data tempDS; set &amp;amp;InputDS;&lt;BR /&gt;
if &amp;amp;flag = _N_-1 then stop;&lt;BR /&gt;
dummy1 = lag(goodCount) + goodCount;&lt;BR /&gt;
dummy2 = lag(totalCount) + totalCount;&lt;BR /&gt;
if (pVal-&amp;amp;minPVal)&amp;lt;.0001 then do;&lt;BR /&gt;
goodCount = dummy1;&lt;BR /&gt;
totalCount = dummy2;&lt;BR /&gt;
call symput ( 'deleteRow', _N_-1);	&lt;BR /&gt;
call symput ( 'Flag', _N_);&lt;BR /&gt;
end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Data &amp;amp;OutputDS; set tempDS;&lt;BR /&gt;
if _N_ NE &amp;amp;deleteRow;&lt;BR /&gt;
drop dummy1 dummy2;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
generatePValues(&amp;amp;OuputDS, AlphaLevel);  /*refresh p-values*/&lt;BR /&gt;
%mend merge;&lt;BR /&gt;
&lt;BR /&gt;
%macro showResults(InputDS,OutputDS);&lt;BR /&gt;
generatePValues(&amp;amp;InputDS);&lt;BR /&gt;
proc sql noprint; select min(pVal) into:MinPVal from &amp;amp;InputDS;&lt;BR /&gt;
proc sql noprint; select count(*) into:TotalObsThisTime from &amp;amp;InputDS;&lt;BR /&gt;
proc sql noprint; select count(*)+1 into:TotalObsLastTime from &amp;amp;InputDS; /* merge only if the number of observation changed in last iteration of while loop*/&lt;BR /&gt;
%do %while (&amp;amp;minPVal &amp;lt; &amp;amp;AlphaLevel and &amp;amp; TotalObs ~= &amp;amp;TotalObsLastTime);&lt;BR /&gt;
%let TotalObsLastTime = TotalObsThisTime;&lt;BR /&gt;
%merge(InputDS, &amp;amp;MinPVal, &amp;amp;OutputDS);&lt;BR /&gt;
proc sql noprint; select count(*) into:temp1 from &amp;amp;OutputDS;&lt;BR /&gt;
%let  TotalObsThisTime = &amp;amp;temp1;&lt;BR /&gt;
proc sql noprint; select min(pVal) into:temp2 from &amp;amp;OutputDS;&lt;BR /&gt;
%let minPVal = &amp;amp;temp2;&lt;BR /&gt;
%end;&lt;BR /&gt;
%mend showResults;&lt;BR /&gt;
&lt;BR /&gt;
Now when I call showresults()... the problem is it should stop in the next iteration after it reaches the merging logic abs(pValue -MinPVal)&amp;lt;.0001 first time.&lt;BR /&gt;
but it doesn't.</description>
      <pubDate>Wed, 19 Nov 2008 12:45:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-two-observations-in-a-single-Dataset-conditionally/m-p/62244#M13553</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-11-19T12:45:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge two observations in a single Dataset conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-two-observations-in-a-single-Dataset-conditionally/m-p/62245#M13554</link>
      <description>remaining code.. after generatePvalues&lt;BR /&gt;
generatePValues(&amp;amp;OuputDS, AlphaLevel);  /*refresh p-values*/&lt;BR /&gt;
%mend merge;&lt;BR /&gt;
&lt;BR /&gt;
%macro showResults(InputDS,OutputDS);&lt;BR /&gt;
generatePValues(&amp;amp;InputDS);&lt;BR /&gt;
proc sql noprint; select min(pVal) into:MinPVal from &amp;amp;InputDS;&lt;BR /&gt;
proc sql noprint; select count(*) into:TotalObsThisTime from &amp;amp;InputDS;&lt;BR /&gt;
proc sql noprint; select count(*)+1 into:TotalObsLastTime from &amp;amp;InputDS; /* merge only if the number of observation changed in last iteration of while loop*/&lt;BR /&gt;
%do %while (&amp;amp;minPVal &amp;lt; &amp;amp;AlphaLevel and &amp;amp; TotalObs ~= &amp;amp;TotalObsLastTime);&lt;BR /&gt;
%let TotalObsLastTime = TotalObsThisTime;&lt;BR /&gt;
%merge(InputDS, &amp;amp;MinPVal, &amp;amp;OutputDS);&lt;BR /&gt;
proc sql noprint; select count(*) into:temp1 from &amp;amp;OutputDS;&lt;BR /&gt;
%let  TotalObsThisTime = &amp;amp;temp1;&lt;BR /&gt;
proc sql noprint; select min(pVal) into:temp2 from &amp;amp;OutputDS;&lt;BR /&gt;
%let minPVal = &amp;amp;temp2;&lt;BR /&gt;
%end;&lt;BR /&gt;
%mend showResults;&lt;BR /&gt;
&lt;BR /&gt;
Now when I call showresults()... the problem is it should stop in the next iteration after it reaches the merging logic abs(pValue -MinPVal)&amp;lt;.0001 first time.&lt;BR /&gt;
but it doesn't.</description>
      <pubDate>Wed, 19 Nov 2008 12:47:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-two-observations-in-a-single-Dataset-conditionally/m-p/62245#M13554</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-11-19T12:47:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge two observations in a single Dataset conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-two-observations-in-a-single-Dataset-conditionally/m-p/62246#M13555</link>
      <description>I'm not sure I understand your challenge - can you perhaps provide some 'before' and 'after' sample data.&lt;BR /&gt;
&lt;BR /&gt;
Is it something like this your are trying ?[pre]&lt;BR /&gt;
data input;&lt;BR /&gt;
input value1 value2;&lt;BR /&gt;
datalines;&lt;BR /&gt;
1.1 1&lt;BR /&gt;
1.2 2&lt;BR /&gt;
2.0 3&lt;BR /&gt;
3.1 4&lt;BR /&gt;
3.2 5&lt;BR /&gt;
1.3 6&lt;BR /&gt;
4.0 7&lt;BR /&gt;
;run;&lt;BR /&gt;
&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;
data dist;&lt;BR /&gt;
  set input;&lt;BR /&gt;
  retain key 0;&lt;BR /&gt;
  prev_val = lag1(value1);&lt;BR /&gt;
  if _n_ = 1 then key = 1;&lt;BR /&gt;
  else if prev_val*0.9 &amp;lt;= value1 &amp;lt;= prev_val*1.1 then; &lt;BR /&gt;
  else key+1;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]In this sample code each record gets a (new) surrogate key based on if the value (current record) is within a range (based on the previous record). After this you can use the new key, to merge, summarize etc. your data.</description>
      <pubDate>Wed, 19 Nov 2008 14:27:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-two-observations-in-a-single-Dataset-conditionally/m-p/62246#M13555</guid>
      <dc:creator>GertNissen</dc:creator>
      <dc:date>2008-11-19T14:27:05Z</dc:date>
    </item>
  </channel>
</rss>

