<?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 how to get min and max date in a variable from data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/538746#M148314</link>
    <description>&lt;P&gt;HI all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to get to know the min and max value of date in a variable in data step . I ll share a sample data set and appreciate if some one can help me out.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have applied min max function but it is giving same value to every observation.&amp;nbsp; I am wishing to create two new variable with min(startdate) and max(end date) corresponding to their SUBJID . Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sample -&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SUBJID&amp;nbsp; &amp;nbsp; &amp;nbsp;Visitdate&lt;/P&gt;&lt;P&gt;101-02&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;15AUG17&lt;/P&gt;&lt;P&gt;101-02&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;14AUG18&lt;/P&gt;&lt;P&gt;101-02&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;24Sep17&lt;/P&gt;&lt;P&gt;101-02&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;101-04&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10JAN18&lt;/P&gt;&lt;P&gt;101-04&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;14DEC18&amp;nbsp;&lt;/P&gt;&lt;P&gt;101-04&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;07DEC17&lt;/P&gt;&lt;P&gt;101-04&lt;/P&gt;&lt;P&gt;101-07&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;13MAR17&lt;/P&gt;&lt;P&gt;101-07&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;28JUL17&lt;/P&gt;&lt;P&gt;101-07&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;22APR18&lt;/P&gt;&lt;P&gt;101-07&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;18JAN19&lt;/P&gt;&lt;P&gt;101-07&lt;/P&gt;</description>
    <pubDate>Tue, 26 Feb 2019 19:13:52 GMT</pubDate>
    <dc:creator>shanky_44</dc:creator>
    <dc:date>2019-02-26T19:13:52Z</dc:date>
    <item>
      <title>how to get min and max date in a variable from data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/538746#M148314</link>
      <description>&lt;P&gt;HI all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to get to know the min and max value of date in a variable in data step . I ll share a sample data set and appreciate if some one can help me out.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have applied min max function but it is giving same value to every observation.&amp;nbsp; I am wishing to create two new variable with min(startdate) and max(end date) corresponding to their SUBJID . Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sample -&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SUBJID&amp;nbsp; &amp;nbsp; &amp;nbsp;Visitdate&lt;/P&gt;&lt;P&gt;101-02&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;15AUG17&lt;/P&gt;&lt;P&gt;101-02&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;14AUG18&lt;/P&gt;&lt;P&gt;101-02&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;24Sep17&lt;/P&gt;&lt;P&gt;101-02&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;101-04&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10JAN18&lt;/P&gt;&lt;P&gt;101-04&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;14DEC18&amp;nbsp;&lt;/P&gt;&lt;P&gt;101-04&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;07DEC17&lt;/P&gt;&lt;P&gt;101-04&lt;/P&gt;&lt;P&gt;101-07&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;13MAR17&lt;/P&gt;&lt;P&gt;101-07&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;28JUL17&lt;/P&gt;&lt;P&gt;101-07&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;22APR18&lt;/P&gt;&lt;P&gt;101-07&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;18JAN19&lt;/P&gt;&lt;P&gt;101-07&lt;/P&gt;</description>
      <pubDate>Tue, 26 Feb 2019 19:13:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/538746#M148314</guid>
      <dc:creator>shanky_44</dc:creator>
      <dc:date>2019-02-26T19:13:52Z</dc:date>
    </item>
    <item>
      <title>Re: how to get min and max date in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/538759#M148323</link>
      <description>&lt;P&gt;you can do something like. min and max function in datastep work on rows not columns, probably you are having issue there&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc sql;
select subjid,
 min(date) as min_date format=date9.,
 max(date) as max_date format=date9.
from have
group by subjid;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Feb 2019 19:21:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/538759#M148323</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2019-02-26T19:21:30Z</dc:date>
    </item>
    <item>
      <title>Re: how to get min and max date in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/538762#M148326</link>
      <description>Hi Kiranv&lt;BR /&gt;&lt;BR /&gt;Thanks for your reply, I have to do this in data step, sry that was nt mentioned in problem statment. I think first. last . statment can help in this case. Isn't it ?</description>
      <pubDate>Tue, 26 Feb 2019 19:13:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/538762#M148326</guid>
      <dc:creator>shanky_44</dc:creator>
      <dc:date>2019-02-26T19:13:20Z</dc:date>
    </item>
    <item>
      <title>Re: how to get min and max date in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/538766#M148328</link>
      <description>&lt;P&gt;I think its too bad that you have to do this in a data step. It's very simple in PROC SQL, it's very simple in PROC MEANS, and so I'd urge you to do this calculation in one of those SAS procedures. The other benefit, of course, is that these procedures have been tested by SAS and tested in 14 bazillion real world applications, so you can have confidence in the results. If you write your own code ... well, you run the risk of mistakes creeping into your calculations, and you (not SAS) have to verify you have done it properly.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Feb 2019 19:17:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/538766#M148328</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-02-26T19:17:10Z</dc:date>
    </item>
    <item>
      <title>Re: how to get min and max date in a variable from data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/538775#M148331</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input SUBJID $    Visitdate :date7.;
format visitdate date7.;
cards;
101-02       15AUG17
101-02       14AUG18
101-02       24Sep17
101-02       .
101-04       10JAN18
101-04       14DEC18 
101-04       07DEC17
101-04			.
101-07       13MAR17
101-07       28JUL17
101-07       22APR18
101-07       18JAN19
101-07			.
;


data want;
do  until(last.SUBJID);
set have;
by SUBJID;
mindate=min(Visitdate,mindate);
maxdate=max(Visitdate,maxdate);
end;
drop visitdate;
format mindate maxdate date7.;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Feb 2019 19:50:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/538775#M148331</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-02-26T19:50:33Z</dc:date>
    </item>
    <item>
      <title>Re: how to get min and max date in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/538816#M148359</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I think its too bad that you have to do this in a data step.&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But it's a good learning exercise to understand how a data step works, particularly BY and FIRST/LAST, though contrived of course.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Feb 2019 21:17:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/538816#M148359</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-02-26T21:17:54Z</dc:date>
    </item>
    <item>
      <title>Re: how to get min and max date in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/538845#M148370</link>
      <description>I got your point Paige, really appreciate your input. I also need to understand how Data step can me help me out in similar scenarios. Thanks for you help!</description>
      <pubDate>Tue, 26 Feb 2019 23:00:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/538845#M148370</guid>
      <dc:creator>shanky_44</dc:creator>
      <dc:date>2019-02-26T23:00:32Z</dc:date>
    </item>
    <item>
      <title>Re: how to get min and max date in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/538848#M148371</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;uses DoW loops. If this is for homework this is a dead give away that its not your own work.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You're most likely needing to use RETAIN (implicit or explicit) and FIRST/LAST processing.&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;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=sashelp.class out=class;
by sex;

data want;
set class;

*by group specified;
by sex;

*keep across rows;
retain max_age;

*if first of each Sex group;
if first.sex then max_age = age;
*other records;
else max_age = max(age, max_age);

*if last of group;
if last.sex then output;

*keep only relevant variables;
keep sex age;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Feb 2019 23:15:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/538848#M148371</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-02-26T23:15:00Z</dc:date>
    </item>
    <item>
      <title>Re: how to get min and max date in a variable from data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/539092#M148469</link>
      <description>Hi novinosrin,&lt;BR /&gt;&lt;BR /&gt;Thanks, This worked perfectly.</description>
      <pubDate>Wed, 27 Feb 2019 19:21:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/539092#M148469</guid>
      <dc:creator>shanky_44</dc:creator>
      <dc:date>2019-02-27T19:21:55Z</dc:date>
    </item>
    <item>
      <title>Re: how to get min and max date in a variable from data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/539097#M148470</link>
      <description>&lt;P&gt;Thanks a lot to everyone, I got to know multiple ways of doing one problem and that is beauty of SAS Community.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Feb 2019 19:24:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/539097#M148470</guid>
      <dc:creator>shanky_44</dc:creator>
      <dc:date>2019-02-27T19:24:08Z</dc:date>
    </item>
    <item>
      <title>Re: how to get min and max date in a variable from data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/539098#M148471</link>
      <description>&lt;P&gt;In my humble opinion, Mr Quant genuis&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp; 's recommendation is the best i.e proc means/summary. Just loop a HAVE dataset&lt;/P&gt;
&lt;P&gt;to 50e6 and output. (50 million records)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See the performance difference between sql, datastep and proc means class/by statement.&amp;nbsp; You will decide whether Starbucks or somewhere else&lt;/P&gt;</description>
      <pubDate>Wed, 27 Feb 2019 19:25:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/539098#M148471</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-02-27T19:25:58Z</dc:date>
    </item>
    <item>
      <title>Re: how to get min and max date in a variable from data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/539112#M148482</link>
      <description>&lt;P&gt;I totally agree with you and Mr.Paige, but my focus here to see the&amp;nbsp; use of BY group processing and I ll understand and learn this Proc sql solution in depth to use further.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Feb 2019 20:16:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/539112#M148482</guid>
      <dc:creator>shanky_44</dc:creator>
      <dc:date>2019-02-27T20:16:10Z</dc:date>
    </item>
    <item>
      <title>Re: how to get min and max date in a variable from data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/758990#M239750</link>
      <description>&lt;P&gt;This seems the most straightforward answer so far:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc summary data=sample;&lt;BR /&gt;var date;&lt;BR /&gt;output out=date_range (drop=_type_ _freq_) min=min_date max=max_date;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 03 Aug 2021 10:18:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/758990#M239750</guid>
      <dc:creator>BiFrost</dc:creator>
      <dc:date>2021-08-03T10:18:34Z</dc:date>
    </item>
    <item>
      <title>Re: how to get min and max date in a variable from data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/759003#M239759</link>
      <description>&lt;P&gt;This is the SUMMARY solution:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have;
class subjid;
/* if that explodes your memory, sort first and use BY */
/* if dataset is already sorted, use BY anyway */
/* if you use CLASS with more than one variable, add NWAY to the PROC SUMMARY statement */
var date;
output
  out=want (drop=_type_ _freq_)
  min()=min_date
  max()=max_date
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Aug 2021 12:14:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-min-and-max-date-in-a-variable-from-data-step/m-p/759003#M239759</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-08-03T12:14:53Z</dc:date>
    </item>
  </channel>
</rss>

