<?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 FIRST. and LAST. DATA step variables example in Certified Specialist Prep Guide in SAS Certification</title>
    <link>https://communities.sas.com/t5/SAS-Certification/FIRST-and-LAST-DATA-step-variables-example-in-Certified/m-p/978698#M1885</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I am working through the 2019 SAS Certified Specialist Prep Guide and I am confused about an explanation of the FIRST. and LAST. variables on page 133. Here is the code in the book:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data work.budget(keep=dept payroll);
set work.temp ;
by dept ;
if wagecat='S' then Yearly=wagerate*12 ; 
else if wagecat='H' then Yearly=wagerate*2000 ;
if first.dept then Payroll=0 ;
payroll+yearly ;
if last.dept ;
run ; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The part that I'm confused by is that the explanation for the line&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;if last.dept ; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;is: "If this observation is the last in the variable, Dept, then end. If not, then read the next observation."&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't understand this explanation - it seems to imply that BY-group processing requires you to tell SAS when the last observation in a BY group occurs. My understanding is that this line tells SAS to output the line where last.Dept=1. However, the next figure (8.1) on page 134 shows all observations for the dataset and says, "When you print the new dataset, you can now list and sum the annual payroll by department."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc print data=work.budget noobs ;
sum payroll ;
format payroll dollar12.2 ;
run ; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I'm confused as to why the second snippet of code is necessary. Shouldn't&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;if last.dept ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;create the sum of the annual payroll by department? And does this line accomplish anything other than controlling the output?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 11 Nov 2025 21:43:12 GMT</pubDate>
    <dc:creator>hcstritz</dc:creator>
    <dc:date>2025-11-11T21:43:12Z</dc:date>
    <item>
      <title>FIRST. and LAST. DATA step variables example in Certified Specialist Prep Guide</title>
      <link>https://communities.sas.com/t5/SAS-Certification/FIRST-and-LAST-DATA-step-variables-example-in-Certified/m-p/978698#M1885</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I am working through the 2019 SAS Certified Specialist Prep Guide and I am confused about an explanation of the FIRST. and LAST. variables on page 133. Here is the code in the book:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data work.budget(keep=dept payroll);
set work.temp ;
by dept ;
if wagecat='S' then Yearly=wagerate*12 ; 
else if wagecat='H' then Yearly=wagerate*2000 ;
if first.dept then Payroll=0 ;
payroll+yearly ;
if last.dept ;
run ; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The part that I'm confused by is that the explanation for the line&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;if last.dept ; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;is: "If this observation is the last in the variable, Dept, then end. If not, then read the next observation."&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't understand this explanation - it seems to imply that BY-group processing requires you to tell SAS when the last observation in a BY group occurs. My understanding is that this line tells SAS to output the line where last.Dept=1. However, the next figure (8.1) on page 134 shows all observations for the dataset and says, "When you print the new dataset, you can now list and sum the annual payroll by department."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc print data=work.budget noobs ;
sum payroll ;
format payroll dollar12.2 ;
run ; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I'm confused as to why the second snippet of code is necessary. Shouldn't&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;if last.dept ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;create the sum of the annual payroll by department? And does this line accomplish anything other than controlling the output?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Nov 2025 21:43:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Certification/FIRST-and-LAST-DATA-step-variables-example-in-Certified/m-p/978698#M1885</guid>
      <dc:creator>hcstritz</dc:creator>
      <dc:date>2025-11-11T21:43:12Z</dc:date>
    </item>
    <item>
      <title>Re: FIRST. and LAST. DATA step variables example in Certified Specialist Prep Guide</title>
      <link>https://communities.sas.com/t5/SAS-Certification/FIRST-and-LAST-DATA-step-variables-example-in-Certified/m-p/978699#M1886</link>
      <description>&lt;P&gt;The&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if last.dept;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;...is just shorthand for&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if last.dept=1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;...as you suspected.&amp;nbsp; It's known as a "subsetting IF", I think, in either case.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;These are in turn shorthand for:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if last.dept=1 then output;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;...I agree that the first explanation is confusing - I've never seen that before.&amp;nbsp; My understanding is that - yes - all this is doing is creating the 0/1 variable.&amp;nbsp; It does not do any sort of summation (re: your 2nd question).&amp;nbsp; I never use any of those features of PROC PRINT, so not really sure about the syntax there.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if last.dept;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;...is really just evaluating whether this resolves to 0 / missing &lt;EM&gt;vs. some other numeric value&lt;/EM&gt;.&amp;nbsp; So if you had a value like $15.75 in a variable called "amount", then:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if amount;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;...would also selectively only output those lines where amount was not zero and not missing.&amp;nbsp;&amp;nbsp;&lt;BR /&gt;Also note that:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if sex="F";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;...is also just evaluating, essentially, to 0 or 1 (SAS does not have typical FALSE / TRUE Boolean values).&amp;nbsp; This is why:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;n_females + (sex="F");&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;...will create an accumulating variable called "n_females" which is just adding 0 or 1 to the value for every observation.&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;You could even just write:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;...to take it to the ridiculous extreme.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Nov 2025 22:38:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Certification/FIRST-and-LAST-DATA-step-variables-example-in-Certified/m-p/978699#M1886</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-11-11T22:38:45Z</dc:date>
    </item>
    <item>
      <title>Re: FIRST. and LAST. DATA step variables example in Certified Specialist Prep Guide</title>
      <link>https://communities.sas.com/t5/SAS-Certification/FIRST-and-LAST-DATA-step-variables-example-in-Certified/m-p/978700#M1887</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;If this observation is the last in the variable, Dept, then end. If not, then read the next observation.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;That is definitely poorly worded.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;First thing "last in the variable" makes no sense at all or if it has any sense it is NOT what the LAST. variable means.&amp;nbsp; The LAST.DEPT is TRUE when this OBSERVATION is the last one in the current group of observations that have this value of DEPT.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;And the second part might sort of be true but does not really explain why the SUBSETTING if statement is being used here.&amp;nbsp; &amp;nbsp;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; means that when the condition is TRUE that the current iteration continues.&amp;nbsp; And if it is FALSE the iteration ends immediately, skipping all of the rest of the data step.&amp;nbsp; Including, most importantly for that data step, the IMPLIED OUTPUT statement that is executed at the end of a data step iteration when the data step does not include any explicit OUTPUT statement.&amp;nbsp; &amp;nbsp;Like with a&amp;nbsp;&amp;nbsp;&lt;A href="https://monopoly.fandom.com/wiki/Go_to_Jail_(card)" target="_blank" rel="noopener"&gt;Go to Jail card in Monopoly&lt;/A&gt;&amp;nbsp;you go directly to jail without passing GO or collecting $200.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;So the end result is that only the last observation in each group actually gets written to the output dataset.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Nov 2025 02:17:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Certification/FIRST-and-LAST-DATA-step-variables-example-in-Certified/m-p/978700#M1887</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-11-12T02:17:25Z</dc:date>
    </item>
  </channel>
</rss>

