<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic SAS Macro: How to Increment Version Only If Data Changes in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-Macro-How-to-Increment-Version-Only-If-Data-Changes/m-p/971296#M46050</link>
    <description>&lt;P&gt;Hello SAS Community,&lt;/P&gt;&lt;P&gt;I have an ETL process that loads data into a versioned table (ESPORTA.ANALISI_CREDITI).&lt;BR /&gt;Currently, every time I run the process, a new version is created and appended, even if the data for the same period is identical to the previous version.&lt;/P&gt;&lt;P&gt;What I need:&lt;/P&gt;&lt;P&gt;I want to increment the version and append data only if the new data is different from the previous version for the same period (DATA_REF).&lt;BR /&gt;If the data is identical, I want to avoid creating a new version and not append anything.&lt;BR /&gt;Question:&lt;BR /&gt;What is the best way in SAS to compare the new data with the previous version and only increment the version if there are actual changes?&lt;BR /&gt;Any code example or best practice is welcome!&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
    <pubDate>Tue, 22 Jul 2025 09:38:04 GMT</pubDate>
    <dc:creator>cepp0</dc:creator>
    <dc:date>2025-07-22T09:38:04Z</dc:date>
    <item>
      <title>SAS Macro: How to Increment Version Only If Data Changes</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-Macro-How-to-Increment-Version-Only-If-Data-Changes/m-p/971296#M46050</link>
      <description>&lt;P&gt;Hello SAS Community,&lt;/P&gt;&lt;P&gt;I have an ETL process that loads data into a versioned table (ESPORTA.ANALISI_CREDITI).&lt;BR /&gt;Currently, every time I run the process, a new version is created and appended, even if the data for the same period is identical to the previous version.&lt;/P&gt;&lt;P&gt;What I need:&lt;/P&gt;&lt;P&gt;I want to increment the version and append data only if the new data is different from the previous version for the same period (DATA_REF).&lt;BR /&gt;If the data is identical, I want to avoid creating a new version and not append anything.&lt;BR /&gt;Question:&lt;BR /&gt;What is the best way in SAS to compare the new data with the previous version and only increment the version if there are actual changes?&lt;BR /&gt;Any code example or best practice is welcome!&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Tue, 22 Jul 2025 09:38:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-Macro-How-to-Increment-Version-Only-If-Data-Changes/m-p/971296#M46050</guid>
      <dc:creator>cepp0</dc:creator>
      <dc:date>2025-07-22T09:38:04Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro: How to Increment Version Only If Data Changes</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-Macro-How-to-Increment-Version-Only-If-Data-Changes/m-p/971300#M46053</link>
      <description>&lt;PRE&gt;&lt;CODE class=""&gt;sas
%macro fix_versioning;
%global next_version;
%let next_version = 1;

%put NOTE: === AVVIO MACRO FIX_VERSIONING ===;
%put NOTE: DATA_REF = &amp;amp;DATA_PERIO;
%put NOTE: DT_VERSION = &amp;amp;dt_version;

/* STEP 1: Determina il numero di versione */
%if %sysfunc(exist(ESPORTA.ANALISI_CREDITI)) %then %do;
proc sql noprint;
select max(VERSION) into :last_ver
from ESPORTA.ANALISI_CREDITI
where DATA_REF = &amp;amp;DATA_PERIO_NUM;
quit;

%if %sysevalf(%superq(last_ver) = , boolean) or %sysevalf(&amp;amp;last_ver = ., boolean) %then %do;
%let next_version = 1;
%put NOTE: Prima versione per DATA_REF=&amp;amp;DATA_PERIO;
%end;
%else %do;
%let next_version = %eval(&amp;amp;last_ver + 1);
%put NOTE: Incremento a versione &amp;amp;next_version;
%end;

/* STEP 2: Crea il dataset temporaneo con la versione corretta */
data WORK.ANALISI_CREDITI;
set WORK.ANALISI_CREDITI;
VERSION = &amp;amp;next_version;
DT_VERSION = &amp;amp;dt_version;
DT_VERSION= input(put(DT_VERSION, 8.), yymmdd8.);
format DT_VERSION ddmmyy10.;
VERSION_ID = cats(put(DT_VERSION, yymmddn8.), '_V', put(VERSION, z2.));
run;

/* STEP 3: Backup tabella esistente */
%let timestamp = %sysfunc(datetime(), datetime20.);
%let timestamp = %sysfunc(translate(&amp;amp;timestamp, '_', ' '));
%let timestamp = %sysfunc(translate(&amp;amp;timestamp, '_', ':'));

data ESPORTA.ANALISI_CREDITI_BACKUP;
set ESPORTA.ANALISI_CREDITI;
backup_date = "&amp;amp;timestamp";
run;

/* STEP 4: Append dei nuovi dati */
proc append base=ESPORTA.ANALISI_CREDITI data=WORK.ANALISI_CREDITI force;
run;
%put NOTE: Nuovi dati con versione &amp;amp;next_version aggiunti ad ANALISI_CREDITI esistente;
%end;
%else %do;
/* Se il dataset non esiste, crealo direttamente */
data ESPORTA.ANALISI_CREDITI;
set WORK.ANALISI_CREDITI;
run;
%put NOTE: Creato nuovo dataset ESPORTA.ANALISI_CREDITI (prima versione);
%end;

/* STEP 5: Aggiorna version_history */
%if %sysfunc(exist(ESPORTA.version_history)) %then %do;
proc sql;
insert into ESPORTA.version_history (DT_VERSION, VERSION)
values(&amp;amp;dt_version, &amp;amp;next_version);
quit;
%end;
%else %do;
data ESPORTA.version_history;
length DT_VERSION 8 VERSION 8;
DT_VERSION = &amp;amp;dt_version;
VERSION = &amp;amp;next_version;
output;
run;
%end;

/* STEP 6: Verifica finale */
proc sql;
title "Verifica record dopo aggiornamento";
select DATA_REF, VERSION, count(*) as num_records
from ESPORTA.ANALISI_CREDITI
group by DATA_REF, VERSION
order by DATA_REF, VERSION;
quit;

%put NOTE: === FINE MACRO FIX_VERSIONING - VERSIONE FINALE = &amp;amp;next_version ===;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 22 Jul 2025 09:47:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-Macro-How-to-Increment-Version-Only-If-Data-Changes/m-p/971300#M46053</guid>
      <dc:creator>cepp0</dc:creator>
      <dc:date>2025-07-22T09:47:43Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro: How to Increment Version Only If Data Changes</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-Macro-How-to-Increment-Version-Only-If-Data-Changes/m-p/971301#M46054</link>
      <description>&lt;P&gt;&lt;A href="https://gist.github.com/cepp0/b317784cad6735090c6acdc30475dec0" target="_blank"&gt;https://gist.github.com/cepp0/b317784cad6735090c6acdc30475dec0&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Jul 2025 09:51:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-Macro-How-to-Increment-Version-Only-If-Data-Changes/m-p/971301#M46054</guid>
      <dc:creator>cepp0</dc:creator>
      <dc:date>2025-07-22T09:51:27Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro: How to Increment Version Only If Data Changes</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-Macro-How-to-Increment-Version-Only-If-Data-Changes/m-p/971302#M46055</link>
      <description>&lt;P&gt;The simplest approach would probably be:&lt;/P&gt;
&lt;P&gt;1) create data set with "old" data,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) create data set with "new" data&lt;/P&gt;
&lt;P&gt;3) use proc compare&amp;nbsp;&lt;/P&gt;
&lt;P&gt;4) get value of SYSINFO (&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/v_065/proc/n1jbbrf1tztya8n1tju77t35dej9.htm#n0301m0ic1y4w7n0znjn1mslzi53" target="_blank"&gt;SAS Help Center: Results: PROC COMPARE&lt;/A&gt;)&lt;/P&gt;
&lt;P&gt;5) looking at returned value decide (%if-%then) shoud the data be updated&lt;/P&gt;
&lt;P&gt;6) if "yes" then updata&lt;/P&gt;
&lt;P&gt;7) if "no" then do not update&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Jul 2025 10:03:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-Macro-How-to-Increment-Version-Only-If-Data-Changes/m-p/971302#M46055</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2025-07-22T10:03:50Z</dc:date>
    </item>
  </channel>
</rss>

