<?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. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/First-and-Last/m-p/808352#M318751</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292396"&gt;@David_Billa&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;How does it increment to till 3 ?&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Please do count the datalines containing&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;1 2022 1&lt;/PRE&gt;
&lt;P&gt;Which number do you come up with?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 18 Apr 2022 16:42:46 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2022-04-18T16:42:46Z</dc:date>
    <item>
      <title>First. and Last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-and-Last/m-p/808334#M318739</link>
      <description>&lt;P&gt;I'd like to know what would be the output if I execute the below program with this sample data. I just trying to understand the behavior of&amp;nbsp;First. and Last.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;DATA WORK.MFGTOT;
     SET WORK.MFG;
     BY YR WK PO;
     IF FIRST.PO THEN DO;
          ZNL_TOT = 0;
          CS_TOT = 0;
          LB_TOT = 0;
     END;
    ZNL_TOT + 1;
    CS_TOT + 1;
    LB_TOT + 1;
     IF LAST.PO;
RUN;
&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;Sample data:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PO YR&amp;nbsp; &amp;nbsp; &amp;nbsp;WK&lt;BR /&gt;1&amp;nbsp; &amp;nbsp;2022&amp;nbsp; &amp;nbsp;1 &lt;BR /&gt;1&amp;nbsp; &amp;nbsp;2022&amp;nbsp; &amp;nbsp;1&lt;BR /&gt;1&amp;nbsp; &amp;nbsp;2022&amp;nbsp; &amp;nbsp;1 &lt;BR /&gt;2&amp;nbsp; &amp;nbsp;2022&amp;nbsp; &amp;nbsp;2&lt;BR /&gt;2&amp;nbsp; &amp;nbsp;2022&amp;nbsp; &amp;nbsp;2&lt;BR /&gt;3&amp;nbsp; &amp;nbsp;2022&amp;nbsp; &amp;nbsp;3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2022 15:42:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-and-Last/m-p/808334#M318739</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2022-04-18T15:42:40Z</dc:date>
    </item>
    <item>
      <title>Re: First. and Last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-and-Last/m-p/808337#M318742</link>
      <description>&lt;P&gt;What part of FIRST and LAST do you not understand?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try saving the values to real variables so you can examine them after the data step to see how they work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 length yr wk po 8;
 input PO YR WK ;
cards;
1 2022 1
1 2022 1
1 2022 1
2 2022 2
2 2022 2
3 2022 3
;

data want;
  set have;
  by yr wk po;
  first_yr = first.yr ;
  last_ye = last.yr;
  first_wk = first.wk;
  last_wk = last.wk;
  first_po = first.po ;
  last_po = last.po;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Obs     yr     wk    po    first_yr    last_ye    first_wk    last_wk    first_po    last_po

 1     2022     1     1        1          0           1          0           1          0
 2     2022     1     1        0          0           0          0           0          0
 3     2022     1     1        0          0           0          1           0          1
 4     2022     2     2        0          0           1          0           1          0
 5     2022     2     2        0          0           0          1           0          1
 6     2022     3     3        0          1           1          1           1          1
&lt;/PRE&gt;
&lt;P&gt;Which observation is the first one where the value of YR is 2022?&amp;nbsp; Which is the last?&lt;BR /&gt;Which observation is the first one in the set where YR is 2022 where WK is 1?&amp;nbsp; Which is the last?&lt;/P&gt;
&lt;P&gt;etc.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2022 15:50:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-and-Last/m-p/808337#M318742</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-18T15:50:12Z</dc:date>
    </item>
    <item>
      <title>Re: First. and Last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-and-Last/m-p/808343#M318743</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;I just ran your code and then the code which I posted. Result I got for the new variables are,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;ZNL_TOT&amp;nbsp; &amp;nbsp; CS_TOT&amp;nbsp; &amp;nbsp; LB_TOT&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How the values are same for all the three new variables?&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2022 16:11:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-and-Last/m-p/808343#M318743</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2022-04-18T16:11:05Z</dc:date>
    </item>
    <item>
      <title>Re: First. and Last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-and-Last/m-p/808344#M318744</link>
      <description>&lt;P&gt;Why would you expect them to be different?&amp;nbsp; They are set to zero exactly the same way at the same time.&amp;nbsp; They increment exactly the same way.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2022 16:17:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-and-Last/m-p/808344#M318744</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-18T16:17:32Z</dc:date>
    </item>
    <item>
      <title>Re: First. and Last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-and-Last/m-p/808346#M318746</link>
      <description>&lt;P&gt;What I found helpful in understanding First and Last with multiple BY variables was to create variables that hold the values and compare them to the data.&lt;/P&gt;
&lt;P&gt;Something like:&lt;/P&gt;
&lt;PRE&gt;DATA WORK.MFGTOT;
     SET WORK.MFG;
     BY YR WK PO;
     Firstyr = First.yr;
     Lastyr = Last.yr;
     Firstwk = First.wk;
     Lastwk = Last.wk;
     Firstpo = First.PO;
     Lastpo = Last.PO;
run;&lt;/PRE&gt;
&lt;P&gt;Values of 1 for True and 0 for False.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want a more interesting TOTAL that provide different numbers of records and/or additional variables to total, maybe named CS ZNL and LB and use ZNL_TOT = ZNL;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2022 16:28:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-and-Last/m-p/808346#M318746</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-04-18T16:28:12Z</dc:date>
    </item>
    <item>
      <title>Re: First. and Last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-and-Last/m-p/808347#M318747</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292396"&gt;@David_Billa&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;I just ran your code and then the code which I posted. Result I got for the new variables are,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;ZNL_TOT&amp;nbsp; &amp;nbsp; CS_TOT&amp;nbsp; &amp;nbsp; LB_TOT&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How the values are same for all the three new variables?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;None of the SUM statements is conditional, so all three sums MUST result in the same number.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2022 16:28:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-and-Last/m-p/808347#M318747</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-04-18T16:28:52Z</dc:date>
    </item>
    <item>
      <title>Re: First. and Last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-and-Last/m-p/808348#M318748</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292396"&gt;@David_Billa&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I'd like to know what would be the output if I execute the below program with this sample data.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This should never be a question asked here in the SAS Communities. Run the code, and you will know what the output is. You don't have to wait for some here to answer, SAS will tell you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;I just trying to understand the behavior of&amp;nbsp;First. and Last.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;if first.&lt;/FONT&gt; causes something to happen on the first record of each value of the BY variable specified; in your case your are using &lt;FONT face="courier new,courier"&gt;if first.PO&lt;/FONT&gt; so it causes something to happen for the first record of each value of PO&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;if last.&lt;/FONT&gt; causes something to happen on the ________ record of each value the BY variable specified (You fill in the blank)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So in your code, for the first record of each value of PO, which of the following happen? (choose one)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;ZNL_TOT, CS_TOT and LB_TOT are assigned a value of zero&lt;/LI&gt;
&lt;LI&gt;ZNL_TOT is assigned a value of zero, CS_TOT and LB_TOT are set to missing&lt;/LI&gt;
&lt;LI&gt;ERRORs are written to the log&lt;/LI&gt;
&lt;LI&gt;all of the above&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;For the last record of each value of PO, you have the following code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;if last.po;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This causes the record to be written to the data set MFGTOT.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;How the values are same for all the three new variables?&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The variables are all handled EXACTLY the same. So why would the values be different?&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2022 16:34:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-and-Last/m-p/808348#M318748</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-04-18T16:34:00Z</dc:date>
    </item>
    <item>
      <title>Re: First. and Last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-and-Last/m-p/808349#M318749</link>
      <description>How does it increment to till 3 ?&lt;BR /&gt;</description>
      <pubDate>Mon, 18 Apr 2022 16:33:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-and-Last/m-p/808349#M318749</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2022-04-18T16:33:19Z</dc:date>
    </item>
    <item>
      <title>Re: First. and Last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-and-Last/m-p/808351#M318750</link>
      <description>&lt;P&gt;It sets it to zero at the start of the group. Then adds one for each observation.&amp;nbsp; Then deletes all but the last observation.&amp;nbsp; So it gets to 3 by following this path:&amp;nbsp; -&amp;gt;0 -&amp;gt; 1-&amp;gt;2-&amp;gt;3.&amp;nbsp; You don't see the 1 and 2 because you deleted those observations with the subsetting IF statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Look up the subsetting IF statement.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.google.com/search?q=%40sas.com+subsetting+if+statement" target="_blank" rel="noopener"&gt;https://www.google.com/search?q=%40sas.com+subsetting+if+statement&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p1cxl8ifdt8u0gn12wqbji8o5fq1.htm" target="_blank" rel="noopener"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p1cxl8ifdt8u0gn12wqbji8o5fq1.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also look up the SUM statement in the documentation to understand why the values increase instead of getting reset to missing every observation.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.google.com/search?q=%40sas.com+sum+statement" target="_blank" rel="noopener"&gt;https://www.google.com/search?q=%40sas.com+sum+statement&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1dfiqj146yi2cn1maeju9wo7ijs.htm" target="_blank" rel="noopener"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1dfiqj146yi2cn1maeju9wo7ijs.htm&lt;/A&gt;&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2022 16:43:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-and-Last/m-p/808351#M318750</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-18T16:43:44Z</dc:date>
    </item>
    <item>
      <title>Re: First. and Last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-and-Last/m-p/808352#M318751</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292396"&gt;@David_Billa&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;How does it increment to till 3 ?&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Please do count the datalines containing&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;1 2022 1&lt;/PRE&gt;
&lt;P&gt;Which number do you come up with?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2022 16:42:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-and-Last/m-p/808352#M318751</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-04-18T16:42:46Z</dc:date>
    </item>
  </channel>
</rss>

