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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.