<?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 the last value? in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-value/m-p/765263#M39465</link>
    <description>&lt;P&gt;how about this code?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array q{*} q_:;
  do i=1 to dim(q);
    if q{i}=. then leave;
    last_value=q{i};
  end;
  drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 01 Sep 2021 08:46:34 GMT</pubDate>
    <dc:creator>japelin</dc:creator>
    <dc:date>2021-09-01T08:46:34Z</dc:date>
    <item>
      <title>How to get the last value?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-value/m-p/765262#M39464</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines truncover;
input type $ q_20211 q_20212 q_20213;
datalines;
XX-1 1 2 3
XX-2 1  
XX-3 1 3 
XX-4 3 4 
XX-5 4 2 3
XX-6 4 
XX-7 2 
XX-8 2 1 4&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want to add a new column named `last_value`. This column should contain the last value for every row based on the last quarter. So for example XX-1 its `last_value` is `3`.&amp;nbsp; The last value of XX-6 is `4`. This dataset is continue growing. So there is a possibilty that in quarter 4 the last_value of XX-1 changes...&lt;/P&gt;</description>
      <pubDate>Wed, 01 Sep 2021 08:37:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-value/m-p/765262#M39464</guid>
      <dc:creator>Andalusia</dc:creator>
      <dc:date>2021-09-01T08:37:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the last value?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-value/m-p/765263#M39465</link>
      <description>&lt;P&gt;how about this code?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array q{*} q_:;
  do i=1 to dim(q);
    if q{i}=. then leave;
    last_value=q{i};
  end;
  drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Sep 2021 08:46:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-value/m-p/765263#M39465</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2021-09-01T08:46:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the last value?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-value/m-p/765265#M39466</link>
      <description>&lt;P&gt;Do not use wide datasets, and store dates and date-related values as SAS dates, so you can use them in calculations.&lt;/P&gt;
&lt;P&gt;So, first convert your dataset to something that is much more useful:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines truncover;
input type $ q_20211 q_20212 q_20213;
datalines;
XX-1 1 2 3
XX-2 1  
XX-3 1 3 
XX-4 3 4 
XX-5 4 2 3
XX-6 4 
XX-7 2 
XX-8 2 1 4
;

proc transpose data=have out=long1 (where=(col1 ne .));
by type;
var q:;
run;

data have_long;
set long1;
rename col1=value;
quarter = input(catx("Q",substr(_name_,3,4),substr(_name_,7)),yyq6.);
format quarter yyq6.;
drop _name_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;From that, finding the last is dead simple:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have_long;
by type quarter;
if last.type;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Sep 2021 08:51:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-value/m-p/765265#M39466</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-09-01T08:51:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the last value?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-value/m-p/765270#M39467</link>
      <description>&lt;P&gt;I tried your code on a larger dataset and it doesnt seem to always work.... take a look for example to row 4 and 5 in the red box it says 2. For row 4 the last measured quarter is q_202102 and that has value 1, for row 5 the last measured quarter is q_202103 and that one also has value 1. And what about the empty ones, those should be filled to because the data is available.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Andalusia_0-1630487457269.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/63201i8570E7B0689E8FC7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Andalusia_0-1630487457269.png" alt="Andalusia_0-1630487457269.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Sep 2021 09:11:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-value/m-p/765270#M39467</guid>
      <dc:creator>Andalusia</dc:creator>
      <dc:date>2021-09-01T09:11:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the last value?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-value/m-p/765271#M39468</link>
      <description>&lt;P&gt;How about that:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines truncover;
input type $ q_20211 q_20212 q_20213;
datalines;
XX-1 1 2 3
XX-2 1  
XX-3 1 3 
XX-4 3 4 
XX-5 4 2 3
XX-6 4 
XX-7 2 
XX-8 2 1 4
;
run;
proc print;
run;

data want;
  set have;
  last = coalesce(q_20213,q_20212,q_20211);
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Wed, 01 Sep 2021 09:13:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-value/m-p/765271#M39468</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2021-09-01T09:13:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the last value?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-value/m-p/765272#M39469</link>
      <description>I see your solution, its an option but I really need the quarters to stay as they are. I would like it as the answer of  &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/226565"&gt;@japelin&lt;/a&gt; but then working obviously</description>
      <pubDate>Wed, 01 Sep 2021 09:13:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-value/m-p/765272#M39469</guid>
      <dc:creator>Andalusia</dc:creator>
      <dc:date>2021-09-01T09:13:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the last value?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-value/m-p/765273#M39470</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt; and what if in a few months time I get the data of q_20214? I cant be changing the code every time a new quarter comes in....</description>
      <pubDate>Wed, 01 Sep 2021 09:14:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-value/m-p/765273#M39470</guid>
      <dc:creator>Andalusia</dc:creator>
      <dc:date>2021-09-01T09:14:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the last value?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-value/m-p/765274#M39471</link>
      <description>&lt;P&gt;For human consumption, you can always use PROC REPORT to&amp;nbsp;&lt;EM&gt;display&lt;/EM&gt; the data in a wide layout (or export it to Excel for the &lt;A href="https://en.wikipedia.org/wiki/Pointy-haired_Boss" target="_blank" rel="noopener"&gt;PHB&lt;/A&gt; with ODS), but for most of your work the long dataset layout is the way to go, unless you have a strong masochistic urge in you &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Sep 2021 09:18:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-value/m-p/765274#M39471</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-09-01T09:18:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the last value?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-value/m-p/765275#M39472</link>
      <description>&lt;P&gt;You can always make it "macro-dynamic":&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines truncover;
input type $ q_20211 q_20212 q_20213;
datalines;
XX-1 1 2 3
XX-2 1  
XX-3 1 3 
XX-4 3 4 
XX-5 4 2 3
XX-6 4 
XX-7 2 
XX-8 2 1 4
;
run;
proc print;
run;

proc transpose data = have(obs=0 keep=q_:) out = list(keep=_name_);
  var _all_;
run;

data _null_;
  length list $ 500;
  retain list;
  set list end = end;
  list = catx(",",_NAME_,list);
  if end then call symputx("list", list, "G");
run;

options symbolgen;
data want;
  set have;
  last = coalesce(&amp;amp;list.);
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Wed, 01 Sep 2021 09:23:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-value/m-p/765275#M39472</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2021-09-01T09:23:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the last value?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-value/m-p/765276#M39473</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array q{*} q_:;
  do i= dim(q) to 1 by -1;
    if q{i} ne . then last_value=q{i};
    if q{i} ne . then leave;
  end;
  drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If possible, I would like a few obs of large datasets to check my code works or not.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Sep 2021 09:25:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-value/m-p/765276#M39473</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2021-09-01T09:25:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the last value?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-value/m-p/765277#M39474</link>
      <description>Almost.... I need a dataset in return not a SAS report...</description>
      <pubDate>Wed, 01 Sep 2021 09:25:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-value/m-p/765277#M39474</guid>
      <dc:creator>Andalusia</dc:creator>
      <dc:date>2021-09-01T09:25:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the last value?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-value/m-p/765278#M39475</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/358343"&gt;@Andalusia&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt; and what if in a few months time I get the data of q_20214? I cant be changing the code every time a new quarter comes in....&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That is exactly why you use a long dataset layout; new data is then&amp;nbsp;&lt;EM&gt;data&lt;/EM&gt; and not&amp;nbsp;&lt;EM&gt;structure&lt;/EM&gt;.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Sep 2021 09:25:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-value/m-p/765278#M39475</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-09-01T09:25:53Z</dc:date>
    </item>
  </channel>
</rss>

