03-20-2017 12:04 AM
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:
IF (YEAR = FYEAR) and (DATADATE>EVENTDATE) THEN OUTPUT;
But of course that is only outputing that row, I want to be able to output the row above in my sorted data set.
P.s Version 9.4
03-20-2017 12:07 AM
The simple answer, really, is that you can't do it on just the example you've shown. However it may be possible.
Can you provide some sample data, and how you want the output to look?
03-20-2017 12:20 AM
So above is roughly what I'm working with.
What is want is to test whether FYEAR = YEAR and if EVENT_DATE < 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.
Hope that helps, and thank you for the reply.
03-20-2017 12:34 AM - edited 03-20-2017 12:36 AM
Here is one way it can be done:
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;
Art, CEO, AnalystFinder.com
03-20-2017 01:21 AM
Can you sort the data in oposit order, than instead output the previos row you need output the next row.
You can initate a flag off. If condition is true turn the flag on.
Geting next observation, if flag is in do the output.
03-20-2017 01:50 AM
Use next pseudo code order:
retain flag 0;
if flag=1 then output;
if <condition> is true then flag=1;
.... any other code with current row ...
03-20-2017 02:30 AM
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).
03-20-2017 03:31 AM
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.
03-20-2017 09:28 AM
My previous example may not have been sufficiently clear or I don't understand what you are trying to do.
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.
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 < Next_DATADATE; run;
Art, CEO, AnalystFinder.com
03-20-2017 05:08 PM
@Tyler_G wrote: "This only worked for the first set of dates." - and you are right.
@KurtBremser gave the answer.
here is the fixed code:
data want; set have; retain flag 0; if flag=1 then output; if <condition> is true then flag=1; else flag=0; /* <<< line changed */ .... any other code with current row ... run;