BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ROHINISDAS
Obsidian | Level 7

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

1 ACCEPTED SOLUTION
7 REPLIES 7
Kurt_Bremser
Super User

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.

 

ROHINISDAS
Obsidian | Level 7

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.

SuryaKiran
Meteorite | Level 14

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
slchen
Lapis Lazuli | Level 10


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

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 7 replies
  • 1170 views
  • 4 likes
  • 5 in conversation