loopsvariablessasbatch-processingauto-increment

Increasing a Batch Routine Number by Timestamp in SAS


I have a routine in SAS that runs intraday, periodically updating data in table A. I would like to identify the batch of data by the timestamp column and increment it with each data update throughout the day.
Example: First routine of the day - entry data:
data

id column_a timestamp
1 Orange Bank 07Apr2025 00:05:15
2 Market Bank 07Apr2025 00:05:15
3 Orange Coins 07Apr2025 00:05:15

Recording output in table A:

id column_a timestamp batch
1 Orange Bank 07Apr2025 00:05:15 1
2 Market Bank 07Apr2025 00:05:15 1
3 Orange Coins 07Apr2025 00:05:15 1

Second routine of the day:
data

id column_a timestamp
4 Black Bank 07Apr2025 00:25:48
5 Joseph House 07Apr2025 00:25:48
6 Blank Mall 07Apr2025 00:25:48

Doing append in table A increasing 1 in batch column by timestamp:

id column_a timestamp batch
4 Black Bank 07Apr2025 00:25:48 2
5 Joseph House 07Apr2025 00:25:48 2
6 Blank Mall 07Apr2025 00:25:48 2

Consolidated table A:

id column_a timestamp batch
1 Orange Bank 07Apr2025 00:05:15 1
2 Market Bank 07Apr2025 00:05:15 1
3 Orange Coins 07Apr2025 00:05:15 1
4 Black Bank 07Apr2025 00:25:48 2
5 Joseph House 07Apr2025 00:25:48 2
6 Blank Mall 07Apr2025 00:25:48 2

And so the routine continues throughout the day, increasing the batch number with each new batch of data... What is the best way to do this?


Solution

  • You can compute the batch number apriori and apply it to the batch data.

    select sum(max(batch),1) into :next_batch_number trimmed from <target> where datepart(timestamp) = today() ;
    
    data batch ;
      set <mydata> ;
      batch = &next_batch_number ;
      timestamp = datetime() ;
    run ;
    
    proc append base=<target> data=batch ;
    run ;