<?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: populating previous date not working in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/populating-previous-date-not-working/m-p/945320#M370382</link>
    <description>&lt;P&gt;If you want the flags, you can do this&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data demon;
    input subjid $ visitdate : yymmdd10.;
    format visitdate yymmdd10.;
    datalines;
111 2024-07-15
111 2024-07-22
111 2024-08-29
222 2024-07-15
222 2024-07-22
333 2024-07-24
333 2024-08-15
333 2024-09-25

;
run;
proc sort data=demon;by subjid visitdate;run;
data demo1;
    set demon;
    by subjid visitdate;
    retain prev_date;

    
    if first.subjid then do;
        prev_date = visitdate;
    end;

    if not first.id then do;
        if visitdate-lag(visitdate)&amp;gt;25 then flag2=1;;
    end;

    if last.subjid then do;
        if visitdate-prev_date&amp;gt;25 then flag1=1;
    end;
    

    format prev_date yymmdd10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 26 Sep 2024 12:15:10 GMT</pubDate>
    <dc:creator>rudfaden</dc:creator>
    <dc:date>2024-09-26T12:15:10Z</dc:date>
    <item>
      <title>populating previous date not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/populating-previous-date-not-working/m-p/945312#M370378</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;From below dataset, I want to populate previous date for unique subjid. I use below sas code but it is not working. can you please help me.&lt;/P&gt;
&lt;P&gt;I need to populate&lt;/P&gt;
&lt;P&gt;1) prev_date as separate column&lt;/P&gt;
&lt;P&gt;2)A flag1 column that populate 1 when difference between first visit date and last visit date &amp;gt;25.&lt;/P&gt;
&lt;P&gt;2)A flag2 column that populate 1 when difference between first visit and previous visit date &amp;gt;25.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data demon;
    input subjid $ visitdate : yymmdd10.;
    format visitdate yymmdd10.;
    datalines;
111 2024-07-15
111 2024-07-22
111 2024-08-29
222 2024-07-15
222 2024-07-22
333 2024-07-24
333 2024-08-15
333 2024-09-25

;
run;
proc sort data=demon;by subjid visitdate;run;
data demo1;
    set demon;
    by subjid visitdate;
    retain prev_date;
    
    if first.subjid then do;
        prev_date = .; 
    end;
    else prev_date = lag(visitdate);  
    
    format prev_date yymmdd10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Sep 2024 11:01:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/populating-previous-date-not-working/m-p/945312#M370378</guid>
      <dc:creator>abraham1</dc:creator>
      <dc:date>2024-09-26T11:01:24Z</dc:date>
    </item>
    <item>
      <title>Re: populating previous date not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/populating-previous-date-not-working/m-p/945315#M370379</link>
      <description>&lt;P&gt;LAG will only populate its queue when it is called, so you must restructure your IF:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;prev_date = lag(visitdate);
if first.subjid then prev_date = .;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 Sep 2024 11:55:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/populating-previous-date-not-working/m-p/945315#M370379</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-09-26T11:55:52Z</dc:date>
    </item>
    <item>
      <title>Re: populating previous date not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/populating-previous-date-not-working/m-p/945316#M370380</link>
      <description>&lt;P&gt;You do not lag the first obs.&lt;BR /&gt;&lt;BR /&gt;Try this:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data demon;
    input subjid $ visitdate : yymmdd10.;
    format visitdate yymmdd10.;
    datalines;
111 2024-07-15
111 2024-07-22
111 2024-08-29
222 2024-07-15
222 2024-07-22
333 2024-07-24
333 2024-08-15
333 2024-09-25

;
run;
proc sort data=demon;by subjid visitdate;run;
data demo1;
    set demon;
    by subjid visitdate;
    retain prev_date;

    prev_date = lag(visitdate);

    if first.subjid then do;
        prev_date = .; 
    end;
      
    
    format prev_date yymmdd10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 Sep 2024 11:55:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/populating-previous-date-not-working/m-p/945316#M370380</guid>
      <dc:creator>rudfaden</dc:creator>
      <dc:date>2024-09-26T11:55:58Z</dc:date>
    </item>
    <item>
      <title>Re: populating previous date not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/populating-previous-date-not-working/m-p/945317#M370381</link>
      <description>&lt;P&gt;And since you set prev_date in every observation, the RETAIN is not necessary.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Sep 2024 11:57:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/populating-previous-date-not-working/m-p/945317#M370381</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-09-26T11:57:52Z</dc:date>
    </item>
    <item>
      <title>Re: populating previous date not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/populating-previous-date-not-working/m-p/945320#M370382</link>
      <description>&lt;P&gt;If you want the flags, you can do this&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data demon;
    input subjid $ visitdate : yymmdd10.;
    format visitdate yymmdd10.;
    datalines;
111 2024-07-15
111 2024-07-22
111 2024-08-29
222 2024-07-15
222 2024-07-22
333 2024-07-24
333 2024-08-15
333 2024-09-25

;
run;
proc sort data=demon;by subjid visitdate;run;
data demo1;
    set demon;
    by subjid visitdate;
    retain prev_date;

    
    if first.subjid then do;
        prev_date = visitdate;
    end;

    if not first.id then do;
        if visitdate-lag(visitdate)&amp;gt;25 then flag2=1;;
    end;

    if last.subjid then do;
        if visitdate-prev_date&amp;gt;25 then flag1=1;
    end;
    

    format prev_date yymmdd10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 Sep 2024 12:15:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/populating-previous-date-not-working/m-p/945320#M370382</guid>
      <dc:creator>rudfaden</dc:creator>
      <dc:date>2024-09-26T12:15:10Z</dc:date>
    </item>
  </channel>
</rss>

