Question

I have a data set where I want to delete the maximum year (which is 2014 right now). I search for a syntax where I can delete this maximum year in the data set without having to write if year=2014 then delete - because I don't want to correct it manually when 2015 becomes the new maximum year.

I have tried something like:

data new;
set old;
if max(year) then delete;
run;

and

data new;
set old;
if year=max(year) then delete;
run;

Bu it does not work - the max function don't seem to be the right way to go?

Kind Regards Maria

Was it helpful?

Solution

Or in PROC SQL...

proc sql ;
  select max(year(DATE)) into :YR from old ;
  delete from old where year(DATE) >= &YR ;
quit ;

Creating a new table instead of deleting from old table...

proc sql ;
  select max(year(DATE)) into :YR from old ;
  create table new as select * from old where year(DATE) lt &YR ;
quit ;

OTHER TIPS

SAS works line by line. Because of this you will need a datastep that first pulls out the max date, and then a second datastep where those rows which contain that date are deleted. For example:

data Max_Date (keep = max_date);
 set original_dataset end = eof;
 retain max_date;
 if date > max_date then max_date = date;
 if eof then do;
    output;
 end;
run;

Put the max date into a macro variable then use this to delete the appropriate records

Maria,

If the max date is always going to be the current year for what you are trying to do, you can always write a macro at the beginning to grab the current year to use in a comparison for deleting from your dataset.

    %let numdate = %sysfunc(today(), date9.);
    %let Max_year = %substr(&numdate,6,4);
    %put todays date is &numdate;
    %put max year is &Max_year;

Result from log:

461  %let numdate = %sysfunc(today(), date9.);
462  %let Max_year = %substr(&numdate,6,4);
463  %put todays date is &numdate;
todays date is 15APR2014
464  %put max year is &Max_year;
max year is 2014

Then the code you had comparing with the macro created should do the job.

 data new;
    set old;
    if year = &Max_year then delete;
 run;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top