<?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: Select last value in calendar year in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Select-last-value-in-calendar-year/m-p/806839#M317966</link>
    <description>&lt;P&gt;Thank you, everyone! They all worked, and i was able to use the template to figure it out how to select the lowest value in a day as well. Appreciate the guidance&lt;/P&gt;</description>
    <pubDate>Fri, 08 Apr 2022 19:41:06 GMT</pubDate>
    <dc:creator>luch25</dc:creator>
    <dc:date>2022-04-08T19:41:06Z</dc:date>
    <item>
      <title>Select last value in calendar year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-last-value-in-calendar-year/m-p/806812#M317954</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I have two questions, but I think they are related in concept and need a similar solution.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. I need to select the last value in a calendar each for each individual and output it into a new dataset.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. I need to select the lowest value in a single day and output that into a new dataset.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I know how to select the last possible value per id, but I'm not sure what to do when it depends on an id and time period, or how to strip that time period from the date given.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have the following example dataset below. I'm so sorry, but i can't get the input of the date value correct. Please let me know how I can fix for the future.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input studyid date value;&lt;BR /&gt;format date datetime22.3;
cards;
1 13JAN2011:07:53:00.000 19
1 20JUL2011:07:30:00.000 24
1 07AUG2014:12:09:06.000 8
1 07AUG2014:15:08.08.000 4
1 09OCT2014:13:42:00.000 10
2 30DEC2004:16:05:00.000 18
2 30DEC2004:19.05:00.000 20
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I would want the datasets to look like this:&lt;/P&gt;
&lt;P&gt;1.&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 195pt;" border="0" width="261" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD width="87" height="21" align="right" style="height: 16.0pt; width: 65pt;"&gt;1&lt;/TD&gt;
&lt;TD width="87" class="xl63" style="width: 65pt;"&gt;2011&lt;/TD&gt;
&lt;TD width="87" align="right" style="width: 65pt;"&gt;24&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" align="right" style="height: 16.0pt;"&gt;1&lt;/TD&gt;
&lt;TD class="xl63"&gt;2014&lt;/TD&gt;
&lt;TD align="right"&gt;10&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" align="right" style="height: 16.0pt;"&gt;2&lt;/TD&gt;
&lt;TD class="xl63"&gt;2004&lt;/TD&gt;
&lt;TD align="right"&gt;20&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2.&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 195pt;" border="0" width="261" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD width="87" height="21" align="right" style="height: 16.0pt; width: 65pt;"&gt;1&lt;/TD&gt;
&lt;TD width="87" class="xl65" style="width: 65pt;"&gt;13JAN2011&lt;/TD&gt;
&lt;TD width="87" align="right" style="width: 65pt;"&gt;19&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" align="right" style="height: 16.0pt;"&gt;1&lt;/TD&gt;
&lt;TD class="xl65"&gt;20JUL2011&lt;/TD&gt;
&lt;TD align="right"&gt;24&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" align="right" style="height: 16.0pt;"&gt;1&lt;/TD&gt;
&lt;TD class="xl65"&gt;07AUG2014&lt;/TD&gt;
&lt;TD align="right"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" align="right" style="height: 16.0pt;"&gt;1&lt;/TD&gt;
&lt;TD class="xl65"&gt;09OCT2014&lt;/TD&gt;
&lt;TD align="right"&gt;10&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" align="right" style="height: 16.0pt;"&gt;2&lt;/TD&gt;
&lt;TD class="xl65"&gt;30DEC2004&lt;/TD&gt;
&lt;TD align="right"&gt;18&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you so much!&lt;/P&gt;</description>
      <pubDate>Fri, 08 Apr 2022 16:59:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-last-value-in-calendar-year/m-p/806812#M317954</guid>
      <dc:creator>luch25</dc:creator>
      <dc:date>2022-04-08T16:59:37Z</dc:date>
    </item>
    <item>
      <title>Re: Select last value in calendar year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-last-value-in-calendar-year/m-p/806817#M317956</link>
      <description>&lt;P&gt;Here's the answer to #1. You can do relatively the same thing for your question #2, so I leave that as a homework assignment for you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
    set have;
    year=year(datepart(date));
run;
data want;
    set have1;
    by studyid year;
    if last.year;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Regarding the code you provided, please test your code first to make sure it works properly. Don't make us debug your code, that's something you should be doing.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Apr 2022 17:06:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-last-value-in-calendar-year/m-p/806817#M317956</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-04-08T17:06:37Z</dc:date>
    </item>
    <item>
      <title>Re: Select last value in calendar year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-last-value-in-calendar-year/m-p/806818#M317957</link>
      <description>&lt;P&gt;Try something like this. I am not able to test this wrigth now. But take it as insperation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sort by id and descending date. Output last date (which is the lowest value as you sorted descending).&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sort data=have;
by studyid descending date;
run;

data want;
set have;
by studyid descending date;
dateNew=datepart(date);
if last.date then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Something like this should work&lt;/P&gt;</description>
      <pubDate>Fri, 08 Apr 2022 17:07:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-last-value-in-calendar-year/m-p/806818#M317957</guid>
      <dc:creator>rudfaden</dc:creator>
      <dc:date>2022-04-08T17:07:13Z</dc:date>
    </item>
    <item>
      <title>Re: Select last value in calendar year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-last-value-in-calendar-year/m-p/806820#M317958</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
  select
    studyid,
    year(datepart(date)) as year,
    value
  from have
  group by studyid, calculated year
  having date = max(date)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested, posted from my tablet.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Apr 2022 17:12:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-last-value-in-calendar-year/m-p/806820#M317958</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-04-08T17:12:15Z</dc:date>
    </item>
    <item>
      <title>Re: Select last value in calendar year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-last-value-in-calendar-year/m-p/806839#M317966</link>
      <description>&lt;P&gt;Thank you, everyone! They all worked, and i was able to use the template to figure it out how to select the lowest value in a day as well. Appreciate the guidance&lt;/P&gt;</description>
      <pubDate>Fri, 08 Apr 2022 19:41:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-last-value-in-calendar-year/m-p/806839#M317966</guid>
      <dc:creator>luch25</dc:creator>
      <dc:date>2022-04-08T19:41:06Z</dc:date>
    </item>
    <item>
      <title>Re: Select last value in calendar year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-last-value-in-calendar-year/m-p/806840#M317967</link>
      <description>&lt;P&gt;This creates the 2 datasets you requested.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;
input studyid timestamp datetime22.3 value;
date = datepart(timestamp);
year = year(date);
format timestamp datetime22. date date.;
cards;
1 13JAN2011:07:53:00.000 19
1 20JUL2011:07:30:00.000 24
1 07AUG2014:12:09:06.000 8
1 07AUG2014:15:08:08.000 4
1 09OCT2014:13:42:00.000 10
2 30DEC2004:16:05:00.000 18
2 30DEC2004:19:05:00.000 20
;
run;

proc sort data=have;
by studyid year;
run;

data want1;
set have;
by studyid year;
if last.year;
keep studyid year value;
run;

proc sort data=have;
by studyid date value;
run;

data want2;
set have;
by studyid date;
if first.date;
keep studyid date value;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Apr 2022 19:56:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-last-value-in-calendar-year/m-p/806840#M317967</guid>
      <dc:creator>average_joe</dc:creator>
      <dc:date>2022-04-08T19:56:32Z</dc:date>
    </item>
  </channel>
</rss>

