Sample 25324: Using More than One Observation in a Calculation, Chapter 11
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
/**************************************************************/
/* S A S S A M P L E L I B R A R Y */
/* */
/* NAME: BUG11U01 */
/* TITLE: More than One Obs. in a Calculation, Chapter 11 */
/* PRODUCT: BASE */
/* SYSTEM: ALL */
/* KEYS: DOC DATASTEP PRINT SUM SET END= DSOPTION DROP= */
/* KEEP= SORT SET FIRST. BY */
/* PROCS: PRINT SORT */
/* DATA: */
/* */
/* SUPPORT: UPDATE: */
/* REF: SAS Language and Procedures: Usage */
/* MISC: */
/* */
/**************************************************************/
/* For simplicity, the infile statement has been removed */
/* and the raw data is being read via the cards statement */
options ls=72;
title;
data tourrev;
input country $ 1-11 landcost vendor $ bookings;
cards;
France 575 Express 10
Spain 510 World 12
Brazil 540 World 6
India 489 Express .
Japan 720 Express 10
Greece 698 Express 20
New Zealand 1489 Southsea 6
Venezuela 425 World 8
Italy 468 Express 9
USSR 924 World 6
Switzerland 734 World 20
Australia 1079 Southsea 10
Ireland 558 Express 9
;
run;
proc print data=tourrev;
title 'SAS Data Set TOURREV';
run;
data total;
set tourrev;
totbook+bookings;
run;
proc print data=total;
var country bookings totbook;
title 'Total Tours Booked';
run;
data total2(keep=totbook);
set tourrev end=lastobs;
totbook+bookings;
if lastobs;
run;
proc print data=total2;
title 'Last Observation Shows Total Tours Booked';
run;
proc sort data=tourrev out=sorttour;
by vendor;
run;
data totalby;
set sorttour;
by vendor;
if first.vendor then vendorbk=0;
vendorbk+bookings;
run;
proc print data=totalby;
title 'Setting the Sum Variable to 0 for BY Groups';
run;
proc sort data=tourrev out=sorttour;
by vendor;
run;
data totalby(drop=country landcost bookings);
set sorttour;
by vendor;
if first.vendor then vendorbk=0;
vendorbk+bookings;
if last.vendor;
run;
proc print data=totalby;
title 'Last Observation in BY Group Contains Group Total';
run;
proc sort data=tourrev out=sorttour;
by vendor;
run;
data details(drop=grpbook grpmoney)
vendgrps(keep=vendor grpbook grpmoney);
set sorttour;
by vendor;
money=landcost*bookings;
output details;
if first.vendor then
do;
grpbook=0;
grpmoney=0;
end;
grpbook+bookings;
grpmoney+money;
if last.vendor then output vendgrps;
run;
proc print data=details;
title 'Detail Records: Dollars Spent on Individual Tours';
run;
proc print data=vendgrps;
title 'Group Totals: Dollars Spent and Bookings by Vendor';
run;
data temp;
set tourrev;
retain holdrev;
revenue=landcost*bookings;
output;
holdrev=revenue;
run;
proc print data=temp;
var country landcost bookings revenue holdrev;
title 'HOLDREV Shows REVENUE from Previous Observation';
run;
data mostrev;
set tourrev;
retain holdrev;
revenue=landcost*bookings;
if revenue>holdrev then holdrev=revenue;
run;
proc print data=mostrev;
var country landcost bookings revenue holdrev;
title 'Collecting the Largest Value of REVENUE in HOLDREV';
run;
data mostrev(keep=holdctry holdrev);
set tourrev end=lastone;
retain holdrev holdctry;
revenue=landcost*bookings;
if revenue>holdrev then
do;
holdrev=revenue;
holdctry=country;
end;
if lastone;
run;
proc print data=mostrev;
title 'Country with the Largest Value of REVENUE';
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
These samples are from the "SAS Language and Procedures: Usage" book (No. 56075), Chapter 11.
For output and additional information on the samples refer to this book.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step Data Management ==> Manipulation and Transformation ==> BY-group processing
|
| Date Modified: | 2005-12-08 11:34:43 |
| Date Created: | 2005-05-23 13:46:02 |
Operating System and Release Information
| SAS System | Base SAS | All | n/a | n/a |