Hi all, 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: 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 ; The part that I'm confused by is that the explanation for the line if last.dept ; is: "If this observation is the last in the variable, Dept, then end. If not, then read the next observation." 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." proc print data=work.budget noobs ;
sum payroll ;
format payroll dollar12.2 ;
run ; I'm confused as to why the second snippet of code is necessary. Shouldn't if last.dept ; create the sum of the annual payroll by department? And does this line accomplish anything other than controlling the output?
... View more