DATA Step, Macro, Functions and more

Please provide the answer using first.id and last.id concept

Accepted Solution Solved
Reply
Contributor
Posts: 20
Accepted Solution

Please provide the answer using first.id and last.id concept

Question: Dataset schoolx is given as :

data schoolx;
input Name $ Class $ Marks;
cards;
AAA X 20
AAA Y 30
AAA Z 40
AAA X 50
BBB X 60
BBB Y 70
BBB X 20
CCC X 10
CCC Y 40
CCC Z 50
CCC Z 30
;
run;

Generate the following output from the above dataset:
AAA X 70
AAA Y 30
AAA Z 40
BBB X 80
BBB Y 70
CCC X 10
CCC Y 40
CCC Z 80


Accepted Solutions
Solution
‎11-14-2016 02:12 PM
Super User
Posts: 6,948

Re: Please provide the answer using first.id and last.id concept

Read the very good SAS documentation on by group processing, and you will find the answer yourself.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Posts: 6,948

Re: Please provide the answer using first.id and last.id concept

First do a proc sort by name and class.

Then, in a data step, use by name class; again.

use

if first.class then newmarks = 0;

to initialize at every group change, use

newmarks + marks;

to add the marks up (this syntax for summarization automatically makes newmarks a retained variable).

use

if last.class then output;

to write out the summarized observations

use

drop marks;

rename newmarks=marks;

to create the old structure.

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 20

Re: Please provide the answer using first.id and last.id concept

can u please help me to explain with the output what exactly happens with this portion of code.I meant how first.class and last.class is working here.?

 

 

if first.class then newmarks = 0;

to initialize at every group change, use

newmarks + marks;

to add the marks up (this syntax for summarization automatically makes newmarks a retained variable).

use

if last.class then output;

to write out the summarized observations

use

drop marks;

rename newmarks=marks;

to create the old structure.

Solution
‎11-14-2016 02:12 PM
Super User
Posts: 6,948

Re: Please provide the answer using first.id and last.id concept

Read the very good SAS documentation on by group processing, and you will find the answer yourself.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 20

Re: Please provide the answer using first.id and last.id concept

thanks
Frequent Contributor
Posts: 127

Re: Please provide the answer using first.id and last.id concept

Is there any specific reason for using first.id and last.id. You can use proc means instead.

 

proc sort data=schoolx;

by name class;

run;

PROC MEANS data=schoolx sum;

class name class;

var marks;

run;

Thanks,
Suryakiran
Super Contributor
Posts: 275

Re: Please provide the answer using first.id and last.id concept


proc sql;
select Name,Class,sum(Marks) as Marks from schoolx group by Name,Class;
quit;

PROC Star
Posts: 552

Re: Please provide the answer using first.id and last.id concept

data schoolx;
input Name $ Class $ Marks;
cards;
AAA X 20
AAA Y 30
AAA Z 40
AAA X 50
BBB X 60
BBB Y 70
BBB X 20
CCC X 10
CCC Y 40
CCC Z 50
CCC Z 30
;
run;

proc sort data = schoolx;
   by Name Class;
run;

data want;
   set schoolx;
   by Name Class;

   if first.class = 1 then do;
      newmarks = 0;
      newmarks = newmarks + marks;
   end;

   else newmarks = newmarks + marks;
   
   if last.class then output;
   retain newmarks;
run;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 220 views
  • 4 likes
  • 5 in conversation