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

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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