<?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: Match observation with another observation according to binary variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Match-observation-with-another-observation-according-to-binary/m-p/53638#M11333</link>
    <description>A SAS DATA step is one choice with INPUT, IF / THEN logic and OUTPUT to read up your input data, use a temporary RETAIN'd SAS variable (or LAG function) to identify the desired observations to keep.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.&lt;BR /&gt;
&lt;BR /&gt;
Recommended Google advanced search argument this topic/post:&lt;BR /&gt;
&lt;BR /&gt;
data step programming site:sas.com</description>
    <pubDate>Sun, 07 Feb 2010 12:13:18 GMT</pubDate>
    <dc:creator>sbb</dc:creator>
    <dc:date>2010-02-07T12:13:18Z</dc:date>
    <item>
      <title>Match observation with another observation according to binary variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-observation-with-another-observation-according-to-binary/m-p/53637#M11332</link>
      <description>data a;&lt;BR /&gt;
input id mark sales;&lt;BR /&gt;
cards;&lt;BR /&gt;
111   1   999999&lt;BR /&gt;
222   0   888888&lt;BR /&gt;
333   0   777777&lt;BR /&gt;
444   1   666666&lt;BR /&gt;
555   1   555555&lt;BR /&gt;
666   0   444444&lt;BR /&gt;
777   0   333333&lt;BR /&gt;
888   0   222222&lt;BR /&gt;
999   0   111111&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Hi, I have the above data set, which is ordered by value of SALES. MARK takes only value of 0 or 1. &lt;BR /&gt;
&lt;BR /&gt;
What I want to do is to extract the observations with MARK=1 and the next closet observation with MARK=0. By "next closet", I mean for the first observation#1 111   1   999999   the next closet observation is observation #2, which is 222   0   888888 (and hence observation #3 is omitted). Observation #4 corresponds to Obs #6, Obs #5 corresponds to Obs #7. Obs #8 and #9 omitted. &lt;BR /&gt;
&lt;BR /&gt;
Hence the final result must have equal number of MARK=1 observations and MARK=0 observations, like this&lt;BR /&gt;
&lt;BR /&gt;
id    mark sales&lt;BR /&gt;
111   1   999999&lt;BR /&gt;
222   0   888888&lt;BR /&gt;
444   1   666666&lt;BR /&gt;
555   1   555555&lt;BR /&gt;
666   0   444444&lt;BR /&gt;
777   0   333333&lt;BR /&gt;
&lt;BR /&gt;
Any idea will be appreciated.</description>
      <pubDate>Sun, 07 Feb 2010 02:16:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-observation-with-another-observation-according-to-binary/m-p/53637#M11332</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-02-07T02:16:03Z</dc:date>
    </item>
    <item>
      <title>Re: Match observation with another observation according to binary variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-observation-with-another-observation-according-to-binary/m-p/53638#M11333</link>
      <description>A SAS DATA step is one choice with INPUT, IF / THEN logic and OUTPUT to read up your input data, use a temporary RETAIN'd SAS variable (or LAG function) to identify the desired observations to keep.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.&lt;BR /&gt;
&lt;BR /&gt;
Recommended Google advanced search argument this topic/post:&lt;BR /&gt;
&lt;BR /&gt;
data step programming site:sas.com</description>
      <pubDate>Sun, 07 Feb 2010 12:13:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-observation-with-another-observation-according-to-binary/m-p/53638#M11333</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-02-07T12:13:18Z</dc:date>
    </item>
    <item>
      <title>Re: Match observation with another observation according to binary variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-observation-with-another-observation-according-to-binary/m-p/53639#M11334</link>
      <description>First you should sort by MARK SALES, both in a descending order.&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sort data=INDATA;&lt;BR /&gt;
by descending MARK descending SALES;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
Then, assuming that MARK will hold a value which is 0 or 1, what you want is output every element with MARK=1 and every element with MARK=0 that are first in the group (if any). This can be done using the auto variables FIRST/LAST to identify the first element of the group:&lt;BR /&gt;
[pre]&lt;BR /&gt;
data OUTDATA;&lt;BR /&gt;
set INDATA;&lt;BR /&gt;
by descending MARK;&lt;BR /&gt;
* output every MARK=1 and every first element of the group MARK=0;&lt;BR /&gt;
if MARK=1 or first.MARK;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Code above not tested!&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>Mon, 08 Feb 2010 09:43:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-observation-with-another-observation-according-to-binary/m-p/53639#M11334</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2010-02-08T09:43:48Z</dc:date>
    </item>
  </channel>
</rss>

