<?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: First. Last. By in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/First-Last-By/m-p/420960#M103573</link>
    <description>&lt;P&gt;Logically, you may need to insert an OUTPUT statement before those last two lines.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As it stands now, you are changing VACANCY_DAYS to 0 before outputting the final observation in each BY group.&amp;nbsp; That obliterates many of the totals you are accumulating.&amp;nbsp; Output first, then set VACANCY_DAYS to 0.&lt;/P&gt;</description>
    <pubDate>Wed, 13 Dec 2017 19:28:37 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2017-12-13T19:28:37Z</dc:date>
    <item>
      <title>First. Last. By</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-Last-By/m-p/420942#M103565</link>
      <description>&lt;P&gt;Hi, I have the code below, where I sort my dataset by my variables, and then&amp;nbsp;use a data step to split my data into by groups, and create a count, resetting on the by group.&lt;/P&gt;&lt;P&gt;Please see the code below:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SORT DATA = kpi.occ_rent out = occ_rent;
	by property_id date company0 metro;
RUN;
DATA ave_vac_by_pm_market;
	set occ_rent (rename = (company0 = company));
	by property_id date company metro;
	/* Increment the vacancy days by one for each day vacant */
	if active = 1 then
		vacancy_days + 1;
	if active = 0 or occupied0 = 1 then
		vacancy_days = 0;
/* 	if last.metro then */
/* 		vacancy_days = 0; */
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The above code, with the last two steps commented out, works just fine, except it doesn't split my count up every time there is a new "metro". However, if I add the final two steps in, all my "vacancy_days" read as zero. Its almost as though it thinks that each row is a new "metro", even though this isn't the case.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any thoughts on why this may be?&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;</description>
      <pubDate>Wed, 13 Dec 2017 19:04:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-Last-By/m-p/420942#M103565</guid>
      <dc:creator>MikeFranz</dc:creator>
      <dc:date>2017-12-13T19:04:34Z</dc:date>
    </item>
    <item>
      <title>Re: First. Last. By</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-Last-By/m-p/420947#M103567</link>
      <description>&lt;P&gt;Do&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;test = last.metro;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and look at the resulting dataset. It could be that it is (almost) always true.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2017 19:08:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-Last-By/m-p/420947#M103567</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-12-13T19:08:52Z</dc:date>
    </item>
    <item>
      <title>Re: First. Last. By</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-Last-By/m-p/420949#M103568</link>
      <description>Thanks for the reply.&lt;BR /&gt;Yes, it is coming up as true (i.e. 1) in each row. But looking at the dataset I can clearly see that it is not true. The data are sorted, as per the sort step above. Any idea why it is always coming up as true?</description>
      <pubDate>Wed, 13 Dec 2017 19:10:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-Last-By/m-p/420949#M103568</guid>
      <dc:creator>MikeFranz</dc:creator>
      <dc:date>2017-12-13T19:10:50Z</dc:date>
    </item>
    <item>
      <title>Re: First. Last. By</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-Last-By/m-p/420953#M103570</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You do know that last.&lt;EM&gt;last-variable&lt;/EM&gt; is true not when the last variable is on the last observation but when the BY group is on the last observation.&amp;nbsp; In this contrived example, last.x3 is always true because earlier values change frequently.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data x;
   do i = 1 to 10;
      x1 = ceil(12 * uniform(7));
      x2 = ceil(12 * uniform(7));
      x3 = ceil( 2 * uniform(7));
      output;
   end;
run;

proc sort out=x2;
   by x1-x3;
run;
   
data x3;
   set x2;
   by x1-x3;
   last = last.x3;
run;
   
proc print; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Obs     i    x1    x2    x3    last
                                   
  1     6     1    11     2      1 
  2    10     2     1     2      1 
  3     1     4    10     2      1 
  4     9     4    11     1      1 
  5     4     6     9     2      1 
  6     2    10     6     2      1 
  7     3    10     9     1      1 
  8     7    11     8     2      1 
  9     8    11    11     1      1 
 10     5    12     6     2      1 &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2017 19:16:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-Last-By/m-p/420953#M103570</guid>
      <dc:creator>WarrenKuhfeld</dc:creator>
      <dc:date>2017-12-13T19:16:10Z</dc:date>
    </item>
    <item>
      <title>Re: First. Last. By</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-Last-By/m-p/420960#M103573</link>
      <description>&lt;P&gt;Logically, you may need to insert an OUTPUT statement before those last two lines.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As it stands now, you are changing VACANCY_DAYS to 0 before outputting the final observation in each BY group.&amp;nbsp; That obliterates many of the totals you are accumulating.&amp;nbsp; Output first, then set VACANCY_DAYS to 0.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2017 19:28:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-Last-By/m-p/420960#M103573</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-12-13T19:28:37Z</dc:date>
    </item>
    <item>
      <title>Re: First. Last. By</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-Last-By/m-p/420965#M103574</link>
      <description>&lt;P&gt;You should change to a first. logic for the reset of the counter. This is much easier to comprehend for someone who reads the code.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2017 19:40:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-Last-By/m-p/420965#M103574</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-12-13T19:40:47Z</dc:date>
    </item>
  </channel>
</rss>

