<?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. and last. understanding in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/First-and-last-understanding/m-p/829799#M327862</link>
    <description>&lt;P&gt;The&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;IF LAST.PERIOD;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Statement is a &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p1cxl8ifdt8u0gn12wqbji8o5fq1.htm" target="_self"&gt;Subsetting If Statement&lt;/A&gt;. Meaning that anything below it executes only then the condition (last.period = 1) is true. Since there is an implicit output statement at the bottom of the data step, this too executes only when last.period is true.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 23 Aug 2022 06:40:22 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2022-08-23T06:40:22Z</dc:date>
    <item>
      <title>First. and last. understanding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-and-last-understanding/m-p/829798#M327861</link>
      <description>&lt;P&gt;Can someone help me understand the meaning of first. and last. from the below step? In the input dataset it has 900 rows but in the output dataset it has only 200 rows.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My understanding is&amp;nbsp;IF FIRST.PERIOD THEN assign SAN_ACT_KNA = 0 else&amp;nbsp;SAN_ACT_KNA + SAN_ACT. What is the meaning of last.period?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;DATA WORK.INSIGHTKNASA;
     SET WORK.INSIGHTPLT;
     BY REGION YR PERIOD CAT_NM METRIC_LEVEL_NM TARGETTYPE; 
     IF FIRST.PERIOD THEN SAN_ACT_KNA = 0;
     SAN_ACT_KNA + SAN_ACT;
     IF LAST.PERIOD;
     METRIC_LEVEL_NM = 'KNA';
     SAN_ACT = SAN_ACT_KNA;
     CAT_NM = 'OTHER';
RUN;
&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 Aug 2022 06:35:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-and-last-understanding/m-p/829798#M327861</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2022-08-23T06:35:34Z</dc:date>
    </item>
    <item>
      <title>Re: First. and last. understanding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-and-last-understanding/m-p/829799#M327862</link>
      <description>&lt;P&gt;The&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;IF LAST.PERIOD;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Statement is a &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p1cxl8ifdt8u0gn12wqbji8o5fq1.htm" target="_self"&gt;Subsetting If Statement&lt;/A&gt;. Meaning that anything below it executes only then the condition (last.period = 1) is true. Since there is an implicit output statement at the bottom of the data step, this too executes only when last.period is true.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Aug 2022 06:40:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-and-last-understanding/m-p/829799#M327862</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-08-23T06:40:22Z</dc:date>
    </item>
    <item>
      <title>Re: First. and last. understanding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-and-last-understanding/m-p/829802#M327865</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;Is my understanding right on ,&amp;nbsp;&lt;SPAN&gt;IF FIRST.PERIOD THEN assign SAN_ACT_KNA = 0 otherwise SAN_ACT_KNA + SAN_ACT?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Aug 2022 07:07:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-and-last-understanding/m-p/829802#M327865</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2022-08-23T07:07:45Z</dc:date>
    </item>
    <item>
      <title>Re: First. and last. understanding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-and-last-understanding/m-p/829807#M327867</link>
      <description>&lt;P&gt;Not exactly.&lt;/P&gt;
&lt;P&gt;If first.period then set &lt;SPAN&gt;SAN_ACT_KNA&amp;nbsp;&lt;/SPAN&gt;to 0.&lt;/P&gt;
&lt;P&gt;For all observations (inluding when first.period is true) perform&amp;nbsp;&lt;SPAN&gt;SAN_ACT_KNA + SAN_ACT.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;So it's not "otherwise" (which would requrie an ELSE statement).&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Aug 2022 07:36:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-and-last-understanding/m-p/829807#M327867</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2022-08-23T07:36:36Z</dc:date>
    </item>
    <item>
      <title>Re: First. and last. understanding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-and-last-understanding/m-p/829809#M327869</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) Read the documentation page which&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;shared.&lt;/P&gt;
&lt;P&gt;2) That data step is "equivalent" to:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA WORK.INSIGHTKNASA;
     SET WORK.INSIGHTPLT;
     BY REGION YR PERIOD CAT_NM METRIC_LEVEL_NM TARGETTYPE; 
     IF FIRST.PERIOD THEN SAN_ACT_KNA = 0;
     SAN_ACT_KNA + SAN_ACT;
     IF LAST.PERIOD then do;
       METRIC_LEVEL_NM = 'KNA';
       SAN_ACT = SAN_ACT_KNA;
       CAT_NM = 'OTHER';
       output;
     end;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 Aug 2022 07:43:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-and-last-understanding/m-p/829809#M327869</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2022-08-23T07:43:37Z</dc:date>
    </item>
    <item>
      <title>Re: First. and last. understanding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-and-last-understanding/m-p/829811#M327870</link>
      <description>&lt;P&gt;It's not exactly "&lt;SPAN&gt;IF FIRST.PERIOD THEN assign SAN_ACT_KNA = 0 &lt;STRONG&gt;otherwise&lt;/STRONG&gt; SAN_ACT_KNA + SAN_ACT&lt;/SPAN&gt;" since the "&lt;SPAN&gt;SAN_ACT_KNA + SAN_ACT&lt;/SPAN&gt;" part is also executed when&amp;nbsp;&lt;SPAN&gt;FIRST.PERIOD=1.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;It's rather:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;IF FIRST.PERIOD THEN 
do;
  SAN_ACT_KNA = 0;
  SAN_ACT_KNA + SAN_ACT;
end;
else 
  SAN_ACT_KNA + SAN_ACT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 Aug 2022 07:46:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-and-last-understanding/m-p/829811#M327870</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2022-08-23T07:46:55Z</dc:date>
    </item>
    <item>
      <title>Re: First. and last. understanding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-and-last-understanding/m-p/829924#M327909</link>
      <description>&lt;P&gt;FIRST.PERIOD is TRUE when this observation is the first for this particular value of PERIOD (within the current values of REGION and YR).&amp;nbsp; So either the previous value of PERIOD was different, or the previous value of YR or REGION was different, or it is the first observation in the dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;LAST.PERIOD is TRUE when this observation is the last for this particular value of PERIOD. So either the next value of PERIOD is different, or the next value of YR or REGION is different, or it is the last observation in the dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The IF/THEN statement sets&amp;nbsp;SAN_ACT_KNA to zero when you start a new group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The SUM statement accumulates the values of&amp;nbsp;&amp;nbsp;SAN_ACT into&amp;nbsp;SAN_ACT_KNA.&amp;nbsp; If runs for every observation so the value of SAN_ACT on the first observation is not skipped.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The subsetting IF statement will stop the step when it is not the last observation for the group.&amp;nbsp; So only one observation per the groups defined by REGION YR and PERIOD will make it into the output datasets.&lt;/P&gt;</description>
      <pubDate>Tue, 23 Aug 2022 16:08:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-and-last-understanding/m-p/829924#M327909</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-08-23T16:08:12Z</dc:date>
    </item>
    <item>
      <title>Re: First. and last. understanding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-and-last-understanding/m-p/829926#M327911</link>
      <description>&lt;P&gt;It's summing SAN_ACT by Region, Year, Period and hardcoding these variables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;     METRIC_LEVEL_NM = 'KNA';
     SAN_ACT = SAN_ACT_KNA;
     CAT_NM = 'OTHER';&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Aug 2022 16:12:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-and-last-understanding/m-p/829926#M327911</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-08-23T16:12:01Z</dc:date>
    </item>
  </channel>
</rss>

