<?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 Re: How to compare current observation with next observation? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-current-observation-with-next-observation/m-p/820647#M323944</link>
    <description>&lt;P&gt;Sorry Andreas! Please find the enclosed updated code below.&amp;nbsp; Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data work.test2 ;&lt;BR /&gt;infile datalines dsd dlm='|' truncover;&lt;BR /&gt;input subject :$8. folderseq RecordPosition _NAME_ :$10. name :$12.&lt;BR /&gt;age :$30. term :$48. rate :$30.&lt;BR /&gt;;&lt;BR /&gt;datalines4;&lt;BR /&gt;301001|1|1|new_val|Frank|21|headache|6&lt;BR /&gt;301001|1|1|newval|""""""|"21|Modified|20"|"headache|Modified|Headache"|"6|Modified|5"&lt;BR /&gt;301001|1|2|new_val|Frank|20|pain|5&lt;BR /&gt;301001|1|2|newval|""""""|""""""|""""""|""""""&lt;BR /&gt;301002|2|1|new_val|Annie|31|abcd|4&lt;BR /&gt;301002|2|1|newval|""""""|""""""|"abcd|Modified|abc"|""""""&lt;BR /&gt;301003|4|0|new_val|Andy|40|b1|3&lt;BR /&gt;301003|4|0|newval|"Andy|added"|"40|added"|"b1|added"|"3|added"&lt;BR /&gt;;;;;&lt;/P&gt;</description>
    <pubDate>Tue, 28 Jun 2022 06:18:26 GMT</pubDate>
    <dc:creator>Leo_2021</dc:creator>
    <dc:date>2022-06-28T06:18:26Z</dc:date>
    <item>
      <title>How to compare current observation with next observation?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-current-observation-with-next-observation/m-p/820350#M323790</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I want to compare some variables in both observations nearby each time in the transposed dataset.&amp;nbsp; If any variable within name, age, term and rate is different,&amp;nbsp; then add a new column flag on first observation.&lt;/P&gt;&lt;P&gt;eg. Observation1: subject=301001, folderseq=1 recordposition=1 _NAME_=new_val;&lt;/P&gt;&lt;P&gt;Observation2: subject=301001, folderseq=1, recordposition=1 and _NAME_=newval;&amp;nbsp; because the variable age is different.&amp;nbsp; so new variable flag='Modified' will be added onto the record with _NAME_=new_val .&amp;nbsp; Could you please help me to look this?&amp;nbsp; Thank you in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best Regards&lt;/P&gt;&lt;P&gt;Leo&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test1;&lt;BR /&gt;input subject &amp;amp; $8. folderseq RecordPosition _NAME_ &amp;amp; $10. name &amp;amp; $12. age &amp;amp; $30. term &amp;amp; $48. rate &amp;amp; $30.;&lt;BR /&gt;cards;&lt;BR /&gt;301001&amp;nbsp; 1.0&amp;nbsp; 1&amp;nbsp; new_val&amp;nbsp; Frank&amp;nbsp; 21&amp;nbsp; headache&amp;nbsp; 6&lt;BR /&gt;301001&amp;nbsp; 1.0&amp;nbsp; 1&amp;nbsp; newval&amp;nbsp; ""&amp;nbsp; 21|Modified|20&amp;nbsp; headache|Modified|Headache&amp;nbsp; 6|Modified|5&lt;BR /&gt;301001&amp;nbsp; 1.0&amp;nbsp; 2&amp;nbsp; new_val&amp;nbsp; Frank&amp;nbsp; 20&amp;nbsp; pain&amp;nbsp; 5&lt;BR /&gt;301001&amp;nbsp; 1.0&amp;nbsp; 2&amp;nbsp; newval&amp;nbsp; ""&amp;nbsp; ""&amp;nbsp; ""&amp;nbsp; ""&lt;BR /&gt;301002&amp;nbsp; 2.0&amp;nbsp; 1&amp;nbsp; new_val&amp;nbsp; Annie&amp;nbsp; 31&amp;nbsp; abcd&amp;nbsp; 4&lt;BR /&gt;301002&amp;nbsp; 2.0&amp;nbsp; 1&amp;nbsp; newval&amp;nbsp; ""&amp;nbsp; ""&amp;nbsp; abcd|Modified|abc&amp;nbsp; ""&lt;BR /&gt;301003&amp;nbsp; 4.0&amp;nbsp; 0&amp;nbsp; new_val&amp;nbsp; Andy&amp;nbsp; 40&amp;nbsp; b1&amp;nbsp; 3&lt;BR /&gt;301003&amp;nbsp; 4.0&amp;nbsp; 0&amp;nbsp; newval&amp;nbsp; Andy|added&amp;nbsp; 40|added&amp;nbsp; b1|added&amp;nbsp; 3|added&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 25 Jun 2022 08:52:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compare-current-observation-with-next-observation/m-p/820350#M323790</guid>
      <dc:creator>Leo_2021</dc:creator>
      <dc:date>2022-06-25T08:52:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare current observation with next observation?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-current-observation-with-next-observation/m-p/820465#M323840</link>
      <description>&lt;P&gt;Please provide data in usable form.&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jun 2022 06:40:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compare-current-observation-with-next-observation/m-p/820465#M323840</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-06-27T06:40:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare current observation with next observation?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-current-observation-with-next-observation/m-p/820641#M323940</link>
      <description>&lt;P&gt;data WORK.TEST2;&lt;BR /&gt;infile datalines dsd truncover;&lt;BR /&gt;input subject:$8. folderseq:32. RecordPosition:32. _NAME_:$10. name:$12. age:$30. term:$48. rate:$30.;&lt;BR /&gt;datalines;&lt;BR /&gt;301001 1 1 new_val Frank 21 headache 6&lt;BR /&gt;301001 1 1 newval "" 21|Modified|20 headache|Modified|Headache 6|Modified|5&lt;BR /&gt;301001 1 2 new_val Frank 20 pain 5&lt;BR /&gt;301001 1 2 newval "" "" "" ""&lt;BR /&gt;301002 2 1 new_val Annie 31 abcd 4&lt;BR /&gt;301002 2 1 newval "" "" abcd|Modified|abc ""&lt;BR /&gt;301003 4 0 new_val Andy 40 b1 3&lt;/P&gt;&lt;P&gt;301003 4 0 newval Andy|added 40|added b1|added 3|added&lt;BR /&gt;;;;;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jun 2022 05:33:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compare-current-observation-with-next-observation/m-p/820641#M323940</guid>
      <dc:creator>Leo_2021</dc:creator>
      <dc:date>2022-06-28T05:33:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare current observation with next observation?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-current-observation-with-next-observation/m-p/820646#M323943</link>
      <description>&lt;P&gt;Sorry, but there are several problem with the step you have posted, please fix them. Also you want to add the expected result for the data provided.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jun 2022 06:10:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compare-current-observation-with-next-observation/m-p/820646#M323943</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-06-28T06:10:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare current observation with next observation?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-current-observation-with-next-observation/m-p/820647#M323944</link>
      <description>&lt;P&gt;Sorry Andreas! Please find the enclosed updated code below.&amp;nbsp; Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data work.test2 ;&lt;BR /&gt;infile datalines dsd dlm='|' truncover;&lt;BR /&gt;input subject :$8. folderseq RecordPosition _NAME_ :$10. name :$12.&lt;BR /&gt;age :$30. term :$48. rate :$30.&lt;BR /&gt;;&lt;BR /&gt;datalines4;&lt;BR /&gt;301001|1|1|new_val|Frank|21|headache|6&lt;BR /&gt;301001|1|1|newval|""""""|"21|Modified|20"|"headache|Modified|Headache"|"6|Modified|5"&lt;BR /&gt;301001|1|2|new_val|Frank|20|pain|5&lt;BR /&gt;301001|1|2|newval|""""""|""""""|""""""|""""""&lt;BR /&gt;301002|2|1|new_val|Annie|31|abcd|4&lt;BR /&gt;301002|2|1|newval|""""""|""""""|"abcd|Modified|abc"|""""""&lt;BR /&gt;301003|4|0|new_val|Andy|40|b1|3&lt;BR /&gt;301003|4|0|newval|"Andy|added"|"40|added"|"b1|added"|"3|added"&lt;BR /&gt;;;;;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jun 2022 06:18:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compare-current-observation-with-next-observation/m-p/820647#M323944</guid>
      <dc:creator>Leo_2021</dc:creator>
      <dc:date>2022-06-28T06:18:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare current observation with next observation?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-current-observation-with-next-observation/m-p/821221#M324215</link>
      <description>&lt;P&gt;Could you please help me to look this again?&amp;nbsp;&amp;nbsp; any suggestions would be appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Leo&lt;/P&gt;</description>
      <pubDate>Fri, 01 Jul 2022 01:50:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compare-current-observation-with-next-observation/m-p/821221#M324215</guid>
      <dc:creator>Leo_2021</dc:creator>
      <dc:date>2022-07-01T01:50:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare current observation with next observation?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-current-observation-with-next-observation/m-p/821238#M324219</link>
      <description>&lt;P&gt;All of the data you provided is different. So as you described the rule the new flag should be true for EVERY observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you provide some data that can actually be used the test whether the code can tell the difference between values that change and those that don't?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also make sure to provide the answer key.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Jul 2022 03:16:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compare-current-observation-with-next-observation/m-p/821238#M324219</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-07-01T03:16:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare current observation with next observation?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-current-observation-with-next-observation/m-p/821239#M324220</link>
      <description>&lt;P&gt;What are the key variables here? How do you want to determine which observations should be compared?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If is much easier to compare the current observation's values to the previous observations value than the next value.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is much easier for the data step to remember what it has seen than to predict the future.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Jul 2022 03:20:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compare-current-observation-with-next-observation/m-p/821239#M324220</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-07-01T03:20:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare current observation with next observation?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-current-observation-with-next-observation/m-p/821243#M324221</link>
      <description>&lt;P&gt;Are you trying to do something like this?&lt;BR /&gt;So you have some data that is keyed by SUBJECT SEQ POS.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input subject $ seq pos name $ age term $ rate ;
cards;
301001 1 1 Frank 21 headache 6
301001 1 2 Frank 20 pain     5
301002 2 1 Annie 31 abcd     4
301003 4 0 Andy  40 b1       3
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And you want to check if there are changes in NAME AGE TERM or RATE.&lt;/P&gt;
&lt;P&gt;So you might want to make a check like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro check(var);
  _name_="&amp;amp;var";
  change=' ';
  if &amp;amp;var ne lag_&amp;amp;var then do;
     if missing(lag_&amp;amp;var) then change = catx('|',&amp;amp;var,'added');
     else if missing(&amp;amp;var) then change = catx('|','removed',lag_&amp;amp;var);
     else change=catx('|',&amp;amp;var,'replaced',lag_&amp;amp;var);
  end;
  output;
%mend check;


data diffs ;
  set have;
  by subject seq pos ;
  lag_name=lag(name);
  lag_age=lag(age);
  lag_term=lag(term);
  lag_rate=lag(rate);
  if not first.seq ;
  length _name_ $32 change $100 ;
  %check(name)
  %check(age)
  %check(term)
  %check(rate)
  keep subject seq pos _name_ change ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which you can then transpose&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=diffs out=wide(drop=_name_) suffix=_change;
  by subject seq pos;
  id _name_;
  var change;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And merge with the original data&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge have wide;
  by subject seq pos;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1656647292986.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/72886iA21D64ABCF67C631/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1656647292986.png" alt="Tom_0-1656647292986.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Jul 2022 03:48:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compare-current-observation-with-next-observation/m-p/821243#M324221</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-07-01T03:48:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare current observation with next observation?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-current-observation-with-next-observation/m-p/821250#M324225</link>
      <description>&lt;P&gt;Hello, Thank you. I have updated this data. I hope to add a new variable flag=M or flag=A to previous observation when there is delimiter('|') and word(Modified or added) at current observation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data work.data_1 ;&lt;BR /&gt;infile datalines dsd dlm='|' truncover;&lt;BR /&gt;input subjectid :$8. folderseq RecordPosition subjectname :$12.&lt;BR /&gt;age :$15. term :$48. rate :$15.&lt;BR /&gt;;&lt;BR /&gt;datalines4;&lt;BR /&gt;301001|1|1|Frank|20|headache|6&lt;BR /&gt;301001|1|1|Frank|20|"headache|Modified|Headache"|6&lt;BR /&gt;301001|1|2|Frank|20|Stomachache|5&lt;BR /&gt;301001|1|2|Frank|20|Stomachache|5&lt;BR /&gt;301001|1|3|Frank|20|Insomnia|5&lt;BR /&gt;301001|1|3|"Frank|added"|"20|added"|"Insomnia|added"|"5|added"&lt;BR /&gt;301002|2|1|Annie|31|fever|4&lt;BR /&gt;301002|2|1|Annie|31|fever|"4|Modified|5"&lt;BR /&gt;301002|2|2|Annie|31|Cough|4&lt;BR /&gt;301002|2|2|Annie|31|Cough|4&lt;BR /&gt;301004|4|0|Andy|40|b1|3&lt;BR /&gt;301004|4|0|"Andy|added"|"40|added"|"b1|added"|"3|added"&lt;BR /&gt;;;;;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Jul 2022 05:44:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compare-current-observation-with-next-observation/m-p/821250#M324225</guid>
      <dc:creator>Leo_2021</dc:creator>
      <dc:date>2022-07-01T05:44:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare current observation with next observation?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-current-observation-with-next-observation/m-p/821264#M324232</link>
      <description>&lt;P&gt;Thank you for your help! you are right. the key variables are separately subject, folderseq and recordposition.&lt;/P&gt;&lt;P&gt;the other variables (name, age, term and rate )will be compared at two different datasets.&lt;/P&gt;&lt;P&gt;actually, I need to review routinely datasets current and previous datasets.&amp;nbsp; The frequency is each month. To find out any update record, added record and deleted record from current dataset after comparing with previous dataset.&amp;nbsp; I have written some code. but I do not know how to put a flag to current dataset to display updated record/new record/removed record.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Jul 2022 09:29:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compare-current-observation-with-next-observation/m-p/821264#M324232</guid>
      <dc:creator>Leo_2021</dc:creator>
      <dc:date>2022-07-01T09:29:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare current observation with next observation?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-current-observation-with-next-observation/m-p/821272#M324235</link>
      <description>&lt;P&gt;Yes. I did.&amp;nbsp; but it does not work. how to put this flag to corresponding records(updated, added and deleted) onto this NEW's dataset? Could you please help me to look this? Thank you in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data work.OLD ;&lt;BR /&gt;infile datalines dsd dlm='|' truncover;&lt;BR /&gt;input name :$12. age subject :$8. folderseq RecordPosition term :$48.&lt;BR /&gt;rate&lt;BR /&gt;;&lt;BR /&gt;datalines4;&lt;BR /&gt;Frank|20|301001|1|1|Headache|5&lt;BR /&gt;Frank|20|301001|1|2|pain|5&lt;BR /&gt;Annie|31|301002|2|1|abc|4&lt;BR /&gt;Annie|31|301002|2|2|abcd|4&lt;BR /&gt;Jessica|38|301005|5|0|a1|3&lt;BR /&gt;;;;;&lt;/P&gt;&lt;P&gt;data work.NEW ;&lt;BR /&gt;infile datalines dsd dlm='|' truncover;&lt;BR /&gt;input name :$12. age subject :$8. folderseq RecordPosition term :$48.&lt;BR /&gt;rate&lt;BR /&gt;;&lt;BR /&gt;datalines4;&lt;BR /&gt;Frank|21|301001|1|1|headache|6&lt;BR /&gt;Frank|20|301001|1|2|pain|5&lt;BR /&gt;Annie|31|301002|2|1|abcd|4&lt;BR /&gt;Annie|31|301002|2|2|abcd|4&lt;BR /&gt;Andy|40|301004|4|0|b1|3&lt;BR /&gt;;;;;&lt;/P&gt;&lt;P&gt;data have1;&lt;BR /&gt;set OLD;&lt;BR /&gt;run;&lt;BR /&gt;data have2;&lt;BR /&gt;set NEW;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%let key_col=subject folderseq RecordPosition;&lt;BR /&gt;/* %let compare_col=name age sex rate; */&lt;BR /&gt;%let key_col_cnt=%sysfunc(countw(&amp;amp;key_col));&lt;BR /&gt;/* Code to find compare column list - START */&lt;BR /&gt;proc contents data=have2 out=contents2 noprint;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%macro where_cond;&lt;BR /&gt;%do i=1 %to &amp;amp;key_col_cnt;&lt;BR /&gt;%if &amp;amp;i &amp;lt; &amp;amp;key_col_cnt %then&lt;BR /&gt;%do;&lt;BR /&gt;%let col=%sysfunc(scan( &amp;amp;key_col,&amp;amp;i));&lt;BR /&gt;"&amp;amp;col" ","&lt;BR /&gt;%end;&lt;BR /&gt;%else&lt;BR /&gt;%do;&lt;BR /&gt;%let col=%sysfunc(scan( &amp;amp;key_col,&amp;amp;i));&lt;BR /&gt;"&amp;amp;col" ", "&lt;BR /&gt;%end;&lt;BR /&gt;&lt;BR /&gt;%end;&lt;BR /&gt;%mend;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;BR /&gt;select name into :compare_col separated by ' '&lt;BR /&gt;from contents2&lt;BR /&gt;where name not in (%where_cond);&lt;BR /&gt;quit;&lt;BR /&gt;%put COMPARE COLUMN = &amp;amp;compare_col ;&lt;BR /&gt;/* Code to find compare column list - END */&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%macro select_col;&lt;BR /&gt;%do i=1 %to &amp;amp;key_col_cnt;&lt;BR /&gt;%let col=%sysfunc(scan( &amp;amp;key_col,&amp;amp;i));&lt;BR /&gt;coalesce(a.&amp;amp;col,b.&amp;amp;col) as &amp;amp;col,&lt;BR /&gt;%end;&lt;BR /&gt;%mend;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro join_cond;&lt;BR /&gt;%do i=1 %to &amp;amp;key_col_cnt;&lt;BR /&gt;%let col=%sysfunc(scan( &amp;amp;key_col,&amp;amp;i));&lt;BR /&gt;a.&amp;amp;col =b.&amp;amp;col and&lt;BR /&gt;%end;&lt;BR /&gt;%mend;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc sort data=have1 out=have1_sorted;&lt;BR /&gt;by &amp;amp;key_col ;run;&lt;BR /&gt;proc sort data=have2 out=have2_sorted;;&lt;BR /&gt;by &amp;amp;key_col ;run;&lt;/P&gt;&lt;P&gt;proc transpose data=have1 out=have1_trans (rename= (_name_=col_name));&lt;BR /&gt;by &amp;amp;key_col ;&lt;BR /&gt;var &amp;amp;compare_col ;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc transpose data=have2 out=have2_trans(rename= (_name_=col_name)) ;&lt;BR /&gt;by &amp;amp;key_col ;&lt;BR /&gt;var &amp;amp;compare_col;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table compare_table&lt;BR /&gt;as&lt;BR /&gt;select %select_col&lt;BR /&gt;coalesce(a.col_name,b.col_name) as variable_name&lt;BR /&gt;,strip(a.col1) as old_val&lt;BR /&gt;,strip(b.col1) as new_val&lt;BR /&gt;,case when strip(a.col1) &amp;lt;&amp;gt; strip(b.col1) and a.subject= b.subject and a.folderseq =b.folderseq then 'Modified'&lt;BR /&gt;when strip(a.col1) &amp;lt;&amp;gt; strip(b.col1) and (a.subject='' or a.folderseq=.) then 'Added'&lt;BR /&gt;when strip(a.col1) &amp;lt;&amp;gt; strip(b.col1) and (b.subject='' or b.folderseq=.) then 'Deleted' end as remark&lt;/P&gt;&lt;P&gt;from have1_trans a&lt;BR /&gt;full join have2_trans b&lt;BR /&gt;on %join_cond&lt;BR /&gt;a.col_name = b.col_name;&lt;BR /&gt;quit;&lt;BR /&gt;Data compare_table_2;&lt;BR /&gt;set compare_table;&lt;BR /&gt;if remark in ("Added") then newval=strip(strip(new_val)||"|"||"Added");&lt;BR /&gt;else if remark in ("Deleted") then newval=catx('|',"@","Deleted",old_val);&lt;BR /&gt;else if remark in ("Modified") then newval=catx('|',new_val,"Modified",old_val);&lt;BR /&gt;run;&lt;BR /&gt;/* Transpose second time */&lt;BR /&gt;proc transpose data=compare_table_2 out=have2_new_trans_2;&lt;BR /&gt;by subject FolderSeq RecordPosition;&lt;BR /&gt;var new_val newval;&lt;BR /&gt;id variable_name;&lt;BR /&gt;run;&lt;BR /&gt;data temp1;&lt;BR /&gt;set have2_new_trans_2;&lt;BR /&gt;delim='|';&lt;BR /&gt;if scan(age,2,delim)="Modified" then flag='M';&lt;BR /&gt;else if scan(age,2,delim)="Added" then flag='A';&lt;BR /&gt;else if scan(age,2,delim)="Deleted" then flag='D';&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Jul 2022 11:53:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compare-current-observation-with-next-observation/m-p/821272#M324235</guid>
      <dc:creator>Leo_2021</dc:creator>
      <dc:date>2022-07-01T11:53:39Z</dc:date>
    </item>
  </channel>
</rss>

