<?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: Selecting a customised row based on requirement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Selecting-a-customised-row-based-on-requirement/m-p/463444#M118074</link>
    <description>&lt;P&gt;Thank you all for contributing your thoughts on this. It really helps.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 18 May 2018 20:44:27 GMT</pubDate>
    <dc:creator>Vishy</dc:creator>
    <dc:date>2018-05-18T20:44:27Z</dc:date>
    <item>
      <title>Selecting a customised row based on requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-a-customised-row-based-on-requirement/m-p/463311#M118025</link>
      <description>&lt;P&gt;How can I select the row I needed based on the condition? Below is the requirement. When date1=date3 then I need the row which has previous month date of date2. In the below example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Date1&lt;/TD&gt;&lt;TD&gt;Date2&lt;/TD&gt;&lt;TD&gt;Date3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1001&lt;/TD&gt;&lt;TD&gt;03-Apr-17&lt;/TD&gt;&lt;TD&gt;Dec-16&lt;/TD&gt;&lt;TD&gt;Jul-03&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1001&lt;/TD&gt;&lt;TD&gt;03-Apr-17&lt;/TD&gt;&lt;TD&gt;Jan-17&lt;/TD&gt;&lt;TD&gt;Jul-03&lt;/TD&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1001&lt;/TD&gt;&lt;TD&gt;03-Apr-17&lt;/TD&gt;&lt;TD&gt;Oct-17&lt;/TD&gt;&lt;TD&gt;Apr-17&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1001&lt;/TD&gt;&lt;TD&gt;03-Apr-17&lt;/TD&gt;&lt;TD&gt;Nov-17&lt;/TD&gt;&lt;TD&gt;Apr-17&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1001&lt;/TD&gt;&lt;TD&gt;03-Apr-17&lt;/TD&gt;&lt;TD&gt;Dec-17&lt;/TD&gt;&lt;TD&gt;Apr-17&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Desired Output:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Date1&lt;/TD&gt;&lt;TD&gt;Date2&lt;/TD&gt;&lt;TD&gt;Date3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1001&lt;/TD&gt;&lt;TD&gt;03-Apr-17&lt;/TD&gt;&lt;TD&gt;Mar-17&lt;/TD&gt;&lt;TD&gt;Jul-03&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Fri, 18 May 2018 13:10:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-a-customised-row-based-on-requirement/m-p/463311#M118025</guid>
      <dc:creator>Vishy</dc:creator>
      <dc:date>2018-05-18T13:10:13Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting a customised row based on requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-a-customised-row-based-on-requirement/m-p/463324#M118028</link>
      <description>&lt;P&gt;The condition is month(date1)=date3 actually.&lt;/P&gt;</description>
      <pubDate>Fri, 18 May 2018 13:35:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-a-customised-row-based-on-requirement/m-p/463324#M118028</guid>
      <dc:creator>Vishy</dc:creator>
      <dc:date>2018-05-18T13:35:36Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting a customised row based on requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-a-customised-row-based-on-requirement/m-p/463327#M118030</link>
      <description>&lt;P&gt;Can we interpret your question as "if date1=date3 then output the previous row"? That seems to work (date2 has the previous month) but that may be a concidence in the data. But if so, this might work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set have;
 set have (firstobs=2 rename=(id=id2 date1=date1_2 date2=date2_2 date3=date3_2));
 keep id date1 date2 date3;

 if date1_2=date3_2 then
  output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I must say that in your example the different formatting of date1 and date3&amp;nbsp;is confusing. If they are strings than date1 will never equal date3.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;-- Jan.&lt;/P&gt;</description>
      <pubDate>Fri, 18 May 2018 13:36:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-a-customised-row-based-on-requirement/m-p/463327#M118030</guid>
      <dc:creator>jklaverstijn</dc:creator>
      <dc:date>2018-05-18T13:36:01Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting a customised row based on requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-a-customised-row-based-on-requirement/m-p/463328#M118031</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs;
input (ID	Date1	Date2	Date3) (: $20.);
cards;
1001	03-Apr-17	Dec-16	Jul-03
1001	03-Apr-17	Jan-17	Jul-03
1001	03-Apr-17	Feb-17	Jul-03
1001	03-Apr-17	Mar-17	Jul-03
1001	03-Apr-17	Apr-17	Apr-17
1001	03-Apr-17	May-17	Apr-17
1001	03-Apr-17	Jun-17	Apr-17
1001	03-Apr-17	Jul-17	Apr-17
1001	03-Apr-17	Aug-17	Apr-17
1001	03-Apr-17	Sep-17	Apr-17
1001	03-Apr-17	Oct-17	Apr-17
1001	03-Apr-17	Nov-17	Apr-17
1001	03-Apr-17	Dec-17	Apr-17
;
run;
data temp;
 set have;
 by id;
 if first.id then n=0;
 n+1;
run;
data x;
 set temp;
 by id;
 retain found;
 if first.id then found=0;
 if substr(date1,4)=date3 and not found then do;nn=n-1;found=1;output;end;
 keep nn;
run;
proc sql;
select *
 from temp
  where n in (select nn from x);
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 May 2018 13:36:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-a-customised-row-based-on-requirement/m-p/463328#M118031</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-05-18T13:36:26Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting a customised row based on requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-a-customised-row-based-on-requirement/m-p/463329#M118032</link>
      <description>&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Post test data in the form of a datastep!&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What are those variables, character?&amp;nbsp; If so you would need to convert them or do some string comparison.&amp;nbsp; The rough logic, is hold current record over to next, then do check and only output then e.g.&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  retain lstd1 lstd2 lstd3;
  by id;
  if first.id then do;
    lstd1=date1;
    lstd2=date2;
    lstd3=date3;
  end;
  else do;
    if index(date3,date1) &amp;gt; 0 then output;
    else do;
      lstd1=date1;
      lstd2=date2;
      lstd3=date3;
    end;
  end;
run;

    &lt;/PRE&gt;</description>
      <pubDate>Fri, 18 May 2018 13:36:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-a-customised-row-based-on-requirement/m-p/463329#M118032</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-05-18T13:36:43Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting a customised row based on requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-a-customised-row-based-on-requirement/m-p/463330#M118033</link>
      <description>&lt;P&gt;So how do you tell when date1=date3?&amp;nbsp; The variables don't appear to contain the same values.&amp;nbsp; Since we have no inside knowledge about what is really in these variables, I'm just going to assume that we can look for date1=date3 and that will magically find the proper observations.&amp;nbsp; But that could be just plain wrong and you may need to supply much more information.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;if date1=date3 and _n_ &amp;gt; 1;&lt;/P&gt;
&lt;P&gt;recnum = _n_ - 1;&lt;/P&gt;
&lt;P&gt;set have point=recnum;&lt;/P&gt;
&lt;P&gt;output;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 18 May 2018 13:36:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-a-customised-row-based-on-requirement/m-p/463330#M118033</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-05-18T13:36:54Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting a customised row based on requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-a-customised-row-based-on-requirement/m-p/463335#M118036</link>
      <description>&lt;P&gt;　&lt;/P&gt;&lt;P&gt;&lt;FONT color="#ff0000" face="Courier New" size="3"&gt;Actually&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; if month &amp;amp; year of Date1 = month &amp;amp; year of date3 then I need the row with previous month&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'s of date2. In the below dataset I need the row4.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;date1=Apr17 &amp;amp; date3=Apr17 so I need the previous month row which is date2=Mar17&lt;/P&gt;&lt;P&gt;data check1;&lt;/P&gt;&lt;P&gt;input row ID date1 :date7. date2 :anydtdte. date3 :anydtdte.;&lt;/P&gt;&lt;P&gt;format row 8. date1 date9. date2 monyy5. date3 monyy5. ;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;1 1001 03Apr17 Dec16 Jul03&lt;/P&gt;&lt;P&gt;2 1001 03Apr17 Jan17 Jul03&lt;/P&gt;&lt;P&gt;3 1001 03Apr17 Feb17 Jul03&lt;/P&gt;&lt;P&gt;4 1001 03Apr17 Mar17 Jul03&lt;/P&gt;&lt;P&gt;5 1001 03Apr17 Apr17 Apr17&lt;/P&gt;&lt;P&gt;6 1001 03Apr17 May17 Apr17&lt;/P&gt;&lt;P&gt;7 1001 03Apr17 Jun17 Apr17&lt;/P&gt;&lt;P&gt;8 1001 03Apr17 Jul17 Apr17&lt;/P&gt;&lt;P&gt;9 1001 03Apr17 Aug17 Apr17&lt;/P&gt;&lt;P&gt;10 1001 03Apr17 Sep17 Apr17&lt;/P&gt;&lt;P&gt;11 1001 03Apr17 Oct17 Apr17&lt;/P&gt;&lt;P&gt;12 1001 03Apr17 Nov17 Apr17&lt;/P&gt;&lt;P&gt;13 1001 03Apr17 Dec17 Apr17&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 May 2018 13:45:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-a-customised-row-based-on-requirement/m-p/463335#M118036</guid>
      <dc:creator>Vishy</dc:creator>
      <dc:date>2018-05-18T13:45:20Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting a customised row based on requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-a-customised-row-based-on-requirement/m-p/463341#M118041</link>
      <description>&lt;P&gt;So just to clarify ... you have a match on 9 of the observations.&amp;nbsp; But you only want to output one observation to account for all of these matches.&amp;nbsp; Does that sound right?&lt;/P&gt;</description>
      <pubDate>Fri, 18 May 2018 13:54:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-a-customised-row-based-on-requirement/m-p/463341#M118041</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-05-18T13:54:54Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting a customised row based on requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-a-customised-row-based-on-requirement/m-p/463349#M118046</link>
      <description>&lt;P&gt;Yes, that's correct. thanks&lt;/P&gt;</description>
      <pubDate>Fri, 18 May 2018 14:23:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-a-customised-row-based-on-requirement/m-p/463349#M118046</guid>
      <dc:creator>Vishy</dc:creator>
      <dc:date>2018-05-18T14:23:54Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting a customised row based on requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-a-customised-row-based-on-requirement/m-p/463370#M118049</link>
      <description>&lt;P&gt;OK, easiest is probably to get all 9 matches and then remove duplicates after the fact:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;if _n_ &amp;gt; 1;&lt;/P&gt;
&lt;P&gt;if year(date1) = year(date3) and month(date1) = month(date3);&lt;/P&gt;
&lt;P&gt;look_for = intnx('month', date1, -1);&lt;/P&gt;
&lt;P&gt;do recnum = _n_ - 1 to 1 by -1 until (look_for = intnx('month', date2, 0));&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set have point=recnum;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if look_for = intnx('month', date2, 0) then output;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=want nodupkey;&lt;/P&gt;
&lt;P&gt;by date1 date2 date3;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 May 2018 15:18:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-a-customised-row-based-on-requirement/m-p/463370#M118049</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-05-18T15:18:30Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting a customised row based on requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-a-customised-row-based-on-requirement/m-p/463444#M118074</link>
      <description>&lt;P&gt;Thank you all for contributing your thoughts on this. It really helps.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 May 2018 20:44:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-a-customised-row-based-on-requirement/m-p/463444#M118074</guid>
      <dc:creator>Vishy</dc:creator>
      <dc:date>2018-05-18T20:44:27Z</dc:date>
    </item>
  </channel>
</rss>

