P3P0
P3P0

Reputation: 165

Append data or Replace based on column values in SAS

I'm trying to save the results from a forecast(dataset) into a historical dataset in SAS servers. I already have the path of the historical dataset but what I'm trying to do here is to append the results if they don't exist or replace them if they already exist on the historical dataset.

Below is how the table that I want to append/replace looks:

:Agency :Forecast_Week :Date Fc :SubAgency :Value
New 12/26/22 12/27/22 One 3243262
New 12/26/22 12/28/22 One 3242355
New 12/26/22 12/29/22 Two 3225142
New 12/26/22 12/30/22 Two 3234235

So, if the records for the Agency, Forecast Week, Date, SubAgency already exists I want to replace them with the new values but if they don't exist in the historical dataset I want to append them.

Do you know how I can do this?

Upvotes: 1

Views: 344

Answers (1)

Christoffer
Christoffer

Reputation: 346

I did something very similar not that long ago:

proc sql;
    create table temp as
        select *
        from table_old 
        where forecast_week NOT IN(select forecast_week from table_new)
    ;
quit;

/* Append the updated and new values */
data table_old;
    set temp
        table_new
    ;
run;

I hope this helps

I asked a similar question in this post maybe you can get some inspiration there:

SAS EG append new data and overwrite already existing rows

Upvotes: 1

Related Questions