Home > Enterprise >  Append data after checking a condition
Append data after checking a condition

Time:12-04

I have a yearly table which on a monthly basis will append data from another table. However, I need to check the max date on the monthly table before appending it. If the max date on monthly is same as YTD, then do not append else append. How can I achieve this in SAS.

I tried using append but don't know how to check the dates before appending.

CodePudding user response:

You can use a macro. First, insert the last year/month id on a variable and preper a macro to execute the append operation:

proc sql;
select max(yourDtcolumn) into : var
from yourTable;
quit;

%macro append;
   proc sql;
     insert into yourtable
      select * from sourcetable;
    quit;
%mend;

then, verify if the variable are the same:


%macro verify;
%if &var > &curMonth %then %do;
    %append;
%end;
%mend;

finally, you call the macro to execute:

%verify;

CodePudding user response:

I'd skip the check, and simply stack the existing data (excluding the current month, if it exists) and the new data.

/* Get period from new data */
proc sql ;
  select min(period) into :LATEST
  from new ;
quit ;

/* Append new to master, and save back master */
data perm.master ;
  set perm.master (where=(period < &LATEST))
      new ;
run ;
  • Related