<?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: Conditionally keep value when compared to previous value that met condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-keep-value-when-compared-to-previous-value-that/m-p/921033#M362739</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id measure_dt :mmddyy. measure_cnt;
format measure_dt mmddyy10.;
datalines;
1 1/22/2008 1
1 4/21/2008 2
1 8/4/2008 3
1 9/17/2008 4
2 5/2/2009 1
2 11/22/2009 2
2 1/9/2010 3
;
run;

data want;
 set have;
 by id;
retain new_index_date ;
if first.id then new_index_date=measure_dt ;
 else days_since_last_index_date=measure_dt-new_index_date;

if days_since_last_index_date&amp;gt;180 then new_index_date=measure_dt ;
format new_index_date mmddyy10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 20 Mar 2024 01:58:33 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2024-03-20T01:58:33Z</dc:date>
    <item>
      <title>Conditionally keep value when compared to previous value that met condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-keep-value-when-compared-to-previous-value-that/m-p/921024#M362735</link>
      <description>&lt;P&gt;I'm trying to keep measurement dates as a new index date if the measurement date is more than 180 days after the last saved index date (i.e., the last measurement date that met the &amp;gt;180 days condition).&amp;nbsp; I'm having trouble iterating this condition every row by the ID group.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have this data:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have;&lt;BR /&gt;input id measure_dt :mmddyy. measure_cnt;&lt;BR /&gt;format measure_dt mmddyy10.;&lt;BR /&gt;datalines;&lt;BR /&gt;1 1/22/2008 1&lt;BR /&gt;1 4/21/2008 2&lt;BR /&gt;1 8/4/2008 3&lt;BR /&gt;1 9/17/2008 4&lt;BR /&gt;2 5/2/2009 1&lt;BR /&gt;2 11/22/2009 2&lt;BR /&gt;2 1/9/2010 3&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want this:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;id&lt;/TD&gt;&lt;TD&gt;measure_dt&lt;/TD&gt;&lt;TD&gt;measure_cnt&lt;/TD&gt;&lt;TD&gt;days since last index date&lt;/TD&gt;&lt;TD&gt;new index date&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1/22/2008&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;1/22/2008&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;4/21/2008&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;90&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;8/4/2008&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;195&lt;/TD&gt;&lt;TD&gt;8/4/2008&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;9/17/2008&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;44&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;5/2/2009&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;5/2/2009&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;11/22/2009&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;204&lt;/TD&gt;&lt;TD&gt;11/22/2009&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1/9/2010&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;48&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried the below code which is getting closer to what I want, but I think there are issues with my do loop as it's outputting multiple rows per each observation that I have.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data want;			
	set have;		
	by id;			
	if first.id then do; 		
		new_index_dt=measure_dt;	
		lag_index=measure_dt;	
		output;	
	end;		
	
	lag_index=lag(new_index_dt);		
	if first.id then lag_index=measure_dt;		
			
	do measure_cnt=2 to max(measure_cnt);		
		days_since_last=measure_dt-lag_index;	
		if days_since_last&amp;gt;=180 then new_index_dt=measure_dt;	
			else if days_since_last&amp;lt;180 then new_index_dt=lag_index;
		lag_index=lag(new_index_dt);	
		output;	
	end;		
	format new_index_dt lag_index mmddyy10.;		
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I'm using SAS EG 8.3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Mar 2024 23:20:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-keep-value-when-compared-to-previous-value-that/m-p/921024#M362735</guid>
      <dc:creator>raj23</dc:creator>
      <dc:date>2024-03-19T23:20:25Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally keep value when compared to previous value that met condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-keep-value-when-compared-to-previous-value-that/m-p/921027#M362736</link>
      <description>&lt;P&gt;A simple RETAIN should do the trick.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id measure_dt :yymmdd. measure_cnt;
  format measure_dt yymmdd10.;
datalines;
1 2008-01-22 1
1 2008-04-21 2
1 2008-08-04 3
1 2008-09-17 4
2 2009-05-02 1
2 2009-11-22 2
2 2010-01-09 3
;

data want;
  set have;
  by id;
  if first.id then index_dt = measure_dt;
  retain index_dt;
  format index_dt yymmdd10.;
  days=measure_dt - index_dt;
  if days &amp;gt; 180 then do;
     index_dt=measure_dt;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;                           measure_
Obs    id    measure_dt       cnt        index_dt    days

 1      1    2008-01-22        1       2008-01-22       0
 2      1    2008-04-21        2       2008-01-22      90
 3      1    2008-08-04        3       2008-08-04     195
 4      1    2008-09-17        4       2008-08-04      44
 5      2    2009-05-02        1       2009-05-02       0
 6      2    2009-11-22        2       2009-11-22     204
 7      2    2010-01-09        3       2009-11-22      48
&lt;/PRE&gt;
&lt;P&gt;I don't understand the missing values in your expected output.&amp;nbsp; Why would you want that?&amp;nbsp; If you want that you will have to make another variable in addition to the one you use to remember (retain) the index date.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Mar 2024 01:39:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-keep-value-when-compared-to-previous-value-that/m-p/921027#M362736</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-03-20T01:39:05Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally keep value when compared to previous value that met condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-keep-value-when-compared-to-previous-value-that/m-p/921031#M362738</link>
      <description>&lt;P&gt;Below code returning zero instead of missings and repeating the values for the new index date - but it wouldn't be hard to add a bit of if/then logic to create the missings instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id measure_dt :mmddyy. measure_cnt;
  format measure_dt mmddyy10.;
  datalines;
1 1/22/2008 1
1 4/21/2008 2
1 8/4/2008 3
1 9/17/2008 4
2 5/2/2009 1
2 11/22/2009 2
2 1/9/2010 3
;
run;

data want;
  set have;
  by id measure_dt;
  attrib days_last_ind label='days since last index date'
         new_ind_date  lable='new index date' format=mmddyy10.
         ;

  retain new_ind_date;

  if first.id then new_ind_date=measure_dt;
  days_last_ind=measure_dt-new_ind_date;
  if days_last_ind&amp;gt;180 then new_ind_date=measure_dt;
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-1710899539003.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/94778i9993CF9EFE673C82/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1710899539003.png" alt="Patrick_0-1710899539003.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Mar 2024 01:52:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-keep-value-when-compared-to-previous-value-that/m-p/921031#M362738</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-03-20T01:52:27Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally keep value when compared to previous value that met condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-keep-value-when-compared-to-previous-value-that/m-p/921033#M362739</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id measure_dt :mmddyy. measure_cnt;
format measure_dt mmddyy10.;
datalines;
1 1/22/2008 1
1 4/21/2008 2
1 8/4/2008 3
1 9/17/2008 4
2 5/2/2009 1
2 11/22/2009 2
2 1/9/2010 3
;
run;

data want;
 set have;
 by id;
retain new_index_date ;
if first.id then new_index_date=measure_dt ;
 else days_since_last_index_date=measure_dt-new_index_date;

if days_since_last_index_date&amp;gt;180 then new_index_date=measure_dt ;
format new_index_date mmddyy10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 20 Mar 2024 01:58:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-keep-value-when-compared-to-previous-value-that/m-p/921033#M362739</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-03-20T01:58:33Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally keep value when compared to previous value that met condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-keep-value-when-compared-to-previous-value-that/m-p/921047#M362743</link>
      <description>&lt;P&gt;For each qualifying observation, generate a _cutoff_date (retained), against which subsequent measure_dt values will be tested:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id measure_dt :mmddyy. measure_cnt;
  format measure_dt mmddyy10.;
datalines;
1 1/22/2008 1
1 4/21/2008 2
1 8/4/2008 3
1 9/17/2008 4
2 5/2/2009 1
2 11/22/2009 2
2 1/9/2010 3
run;

data want (drop=_:);
  set have;
  by id;
  retain _cutoff_date;
  if first.id=1 or measure_dt&amp;gt;_cutoff_date then do;
    new_index_date=measure_dt;
    _cutoff_date=measure_dt+180;
  end;
  format new_index_date mmddyy10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This replicates the results you showed.&amp;nbsp; But if you want to keep the established new_index_date for records within 180 dates, just add new_index_date to the RETAIN statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Mar 2024 04:02:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-keep-value-when-compared-to-previous-value-that/m-p/921047#M362743</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-03-20T04:02:22Z</dc:date>
    </item>
  </channel>
</rss>

