<?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: Identifying if a record changed during a certain time period in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Identifying-if-a-record-changed-during-a-certain-time-period/m-p/951468#M371941</link>
    <description>&lt;P&gt;This also creates your result:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input record $ label $ dtstart :yymmdd10. dtend :yymmdd10. id;
format dtstart yymmdd10. dtend yymmdd10.;
datalines;
H WA123 2010-01-01 2018-07-05 1
C WA123 2018-07-06 9999-12-31 1
H AJ456 2015-02-15 2019-10-30 2
H AJ456 2019-10-31 2022-12-31 2
C AJ456 2023-01-01 9999-12-31 2
H AJ123 2000-01-25 2010-08-20 3
H AJ123 2010-08-21 2020-09-01 3
C AJ124 2020-09-02 9999-12-31 3
;

%let start = %sysfunc(inputn(2018-01-01,yymmdd10.));
%let end = %sysfunc(inputn(2023-12-31,yymmdd10.));

data want;
merge
  have
  have (
    firstobs=2
    keep=id label
    rename=(id=_id label=_label)
  )
;
if
  _id ne id or 
  id = _id and label ne _label and &amp;amp;start. le dtend le &amp;amp;end.
;
drop _id _label;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 21 Nov 2024 13:20:32 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2024-11-21T13:20:32Z</dc:date>
    <item>
      <title>Identifying if a record changed during a certain time period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-if-a-record-changed-during-a-certain-time-period/m-p/951387#M371918</link>
      <description>&lt;P&gt;I have a large dataset with several variables.&amp;nbsp;Where record=C, this is the current accepted Label value.&amp;nbsp;Where record=H these are historical labels.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am having trouble figuring out how to produce a new dataset with the label for every ID that is current (i.e., where record=C), but also if there was a change in label during the time period. Obviously getting where records=C is easy, but that 'if there record value changed during the set time period' part is tripping me up.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In other words, if there were historical changes but the label didn't change, then only the current record is desired. If there was a label change I would want both the current and the historical record to see what the label was changed from .&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Time period of interest 2018-01-01 through 2023-12-31&amp;nbsp; (*note, present record dtend=9999-12-31)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input record $ label $ dtstart :yymmdd10. dtend :yymmdd10. id;
format dtstart yymmdd10. dtend yymmdd10.;
datalines;
H WA123 2010-01-01 2018-07-05 1
C WA123 2018-07-06 9999-12-31 1
H AJ456 2015-02-15 2019-10-30 2
H AJ456 2019-10-31 2022-12-31 2
C AJ456 2023-01-01 9999-12-31 2
H AJ123 2000-01-25 2010-08-20 3
H AJ123 2010-08-21 2020-09-01 3
C AJ124 2020-09-02 9999-12-31 3
;
RUN;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;BR /&gt;data want;
input record $ label $ dtstart :yymmdd10. dtend :yymmdd10. id changeflag;
format dtstart yymmdd10. dtend yymmdd10.;
datalines;
C WA123 2018-07-06 9999-12-31 1 0 
C AJ456 2023-01-01 9999-12-31 2 0 
H AJ123 2010-08-21 2020-09-01 3 1 
C AJ124 2020-09-02 9999-12-31 3 1 
;
RUN;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Does my want make sense?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For IDs 1 and 2 : I don't need previous records because label didn't change.&lt;/P&gt;
&lt;P&gt;For ID 3 I need the current and the previous record (which I would want flagged as being part of a changed set) because they changed during the time period.&amp;nbsp;&amp;nbsp;&lt;CODE class=" language-sas"&gt; &lt;/CODE&gt;&lt;BR /&gt;&lt;BR /&gt;The last caveat, if there were multiple label changes in the time period for an ID I would need to see each of them, not just the most recent.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Nov 2024 21:54:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-if-a-record-changed-during-a-certain-time-period/m-p/951387#M371918</guid>
      <dc:creator>sasgorilla</dc:creator>
      <dc:date>2024-11-20T21:54:26Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying if a record changed during a certain time period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-if-a-record-changed-during-a-certain-time-period/m-p/951425#M371929</link>
      <description>&lt;P&gt;OK. Assuming the data has been sorted as what you showed.&lt;/P&gt;
&lt;P&gt;Otherwise, you need one more PROC SORT.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input record $ label $ dtstart :yymmdd10. dtend :yymmdd10. id;
format dtstart yymmdd10. dtend yymmdd10.;
datalines;
H WA123 2010-01-01 2018-07-05 1
C WA123 2018-07-06 9999-12-31 1
H AJ456 2015-02-15 2019-10-30 2
H AJ456 2019-10-31 2022-12-31 2
C AJ456 2023-01-01 9999-12-31 2
H AJ123 2000-01-25 2010-08-20 3
H AJ123 2010-08-21 2020-09-01 3
C AJ124 2020-09-02 9999-12-31 3
;
RUN;

data temp;
 set have;
 by id label notsorted;
 if last.label;
run;
data want;
 set temp;
 by id;
 changeflag=not (first.id and last.id);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Nov 2024 01:42:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-if-a-record-changed-during-a-certain-time-period/m-p/951425#M371929</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-11-21T01:42:14Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying if a record changed during a certain time period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-if-a-record-changed-during-a-certain-time-period/m-p/951427#M371931</link>
      <description>&lt;P&gt;Thanks, Ksharp.&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;It looks like your code gets me closer, but it isn't accounting for the date range specified. Is there a way I can limit the observations being pulled to only those that occurred within a time window (i.e. 2018-01-01 to 2023-12-31)?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Nov 2024 02:28:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-if-a-record-changed-during-a-certain-time-period/m-p/951427#M371931</guid>
      <dc:creator>sasgorilla</dc:creator>
      <dc:date>2024-11-21T02:28:27Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying if a record changed during a certain time period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-if-a-record-changed-during-a-certain-time-period/m-p/951430#M371932</link>
      <description>&lt;P&gt;I don't understand what you mean.&lt;/P&gt;
&lt;P&gt;You could filter these obs by IF or WHERE statement. Like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input record $ label $ dtstart :yymmdd10. dtend :yymmdd10. id;
format dtstart yymmdd10. dtend yymmdd10.;
if '01jan2018'd&amp;lt;=dtstart&amp;lt;='31dec2023'd ;
datalines;
H WA123 2010-01-01 2018-07-05 1
C WA123 2018-07-06 9999-12-31 1
H AJ456 2015-02-15 2019-10-30 2
H AJ456 2019-10-31 2022-12-31 2
C AJ456 2023-01-01 9999-12-31 2
H AJ123 2000-01-25 2010-08-20 3
H AJ123 2010-08-21 2020-09-01 3
C AJ124 2020-09-02 9999-12-31 3
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Nov 2024 03:34:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-if-a-record-changed-during-a-certain-time-period/m-p/951430#M371932</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-11-21T03:34:42Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying if a record changed during a certain time period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-if-a-record-changed-during-a-certain-time-period/m-p/951431#M371933</link>
      <description>&lt;P&gt;Or you want this statement ?&lt;/P&gt;
&lt;PRE&gt;data have;
input record $ label $ dtstart :yymmdd10. dtend :yymmdd10. id;
format dtstart yymmdd10. dtend yymmdd10.;
if '01jan2018'd&amp;lt;=dtstart or dtend&amp;lt;='31dec2023'd ;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Nov 2024 03:41:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-if-a-record-changed-during-a-certain-time-period/m-p/951431#M371933</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-11-21T03:41:45Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying if a record changed during a certain time period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-if-a-record-changed-during-a-certain-time-period/m-p/951436#M371936</link>
      <description>&lt;P&gt;Very similar to what&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;already proposed. If that doesn't return the desired result then please post amended sample data with the additional cases.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input record $ label $ dtstart :yymmdd10. dtend :yymmdd10. id changeflag;
  format dtstart yymmdd10. dtend yymmdd10.;
  datalines;
H WA123 2010-01-01 2018-07-05 1 .
C WA123 2018-07-06 9999-12-31 1 0
H AJ456 2015-02-15 2019-10-30 2 .
H AJ456 2019-10-31 2022-12-31 2 .
C AJ456 2023-01-01 9999-12-31 2 0
H AJ123 2000-01-25 2010-08-20 3 .
H AJ123 2010-08-21 2020-09-01 3 1
C AJ124 2020-09-02 9999-12-31 3 1
;
RUN;

/*proc sort data=have presorted;*/
/*  by id dtstart;*/
/*run;*/

data want;
  set have(where=(dtstart   between '01jan2018'd and '31dec2023'd 
                  or dtend between '01jan2018'd and '31dec2023'd 
                  or dtstart&amp;lt;'01jan2018'd and dtend='31dec9999'd));
  by id label notsorted;
  retain changeflag_derived 0;
  if last.label then 
    do;
      if not last.id then changeflag_derived= 1;
      output;
      if last.id then changeflag_derived= 0;
    end;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1732169606110.png" style="width: 543px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/102394iFC7EE67C732FB188/image-dimensions/543x125?v=v2" width="543" height="125" role="button" title="Patrick_0-1732169606110.png" alt="Patrick_0-1732169606110.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>Thu, 21 Nov 2024 06:21:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-if-a-record-changed-during-a-certain-time-period/m-p/951436#M371936</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-11-21T06:21:43Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying if a record changed during a certain time period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-if-a-record-changed-during-a-certain-time-period/m-p/951468#M371941</link>
      <description>&lt;P&gt;This also creates your result:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input record $ label $ dtstart :yymmdd10. dtend :yymmdd10. id;
format dtstart yymmdd10. dtend yymmdd10.;
datalines;
H WA123 2010-01-01 2018-07-05 1
C WA123 2018-07-06 9999-12-31 1
H AJ456 2015-02-15 2019-10-30 2
H AJ456 2019-10-31 2022-12-31 2
C AJ456 2023-01-01 9999-12-31 2
H AJ123 2000-01-25 2010-08-20 3
H AJ123 2010-08-21 2020-09-01 3
C AJ124 2020-09-02 9999-12-31 3
;

%let start = %sysfunc(inputn(2018-01-01,yymmdd10.));
%let end = %sysfunc(inputn(2023-12-31,yymmdd10.));

data want;
merge
  have
  have (
    firstobs=2
    keep=id label
    rename=(id=_id label=_label)
  )
;
if
  _id ne id or 
  id = _id and label ne _label and &amp;amp;start. le dtend le &amp;amp;end.
;
drop _id _label;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Nov 2024 13:20:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-if-a-record-changed-during-a-certain-time-period/m-p/951468#M371941</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-11-21T13:20:32Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying if a record changed during a certain time period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-if-a-record-changed-during-a-certain-time-period/m-p/951510#M371943</link>
      <description>&lt;P&gt;This example doesn't do anything to incorporate your data range because I don't see a clear definition of how date range compliance is determined given TWO date variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One thought: Change, just a change, can be done with numeric values using the Range function or summary statistic. If the Range is 0 for set then there was no change. So one approach would be to add a numeric variable and check the range.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
input record $ label $ dtstart :yymmdd10. dtend :yymmdd10. id;
format dtstart yymmdd10. dtend yymmdd10.;
  rec_num = index(record,'C');
datalines;
H WA123 2010-01-01 2018-07-05 1
C WA123 2018-07-06 9999-12-31 1
H AJ456 2015-02-15 2019-10-30 2
H AJ456 2019-10-31 2022-12-31 2
C AJ456 2023-01-01 9999-12-31 2
H AJ123 2000-01-25 2010-08-20 3
H AJ123 2010-08-21 2020-09-01 3
C AJ124 2020-09-02 9999-12-31 3
;
RUN;

proc summary data=have nway;
   class label;
   var rec_num;
   output out=example (drop=_:) range=ChangeFlag;
run;&lt;/PRE&gt;
&lt;P&gt;Where this approach might be of more use is when you have multiple values of the "record" type variable and are interested in a change between group A, B, C and P, D, Q but not within the group.&amp;nbsp; All the A, B, C could be assigned one number and all of P,D, Q a different number.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A potentially useful side effect of this approach given some hierarchy of values or groups of values is the actual range value gives "how far" the&amp;nbsp; values changed though not the direction.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Nov 2024 15:32:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-if-a-record-changed-during-a-certain-time-period/m-p/951510#M371943</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-11-21T15:32:25Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying if a record changed during a certain time period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-if-a-record-changed-during-a-certain-time-period/m-p/951806#M372046</link>
      <description>&lt;P&gt;Thank you, everyone for your help.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I discovered a bit of a problem as I applied several of the variations of code provided that I wasn't aware of initially. That is, the 'ID' for some labels changed over records as well. What I ended up doing was using another variable that was essentially a group name, and then going through the labels and IDs for that group name. I essentially kept all non-duplicated lables rather than IDs, as the labels were ultimately most important.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks again.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2024 14:06:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-if-a-record-changed-during-a-certain-time-period/m-p/951806#M372046</guid>
      <dc:creator>sasgorilla</dc:creator>
      <dc:date>2024-11-25T14:06:15Z</dc:date>
    </item>
  </channel>
</rss>

