<?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: How to get last data available in a variable by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-last-data-available-in-a-variable-by-group/m-p/405962#M98790</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;: NOOOOO! This can't be&amp;nbsp; that simple ... ok, have to find something else to increase job security &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 20 Oct 2017 14:18:44 GMT</pubDate>
    <dc:creator>error_prone</dc:creator>
    <dc:date>2017-10-20T14:18:44Z</dc:date>
    <item>
      <title>How to get last data available in a variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-last-data-available-in-a-variable-by-group/m-p/405914#M98777</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;I'll try to explain what I mean in the thread title as clear as possible.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a database in SAS 9.4 with +1500 variables and around 200,000 observations. I want to group those observations by ID (around 60,000 unique IDs), ordered by a date and get the last data available for each variable and ID. For example, if my data is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID VAR1 VAR2 DATE (already ordered)&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Day1&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &lt;STRONG&gt;1&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Day2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; .&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;STRONG&gt;2&lt;/STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;STRONG&gt;Day3&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 2&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;STRONG&gt;3&lt;/STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Day1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &lt;STRONG&gt;3&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;STRONG&gt;Day2&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;And I want to get:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;ID VAR1 VAR2 DATE&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;1&amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Day3&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;2&amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Day2&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been using the sentence BY and last.ID/last.Date since most of the data is available in the last observation, but I don't know if it is possible to get into&amp;nbsp;such detail as (I made up the following code in order to try to explain what I need):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;from VAR1 to VAR1500
i = 0
while (last-i).ID (VAR1) = . do
i+1
last.ID (VAR1) = (last-i).ID (VAR1)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I believe it is impossible especially bearing in mind that there will be 60.000 groups of IDs and 1.500 variables&amp;nbsp;making the loop work 90 million times, but I'd appreciate any kind of help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Oct 2017 10:15:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-last-data-available-in-a-variable-by-group/m-p/405914#M98777</guid>
      <dc:creator>DaniLagetsson</dc:creator>
      <dc:date>2017-10-20T10:15:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to get last data available in a variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-last-data-available-in-a-variable-by-group/m-p/405915#M98778</link>
      <description>&lt;P&gt;By group processing, its well documented in the manual.&amp;nbsp; For your issue, sort, then by group:&lt;/P&gt;
&lt;PRE&gt;proc sort data=have;
  by id date;
run;
data want;
  set have;
  by id;
  if last.id;
run;&lt;/PRE&gt;
&lt;P&gt;You could also cheat somewhat by using the way proc sort works:&lt;/P&gt;
&lt;PRE&gt;proc sort data=have out=want nodupkey;
  by id descending date;
run;&lt;/PRE&gt;
&lt;P&gt;Sort keeps the first record in any sequence when nodupkey is on, so descending means last is first record.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Oct 2017 10:21:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-last-data-available-in-a-variable-by-group/m-p/405915#M98778</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-10-20T10:21:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to get last data available in a variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-last-data-available-in-a-variable-by-group/m-p/405924#M98780</link>
      <description>&lt;P&gt;Hi RW9, thank you for your answer.&lt;/P&gt;&lt;P&gt;Nevertheless that will only get the whole last observation, not minding whether a value is missing or not.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Oct 2017 11:43:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-last-data-available-in-a-variable-by-group/m-p/405924#M98780</guid>
      <dc:creator>DaniLagetsson</dc:creator>
      <dc:date>2017-10-20T11:43:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to get last data available in a variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-last-data-available-in-a-variable-by-group/m-p/405925#M98781</link>
      <description>&lt;P&gt;Assuming that all var* are&amp;nbsp; numbers:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   by id;

   length _var1-_var2 i 8;
   retain _var:;
   drop _var: i;

   array v var1-var2;
   array r _var1-_var2;

   do i = 1 to dim(v);
      if not missing(v[i]) then r[i] = v[i];
   end;

   if last.id then do;
      do i = 1 to dim(v);
         v[i] = r[i];
      end;
      output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Oct 2017 11:46:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-last-data-available-in-a-variable-by-group/m-p/405925#M98781</guid>
      <dc:creator>error_prone</dc:creator>
      <dc:date>2017-10-20T11:46:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to get last data available in a variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-last-data-available-in-a-variable-by-group/m-p/405926#M98782</link>
      <description>&lt;P&gt;The basic code for what you want to achieve with one variable is this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have (rename=(var1=_var1));
by id date;
retain var1;
if _var1 ne . then var1 = _var1;
if last.id then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;With macro processing, we can expand this code to deal with an arbitrary number of similarly named variables:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro make_want(numvars);
data want;
set have (rename=(
%do i = 1 %to &amp;amp;numvars;
  var&amp;amp;i.=_var&amp;amp;i.
%end;
));
by id date;
retain
%do i = 1 %to &amp;amp;numvars;
  var&amp;amp;i.
%end;
;
%do i = 1 %to &amp;amp;numvars;
if _var&amp;amp;i. ne . then var&amp;amp;i. = _var&amp;amp;i.;
%end;
if last.id then output;
run;
%mend;
%make_want(1500)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Oct 2017 11:52:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-last-data-available-in-a-variable-by-group/m-p/405926#M98782</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-10-20T11:52:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to get last data available in a variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-last-data-available-in-a-variable-by-group/m-p/405934#M98784</link>
      <description>&lt;P&gt;Ah, sorry, I didn't see the value carry forward.&amp;nbsp; Maybe:&lt;/P&gt;
&lt;PRE&gt;data have;
  input id var1 var2 date $;
datalines;
1 2 3 Day1
1 1 4 Day2
1 . 2 Day3
2 2 3 Day1
2 3 . Day2
;
run;
data want;
  set have;
  by id;
  retain lst_var1 lst_var2;
  if first.id then call missing(lst_var1,lst_var2);
  if var1 ne . then lst_var1=var1;
  if var2 ne . then lst_var2=var2;
  if var1=. then var1=lst_var1;
  if var2=. then var2=lst_var2;
  if last.id;
run;&lt;/PRE&gt;
&lt;P&gt;Could simplfy it a bit.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Oct 2017 12:49:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-last-data-available-in-a-variable-by-group/m-p/405934#M98784</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-10-20T12:49:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to get last data available in a variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-last-data-available-in-a-variable-by-group/m-p/405935#M98785</link>
      <description>&lt;P&gt;SAS actually has tools to make this incredibly simple.&amp;nbsp; Assuming you have already completed this step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=have;&lt;/P&gt;
&lt;P&gt;by id date;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All you need to do from that point is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;update have (obs=0) have;&lt;/P&gt;
&lt;P&gt;by id;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't even need to know all the variable names.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Oct 2017 12:50:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-last-data-available-in-a-variable-by-group/m-p/405935#M98785</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-10-20T12:50:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to get last data available in a variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-last-data-available-in-a-variable-by-group/m-p/405962#M98790</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;: NOOOOO! This can't be&amp;nbsp; that simple ... ok, have to find something else to increase job security &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Oct 2017 14:18:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-last-data-available-in-a-variable-by-group/m-p/405962#M98790</guid>
      <dc:creator>error_prone</dc:creator>
      <dc:date>2017-10-20T14:18:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to get last data available in a variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-last-data-available-in-a-variable-by-group/m-p/406424#M98965</link>
      <description>&lt;P&gt;Thank you all! Alas, each of the 1500 variables have a different name so most of the solutions would take a lot of space to develop, although they'd work. The one from Astounding makes it so simple.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Oct 2017 06:33:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-last-data-available-in-a-variable-by-group/m-p/406424#M98965</guid>
      <dc:creator>DaniLagetsson</dc:creator>
      <dc:date>2017-10-23T06:33:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to get last data available in a variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-last-data-available-in-a-variable-by-group/m-p/414588#M101590</link>
      <description>&lt;P&gt;This can do the job.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;proc sort data=konsort out=konsort;by gruppe;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data last; set konsort;by gruppe;&lt;BR /&gt;if last.gruppe then output;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 18 Nov 2017 09:29:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-last-data-available-in-a-variable-by-group/m-p/414588#M101590</guid>
      <dc:creator>EjnerBørsting</dc:creator>
      <dc:date>2017-11-18T09:29:02Z</dc:date>
    </item>
  </channel>
</rss>

