SAS_Sort_accum_total

# SAS_Sort_accum_total - Sort And Sort Accumulating...

This preview shows pages 1–15. Sign up to view the full content.

Sort And Sort And Accumulating Accumulating Totals Totals Last Updated : 29June, 2004 Center of Excellence Data Warehousing

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Objectives Understand how the SAS System initializes the value of a variable in the PDV. Prevent reinitialization of a variable in the PDV. Create an accumulating variable.
Creating an Accumulating Variable The SAS data set prog2.daysales contains daily sales data for a retail store. There is one observation for each day in April showing the date ( SaleDate ) and the total receipts for that day ( SaleAmt ). Sal eDat e Sal eAmt 01APR2001 498. 49 02APR2001 946. 50 03APR2001 994. 97 04APR2001 564. 59 05APR2001 783. 01 06APR2001 228. 82 07APR2001 930. 57 08APR2001 211. 47 09APR2001 156. 23 10APR2001 117. 69 11APR2001 374. 73 12APR2001 252. 73

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Creating an Accumulating Variable The store manager also wants to see a running total of sales for the month as of each day. Partial Output Sal e Sal eDat e Amt Mt h2Dt e 01APR2001 498. 49 498. 49 02APR2001 946. 50 1444. 99 03APR2001 994. 97 2439. 96 04APR2001 564. 59 3004. 55 05APR2001 783. 01 3787. 56
Creating Mth2Dte By default, variables created with an assignment statement are initialized to missing at the top of the DATA step. An accumulating variable must retain its value from one observation to the next. Mth2Dte=Mth2Dte+SaleAmt;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
The RETAIN Statement General form of the RETAIN statement: The RETAIN statement prevents SAS from re- initializing the values of new variables at the top of the DATA step. Previous values of retained variables are available for processing across iterations of the DATA step. RETAIN variable-name < initial-value > … ;
The RETAIN Statement The RETAIN statement retains the value of the variable in the PDV across iterations of the DATA step initializes the retained variable to missing before the first execution of the DATA step if an initial value is not specified is a compile-time-only statement.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Retain Mth2Dte and Set an Initial Value If you do not supply an initial value, all the values of Mth2Dte will be missing. retain Mth2Dte 0;
data mnthtot; set prog2.daysales; retain Mth2Dte 0; Mth2Dte=Mth2Dte+SaleAmt; run; Creating an Accumulating Variable

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
... ... Sal eDat e Sal eAmt 15066 498. 49 15067 946. 50 15068 994. 97 15069 564. 59 15070 783. 01 SALEAMT data mnthtot; set prog2.daysales; retain Mth2Dte 0; Mth2Dte=Mth2Dte+SaleAmt; run; MTH2DTE SALEDATE R Compile
... ... Sal eDat e Sal eAmt 15066 498. 49 15067 946. 50 15068 994. 97 15069 564. 59 15070 783. 01 SALEAMT . data mnthtot; set prog2.daysales; retain Mth2Dte 0; Mth2Dte=Mth2Dte+SaleAmt; run; MNTH2DTE SALEDATE . R Execute 0

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
... ... Sal eDat e Sal eAmt 15066 498. 49 15067 946. 50 15068 994. 97 15069 564. 59 15070 783. 01 SALEAMT . data mnthtot; set prog2.daysales; retain Mth2Dte 0; Mth2Dte=Mth2Dte+SaleAmt; run; MNTH2DTE SALEDATE . R 498.49 0 15066
... ... Sal eDat e Sal eAmt 15066 498. 49 15067 946. 50 15068 994. 97 15069 564. 59 15070 783. 01 SALEAMT . data mnthtot; set prog2.daysales; retain Mth2Dte 0; Mth2Dte=Mth2Dte+SaleAmt; run; MNTH2DTE SALEDATE . R 15066 498.49 0 498.49 0+498.49

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
... ... Write out observation to  mnthtot .
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern