Hello team,
Why a grouping is not happening if I don't bring first and last in grouping? What is the role of first and last?
Proc sort data=cert.usa out=work.temp; by dept; run; data work.budget (Keep = dept payroll); set work.temp; by dept; if wagecate = '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;
What do you mean by "grouping is not happening" ?
@GN0001 wrote:
Hello team,
Why a grouping is not happening if I don't bring first and last in grouping? What is the role of first and last?
First and last create the grouping. If you don't have it, then no grouping is created because there is no code to do that grouping.
FIRST.DEPT sets the sum to zero each time DEPT changes. LAST.DEPT is used to control which records are output, only the last record for each value of DEPT is output.
I always recommend that you don't do summing (or computing most other statistics) by group in a DATA step, because SAS has already written that code for you in PROC MEANS/PROC SUMMARY. Really, don't do this in a DATA step.
proc summary data=temp nway;
class dept;
var payroll;
output out=sums sum=;
run;
Maxim 1: Read the Documentation.
See How SAS Identifies the Beginning and End of a BY Group and BY-Group Processing in the DATA Step
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.