DATA Step, Macro, Functions and more

How to check if new record is added in a sas dataset

Reply
Occasional Contributor
Posts: 15

How to check if new record is added in a sas dataset

Hi ,

I have a requirement in which on daily basis, I have to find if a new record is added in the dataset and if it is added then I have to use that record for further process.

Please let me know how can I find this through SAS code.

Thanks,
SASACC
Super Contributor
Posts: 578

Re: How to check if new record is added in a sas dataset

You'll need to give some specifics around how your data are organized. Some examples would be nice as well.
Frequent Contributor
Posts: 139

Re: How to check if new record is added in a sas dataset

It all depends on how the new record is inserted into your master table and how big your table is. There are many was to accomplish this functionallity.

I would use any of the 4 methods below:
1) I would create a timestamp and the most recent record would be the max timestamp, additional you can create a new record flag from the transaction set.

2) create a new record flag from the incoming data set
See below for a quick code snippet.

3) create an audit data set

4) create a unique sequential id for your master file and store the max value in another table. All inserted record would be incremented by 1 then any ID greater than the max id would be new.



data master_table;
format timestamp datetime21.2;
ID=1;value='HELLO';timestamp=datetime();output;
ID=2;value='WORLD';timestamp=datetime();output;
run;
proc sql;
create table max_id as
select max(id) as max_id
from master_table;
select max_id
into :max_id
from max_id;
quit;

data transaction_table;
value='TEST ';
run;

data master_table;
set master_table (in=inMaster)
transaction_table (in=inTransaction)
;
* create timestamp to find newest record;
* create 1/0 indicator for newest record;
* increment id by 1;
if inTransaction then do;
counter+1;
timestamp=datetime();
new=(inTransaction);
id=&max_id+counter;
end;
run;
Valued Guide
Posts: 2,177

Re: How to check if new record is added in a sas dataset

hope this helps.
you can easily tell the negative.
When a table is updated, a timestamp in its header is updated. The following code assumes the data set you want to check, is named yourlib.yourmem

Proc sql noprint; select modate format= datetime19. into :latest_update from dictionary.tables where libname= 'YOURLIB' and memname= 'YOURMEM' ;
quit ;
%put latest update was &latest_update ;

You can then judge whether this is before or after the last time you reported.

Having the macro variable &latest_update also allows you to use it as a SAS datetime constant, as in these statements

%put represented as %sysfunc( putn( "&latest_update"dt, twmdy ));
%put just as date %sysfunc( putn( "&latest_update"dt,dtdate9));
%put time since latest update is %sysfunc( range( %sysfunc(datetime()), "&latest_update"dt), time. );

Additional information is available from the same dictionary table, including "number of observations or rows of data in the table". That can be extracted in the same kind of way. However, since this data might be updated with a new row and have an old row deleted, the number of rows might not change when a new row is added, so I assumed the "modified date" would provide the safest indication of change.


peterC
Ask a Question
Discussion stats
  • 3 replies
  • 634 views
  • 0 likes
  • 4 in conversation