<?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: Output entire row based on IF statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Output-entire-row-based-on-IF-statement/m-p/342483#M272867</link>
    <description>&lt;P&gt;You will need to add a statement that resets the flag to zero once a suiting condition is met, if you want to repeat the procedure throughout the dataset.&lt;/P&gt;</description>
    <pubDate>Mon, 20 Mar 2017 07:31:07 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2017-03-20T07:31:07Z</dc:date>
    <item>
      <title>Output entire row based on IF statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-entire-row-based-on-IF-statement/m-p/342456#M272859</link>
      <description>&lt;P&gt;Hey there,&lt;BR /&gt;&lt;BR /&gt;I'm trying to output the previous row of a table based on an IF statement. So what I want is to do output the line above if the criteria of the IF statement is met. Something like the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;/P&gt;&lt;P&gt;set want;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;IF (YEAR = FYEAR) and (DATADATE&amp;gt;EVENTDATE) THEN OUTPUT;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;run; quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But of course that is only outputing that row, I want to be able to output the row above in my sorted data set.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;P.s Version 9.4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Mar 2017 04:04:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-entire-row-based-on-IF-statement/m-p/342456#M272859</guid>
      <dc:creator>Tyler_G</dc:creator>
      <dc:date>2017-03-20T04:04:35Z</dc:date>
    </item>
    <item>
      <title>Re: Output entire row based on IF statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-entire-row-based-on-IF-statement/m-p/342457#M272860</link>
      <description>&lt;P&gt;The simple answer, really, is that you can't do it on just the example you've shown. However it may be possible.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you provide some sample data, and how you want the output to look?&lt;/P&gt;</description>
      <pubDate>Mon, 20 Mar 2017 04:07:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-entire-row-based-on-IF-statement/m-p/342457#M272860</guid>
      <dc:creator>LaurieF</dc:creator>
      <dc:date>2017-03-20T04:07:33Z</dc:date>
    </item>
    <item>
      <title>Re: Output entire row based on IF statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-entire-row-based-on-IF-statement/m-p/342458#M272861</link>
      <description>&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;CUSIP&lt;/TD&gt;&lt;TD&gt;DATADATE&lt;/TD&gt;&lt;TD&gt;FYEAR&lt;/TD&gt;&lt;TD&gt;AT&lt;/TD&gt;&lt;TD&gt;CEQ&lt;/TD&gt;&lt;TD&gt;CSHO&lt;/TD&gt;&lt;TD&gt;PRCC_F&lt;/TD&gt;&lt;TD&gt;EVENT_DATE&lt;/TD&gt;&lt;TD&gt;YEAR&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;88630&lt;/TD&gt;&lt;TD&gt;31/10/1989&lt;/TD&gt;&lt;TD&gt;1989&lt;/TD&gt;&lt;TD&gt;143.831&lt;/TD&gt;&lt;TD&gt;110.47&lt;/TD&gt;&lt;TD&gt;13.218&lt;/TD&gt;&lt;TD&gt;15.375&lt;/TD&gt;&lt;TD&gt;25/06/2002&lt;/TD&gt;&lt;TD&gt;2002&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;88630&lt;/TD&gt;&lt;TD&gt;31/10/1990&lt;/TD&gt;&lt;TD&gt;1990&lt;/TD&gt;&lt;TD&gt;181.665&lt;/TD&gt;&lt;TD&gt;134.013&lt;/TD&gt;&lt;TD&gt;13.289&lt;/TD&gt;&lt;TD&gt;16.375&lt;/TD&gt;&lt;TD&gt;25/06/2002&lt;/TD&gt;&lt;TD&gt;2002&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;88630&lt;/TD&gt;&lt;TD&gt;31/10/1991&lt;/TD&gt;&lt;TD&gt;1991&lt;/TD&gt;&lt;TD&gt;247.169&lt;/TD&gt;&lt;TD&gt;158.374&lt;/TD&gt;&lt;TD&gt;13.429&lt;/TD&gt;&lt;TD&gt;24.375&lt;/TD&gt;&lt;TD&gt;25/06/2002&lt;/TD&gt;&lt;TD&gt;2002&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;88630&lt;/TD&gt;&lt;TD&gt;31/10/1992&lt;/TD&gt;&lt;TD&gt;1992&lt;/TD&gt;&lt;TD&gt;240.762&lt;/TD&gt;&lt;TD&gt;182.188&lt;/TD&gt;&lt;TD&gt;13.61&lt;/TD&gt;&lt;TD&gt;36.25&lt;/TD&gt;&lt;TD&gt;25/06/2002&lt;/TD&gt;&lt;TD&gt;2002&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;88630&lt;/TD&gt;&lt;TD&gt;31/10/1993&lt;/TD&gt;&lt;TD&gt;1993&lt;/TD&gt;&lt;TD&gt;280.054&lt;/TD&gt;&lt;TD&gt;220.394&lt;/TD&gt;&lt;TD&gt;27.697&lt;/TD&gt;&lt;TD&gt;36.5&lt;/TD&gt;&lt;TD&gt;25/06/2002&lt;/TD&gt;&lt;TD&gt;2002&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So above is roughly what I'm working with.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;What is want is to test whether FYEAR = YEAR and if EVENT_DATE &amp;lt; DATADATE (therefore if they are in the same year and the data date is after the event date). If this is the case I want to output the previous years data, which is just the row above after it has been sorted. So output format will be the same. Basically I am trying to cull a data set to only include specific rows and that involves taking the row above if the criteria is met.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope that helps, and thank you for the reply.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Mar 2017 04:20:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-entire-row-based-on-IF-statement/m-p/342458#M272861</guid>
      <dc:creator>Tyler_G</dc:creator>
      <dc:date>2017-03-20T04:20:30Z</dc:date>
    </item>
    <item>
      <title>Re: Output entire row based on IF statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-entire-row-based-on-IF-statement/m-p/342461#M272862</link>
      <description>&lt;P&gt;Here is one way it can be done:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data class;
  set sashelp.class;
run;

data want (drop=Next_age);
  merge class
        class (keep=age
               rename=(age=Next_age)
               firstobs=2);
  if Next_age eq 13 then output;
run;

&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Mon, 20 Mar 2017 04:36:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-entire-row-based-on-IF-statement/m-p/342461#M272862</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-03-20T04:36:15Z</dc:date>
    </item>
    <item>
      <title>Re: Output entire row based on IF statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-entire-row-based-on-IF-statement/m-p/342465#M272863</link>
      <description>&lt;P&gt;Can you sort the data in oposit order, than instead output the previos row you need output the next row.&lt;/P&gt;
&lt;P&gt;You can initate a flag off. If condition is true turn the flag on.&lt;/P&gt;
&lt;P&gt;Geting next observation, if flag is in do the output.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Mar 2017 05:21:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-entire-row-based-on-IF-statement/m-p/342465#M272863</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-03-20T05:21:06Z</dc:date>
    </item>
    <item>
      <title>Re: Output entire row based on IF statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-entire-row-based-on-IF-statement/m-p/342468#M272864</link>
      <description>&lt;P&gt;How would I output the next row then? What code do you recommend?&lt;/P&gt;</description>
      <pubDate>Mon, 20 Mar 2017 05:45:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-entire-row-based-on-IF-statement/m-p/342468#M272864</guid>
      <dc:creator>Tyler_G</dc:creator>
      <dc:date>2017-03-20T05:45:32Z</dc:date>
    </item>
    <item>
      <title>Re: Output entire row based on IF statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-entire-row-based-on-IF-statement/m-p/342469#M272865</link>
      <description>&lt;P&gt;Use&amp;nbsp;next pseudo code order:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; retain flag 0;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;STRONG&gt; if flag=1 then output;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if &amp;lt;condition&amp;gt; is true then flag=1;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .... any other code with current row ...&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Mar 2017 05:50:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-entire-row-based-on-IF-statement/m-p/342469#M272865</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-03-20T05:50:15Z</dc:date>
    </item>
    <item>
      <title>Re: Output entire row based on IF statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-entire-row-based-on-IF-statement/m-p/342476#M272866</link>
      <description>&lt;P&gt;This only worked for the first set of dates. Then for the rest nothing happened. So it only cut off a few data from the top. Additionally I only want it to output the row previous, not all rows previous. So just the one directly before (or after in descending order).&lt;/P&gt;</description>
      <pubDate>Mon, 20 Mar 2017 06:30:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-entire-row-based-on-IF-statement/m-p/342476#M272866</guid>
      <dc:creator>Tyler_G</dc:creator>
      <dc:date>2017-03-20T06:30:18Z</dc:date>
    </item>
    <item>
      <title>Re: Output entire row based on IF statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-entire-row-based-on-IF-statement/m-p/342483#M272867</link>
      <description>&lt;P&gt;You will need to add a statement that resets the flag to zero once a suiting condition is met, if you want to repeat the procedure throughout the dataset.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Mar 2017 07:31:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-entire-row-based-on-IF-statement/m-p/342483#M272867</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-03-20T07:31:07Z</dc:date>
    </item>
    <item>
      <title>Re: Output entire row based on IF statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-entire-row-based-on-IF-statement/m-p/342580#M272868</link>
      <description>&lt;P&gt;My previous example may not have been sufficiently clear or I don't understand what you are trying to do.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I changed one record (#4) in your data so that at least one record would meet the conditions (i.e., record #4). The code outputs record number 3.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
  informat DATADATE EVENT_DATE anydtdte10.;
  format DATADATE EVENT_DATE date9.;
  infile cards dlm=',';
  input CUSIP DATADATE FYEAR AT CEQ CSHO PRCC_F EVENT_DATE YEAR;
  cards;
88630,31/10/1989,1989,143.831,110.47,13.218,15.375,25/06/2002,2002
88630,31/10/1990,1990,181.665,134.013,13.289,16.375,25/06/2002,2002
88630,31/10/1991,1991,247.169,158.374,13.429,24.375,25/06/2002,2002
88630,31/10/1992,1992,240.762,182.188,13.61,36.25,25/06/1992,1992
88630,31/10/1993,1993,280.054,220.394,27.697,36.5,25/06/2002,2002
;

data want (drop=Next_:);
  merge have
        have (keep=year fyear event_date datadate
               rename=(year=Next_year
                       fyear=Next_fyear
                       event_date=next_event_date
                       datadate=next_datadate)
               firstobs=2);
  if Next_FYEAR = Next_YEAR and Next_EVENT_DATE &amp;lt; Next_DATADATE;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Mar 2017 13:28:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-entire-row-based-on-IF-statement/m-p/342580#M272868</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-03-20T13:28:39Z</dc:date>
    </item>
    <item>
      <title>Re: Output entire row based on IF statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-entire-row-based-on-IF-statement/m-p/342767#M272869</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/130187"&gt;@Tyler_G&lt;/a&gt;&amp;nbsp;wrote: "&lt;SPAN&gt;This only worked for the first set of dates." - and you are right.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;gave the answer.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;here is the fixed code:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
        retain flag 0;
        if flag=1 then output;
        if &amp;lt;condition&amp;gt; is true then flag=1; else flag=0;  /* &amp;lt;&amp;lt;&amp;lt; line changed */
        .... any other code with current row ...
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 Mar 2017 21:08:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-entire-row-based-on-IF-statement/m-p/342767#M272869</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-03-20T21:08:13Z</dc:date>
    </item>
  </channel>
</rss>

