BookmarkSubscribeRSS Feed
GN0001
Barite | Level 11

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;
Blue Blue
3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

What do you mean by "grouping is not happening" ?

PaigeMiller
Diamond | Level 26

@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;

  

--
Paige Miller

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 956 views
  • 1 like
  • 4 in conversation