<?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 How to flag these by looking at the next row of records? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-these-by-looking-at-the-next-row-of-records/m-p/470771#M285685</link>
    <description>&lt;P&gt;Good Day everyone here.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have this scenario on my hand where i was given a Data that requires me to flag into 5 different criteria.&lt;/P&gt;&lt;P&gt;Before i talk about the criteria/rules, let me share the data with you all.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Services	buy_sell	seq_number	trade_id	Type
2009-AAA	Sell		0		100		D
2009-AAA	Buy		0		101		D
2009-BBB	Sell		0		102		D
2009-BBB	Buy		0		103		F
2009-BBB	Buy		0		104		F
2010-CCC	Sell		0		105		F
2010-DDD	Buy		0		106		F
2010-DDD	Buy		0		107		F
2010-EEE	Buy		0		108		D
2010-EEE	Sell		0		109		F&lt;BR /&gt;2011-ZZZ        Buy             0               110             F&lt;/PRE&gt;&lt;P&gt;The data type as below:&lt;/P&gt;&lt;P&gt;Services 20 Character&lt;/P&gt;&lt;P&gt;buy_sell 5 Character&lt;/P&gt;&lt;P&gt;seq_number 8 num&lt;/P&gt;&lt;P&gt;trade_id 10 Character&lt;/P&gt;&lt;P&gt;Type 1 Character&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now here are the Criterias:&lt;/P&gt;&lt;P&gt;1) list out the &lt;U&gt;Services&lt;/U&gt; that has &lt;U&gt;Buy_sell&lt;/U&gt; Buy and Sell &amp;amp; D and D as &lt;U&gt;type&lt;/U&gt; (All this in Pair, could just be 1 pair in most cases but also possible to have more than a pair). From the data above, Services 2009-AAA fulfill this criteria. Flag records in this criteria as 'A'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2) list out &lt;U&gt;Services&lt;/U&gt; that has &lt;U&gt;Buy_sell&lt;/U&gt; Buy and Sell &amp;amp; F and D as &lt;U&gt;type&lt;/U&gt;. (All this in Pair, could just be 1 pair in most cases but also possible to have more than a pair). From the data above, Services 2010-EEE fulfill this criteria. Flag records in this criteria as 'B'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3) List out Services that has &lt;U&gt;Buy_sell&lt;/U&gt; Buy and Sell &amp;amp; F and F as &lt;U&gt;type&lt;/U&gt;. (All this in Pair, could just be 1 pair in most cases but also possible to have more than a pair). From the data above, Services 2009-BBB fulfill this criteria BUT with additional trade_id of 104. In this case, mark this 3 records(2009-BBB) as Exception flag. Flag records in this criteria as 'C'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;4) list out &lt;U&gt;Services&lt;/U&gt; that has &lt;U&gt;Buy_sell&lt;/U&gt; Buy and Buy or Sell and Sell &amp;amp; any &lt;U&gt;type&lt;/U&gt;. (All this in Pair, could just be 1 pair in most cases but also possible to have more than a pair). From the data above, Services 2010-DDD fulfill this criteria. Flag records in this criteria as 'D'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;5) List out &lt;U&gt;Services&lt;/U&gt; that only has 1 record alone. From the data above, Services 2011-ZZZ fulfill this criteria. Flag records in this criteria as 'E'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is a big challenge to me as i was thinking to group them first. However, how should i further work from grouped data.&lt;/P&gt;&lt;P&gt;I really hope if anyone can help me with SAS Dataset or Proc Sql or even macro loop if necessary for this case.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A big thanks in advanced.&lt;/P&gt;</description>
    <pubDate>Sat, 16 Jun 2018 08:51:29 GMT</pubDate>
    <dc:creator>imdickson</dc:creator>
    <dc:date>2018-06-16T08:51:29Z</dc:date>
    <item>
      <title>How to flag these by looking at the next row of records?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-these-by-looking-at-the-next-row-of-records/m-p/470771#M285685</link>
      <description>&lt;P&gt;Good Day everyone here.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have this scenario on my hand where i was given a Data that requires me to flag into 5 different criteria.&lt;/P&gt;&lt;P&gt;Before i talk about the criteria/rules, let me share the data with you all.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Services	buy_sell	seq_number	trade_id	Type
2009-AAA	Sell		0		100		D
2009-AAA	Buy		0		101		D
2009-BBB	Sell		0		102		D
2009-BBB	Buy		0		103		F
2009-BBB	Buy		0		104		F
2010-CCC	Sell		0		105		F
2010-DDD	Buy		0		106		F
2010-DDD	Buy		0		107		F
2010-EEE	Buy		0		108		D
2010-EEE	Sell		0		109		F&lt;BR /&gt;2011-ZZZ        Buy             0               110             F&lt;/PRE&gt;&lt;P&gt;The data type as below:&lt;/P&gt;&lt;P&gt;Services 20 Character&lt;/P&gt;&lt;P&gt;buy_sell 5 Character&lt;/P&gt;&lt;P&gt;seq_number 8 num&lt;/P&gt;&lt;P&gt;trade_id 10 Character&lt;/P&gt;&lt;P&gt;Type 1 Character&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now here are the Criterias:&lt;/P&gt;&lt;P&gt;1) list out the &lt;U&gt;Services&lt;/U&gt; that has &lt;U&gt;Buy_sell&lt;/U&gt; Buy and Sell &amp;amp; D and D as &lt;U&gt;type&lt;/U&gt; (All this in Pair, could just be 1 pair in most cases but also possible to have more than a pair). From the data above, Services 2009-AAA fulfill this criteria. Flag records in this criteria as 'A'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2) list out &lt;U&gt;Services&lt;/U&gt; that has &lt;U&gt;Buy_sell&lt;/U&gt; Buy and Sell &amp;amp; F and D as &lt;U&gt;type&lt;/U&gt;. (All this in Pair, could just be 1 pair in most cases but also possible to have more than a pair). From the data above, Services 2010-EEE fulfill this criteria. Flag records in this criteria as 'B'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3) List out Services that has &lt;U&gt;Buy_sell&lt;/U&gt; Buy and Sell &amp;amp; F and F as &lt;U&gt;type&lt;/U&gt;. (All this in Pair, could just be 1 pair in most cases but also possible to have more than a pair). From the data above, Services 2009-BBB fulfill this criteria BUT with additional trade_id of 104. In this case, mark this 3 records(2009-BBB) as Exception flag. Flag records in this criteria as 'C'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;4) list out &lt;U&gt;Services&lt;/U&gt; that has &lt;U&gt;Buy_sell&lt;/U&gt; Buy and Buy or Sell and Sell &amp;amp; any &lt;U&gt;type&lt;/U&gt;. (All this in Pair, could just be 1 pair in most cases but also possible to have more than a pair). From the data above, Services 2010-DDD fulfill this criteria. Flag records in this criteria as 'D'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;5) List out &lt;U&gt;Services&lt;/U&gt; that only has 1 record alone. From the data above, Services 2011-ZZZ fulfill this criteria. Flag records in this criteria as 'E'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is a big challenge to me as i was thinking to group them first. However, how should i further work from grouped data.&lt;/P&gt;&lt;P&gt;I really hope if anyone can help me with SAS Dataset or Proc Sql or even macro loop if necessary for this case.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A big thanks in advanced.&lt;/P&gt;</description>
      <pubDate>Sat, 16 Jun 2018 08:51:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-these-by-looking-at-the-next-row-of-records/m-p/470771#M285685</guid>
      <dc:creator>imdickson</dc:creator>
      <dc:date>2018-06-16T08:51:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag these by looking at the next row of records?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-these-by-looking-at-the-next-row-of-records/m-p/470843#M285686</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/63520"&gt;@imdickson&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Good Day everyone here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have this scenario on my hand where i was given a Data that requires me to flag into 5 different criteria.&lt;/P&gt;
&lt;P&gt;Before i talk about the criteria/rules, let me share the data with you all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Services	buy_sell	seq_number	trade_id	Type
2009-AAA	Sell		0		100		D
2009-AAA	Buy		0		101		D
2009-BBB	Sell		0		102		D
2009-BBB	Buy		0		103		F
2009-BBB	Buy		0		104		F
2010-CCC	Sell		0		105		F
2010-DDD	Buy		0		106		F
2010-DDD	Buy		0		107		F
2010-EEE	Buy		0		108		D
2010-EEE	Sell		0		109		F&lt;BR /&gt;2011-ZZZ        Buy             0               110             F&lt;/PRE&gt;
&lt;P&gt;The data type as below:&lt;/P&gt;
&lt;P&gt;Services 20 Character&lt;/P&gt;
&lt;P&gt;buy_sell 5 Character&lt;/P&gt;
&lt;P&gt;seq_number 8 num&lt;/P&gt;
&lt;P&gt;trade_id 10 Character&lt;/P&gt;
&lt;P&gt;Type 1 Character&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/63520"&gt;@imdickson&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Please provide a fully tested SAS data step which creates a SAS data set with your sample data. Don't expect us to do this prep work for you.&lt;/P&gt;
&lt;P&gt;It's also always appreciated if you demonstrate some of your own work, i.e. posting some not yet working code or some thought how you would approach this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'd probably go for an array approach:&lt;/P&gt;
&lt;P&gt;First transpose your data to a single observation per Service number, define arrays for the columns and use the WHICHN() WHICHC() functions to find pairs matching your conditions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What are the volumes you're dealing with? How many rows are approximately in your real source data.&lt;/P&gt;</description>
      <pubDate>Sun, 17 Jun 2018 03:15:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-these-by-looking-at-the-next-row-of-records/m-p/470843#M285686</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-06-17T03:15:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag these by looking at the next row of records?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-these-by-looking-at-the-next-row-of-records/m-p/470931#M285687</link>
      <description>&lt;P&gt;It is not clear if pairs should be consecutive or not. In case they don't need to be consecutive:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; data have;
 input Service $ buy_sell $ seq_number trade_id Type :$1.;
 datalines;
2009-AAA Sell 0 100 D
2009-AAA Buy 0 101 D
2009-BBB Sell 0 102 D
2009-BBB Buy 0 103 F
2009-BBB Buy 0 104 F
2010-CCC Sell 0 105 F
2010-DDD Buy 0 106 F
2010-DDD Buy 0 107 F
2010-EEE Buy 0 108 D
2010-EEE Sell 0 109 F
2011-ZZZ Buy 0 110 F
; 

proc sql;
create table pairs as
select unique 
    a.service,
    case when a.buy_sell&amp;lt;b.buy_sell 
        then catx("-", a.buy_sell, b.buy_sell)
        else catx("-", b.buy_sell, a.buy_sell) end as bs_pair length=8,
    case when a.type&amp;lt;b.type 
        then catx("-", a.type, b.type) 
        else catx("-", b.type, a.type) end as type_pair length=3
from 
    have as a left join
    have as b on a.service=b.service
where a.trade_id &amp;gt; b.trade_id
order by service, bs_pair;
create table services as select unique service from have;
quit;

data want;
merge services pairs(in=inp); by service;

exception = not (first.service and last.service);

if inp then do;
    if bs_pair = "Buy-Sell" and type_pair="D-D" then flag = "A";
    else if bs_pair = "Buy-Sell" and type_pair="D-F" then flag = "B";
    else if bs_pair = "Buy-Sell" and type_pair="F-F" then flag = "C";
    else if bs_pair in ("Buy-Buy", "Sell-Sell") then flag = "D";
    end;

else flag = "E";
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 17 Jun 2018 20:39:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-these-by-looking-at-the-next-row-of-records/m-p/470931#M285687</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-06-17T20:39:30Z</dc:date>
    </item>
  </channel>
</rss>

