Presumably you have a variable representing the date. You would need to put the data in the proper order first:
proc sort data=have;
by patient aedecod aeterm date;
run;
Then check your conditions:
data want;
set have;
by patient aedecod aeterm;
prior_severity = lag(aesev);
if first.aeterm=0 and (aesev > prior_severity) and (aestdtc < rfstdtc) then flag='Y';
drop prior_severity;
run;
As someone relatively new to SAS, there are several topics you will need to learn, to understand the program. Start with the addition of a BY statement following the SET statement. It's something you will use regularly.
First, however, see if this flags the observations that you are hoping to flag.