Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 5 Sorting, Printing, and Summarizing Your Data.

Similar presentations


Presentation on theme: "Lecture 5 Sorting, Printing, and Summarizing Your Data."— Presentation transcript:

1 Lecture 5 Sorting, Printing, and Summarizing Your Data

2 Review Creating and Redefining Variables SAS Functions IF-THEN Statements Grouping Observations with IF-THEN/ELSE Subsetting Data Simplifying Programs with Arrays

3 Lecture Structure Using SAS Procedures Printing Your Data with PROC PRINT Changing the Appearance of Printed Values with Formats Summarizing Your Data Using PROC MEANS

4 Using SAS Procedures LABEL ReceiveDate = 'Date order was received' ShipDate = 'Date merchandise was shipped';

5 Printing Your Data with PROC PRINT Use the NOOBS option in the PROC PRINT statement. If you don’t want observation numbers Print the labels instead of the variable names, then add the LABEL option as well. PROC PRINT DATA = data-set NOOBS LABEL; The following are optional statements that sometimes come in handy:

6 Printing Your Data with PROC PRINT DATA sales; INFILE 'D:\My Documents\My Class\TA\MyCode\05code and data\Candy.dat'; INPUT Name $ 1-11 Class @15 DateReturned MMDDYY10. CandyType $ Quantity; Profit = Quantity * 1.25; PROC SORT DATA = sales; BY Class; PROC PRINT DATA = sales; BY Class; BY Class; SUM Profit; SUM Profit; VAR Name DateReturned CandyType Profit; VAR Name DateReturned CandyType Profit; TITLE 'Candy Sales for Field Trip by Class'; RUN; Adriana 21 3/21/2008 MP 7 Nathan 14 3/21/2008 CD 19 Matthew 14 3/21/2008 CD 14 Claire 14 3/22/2008 CD 11 Caitlin 21 3/24/2008 CD 9 Ian 21 3/24/2008 MP 18 Chris 14 3/25/2008 CD 6 Anthony 21 3/25/2008 MP 13 Stephen 14 3/25/2008 CD 10 Erika 21 3/25/2008 MP 17

7 Changing the Appearance of Printed Values with Formats CharacterNumericDate $formatw.formatw.dformatw. FORMAT statement FORMAT Profit Loss DOLLAR8.2 SaleDate MMDDYY8.; FORMAT statements can go in either DATA steps or PROC steps. If the FORMAT statement is in a DATA step, then the format association is permanent and is stored with the SAS data set. If the FORMAT statement is in a PROC step, then it is temporary— affecting only the results from that procedure. PUT statement PUT Profit DOLLAR8.2 Loss DOLLAR8.2 SaleDate MMDDYY8.;

8 Changing the Appearance of Printed Values with Formats Adriana 21 3/21/2008 MP 7 Nathan 14 3/21/2008 CD 19 Matthew 14 3/21/2008 CD 14 Claire 14 3/22/2008 CD 11 Caitlin 21 3/24/2008 CD 9 Ian 21 3/24/2008 MP 18 Chris 14 3/25/2008 CD 6 Anthony 21 3/25/2008 MP 13 Stephen 14 3/25/2008 CD 10 Erika 21 3/25/2008 MP 17 DATA sales; INFILE 'D:\My Documents\My Class\TA\MyCode\05code and data\Candy.dat'; INPUT Name $ 1-11 Class @15 DateReturned MMDDYY10. CandyType $ Quantity; Profit = Quantity * 1.25; PROC PRINT DATA = sales; VAR Name DateReturned CandyType Profit; FORMAT DateReturned DATE9. Profit DOLLAR6.2; FORMAT DateReturned DATE9. Profit DOLLAR6.2; TITLE 'Candy Sale Data Using Formats'; RUN;

9 Summarizing Your Data Using PROC MEANS PROC MEANS options; If you do not specify any options, MEANS will print the number of non-missing values, the mean, the standard deviation, and the minimum and maximum values for each variable.

10 If you use the PROC MEANS statement with no other statements, then you will get statistics for all observations and all numeric variables in your data set. Here are some of the optional statements you may want to use: Summarizing Your Data Using PROC MEANS

11 756-01 05/04/2008 120 80 110 834-01 05/12/2008 90 160 60 901-02 05/18/2008 50 100 75 834-01 06/01/2008 80 60 100 756-01 06/11/2008 100 160 75 901-02 06/19/2008 60 60 60 756-01 06/25/2008 85 110 100 A wholesale nursery is selling garden flowers, and they want to summarize their sales figures by month. The data file which follows contains the customer ID, date of sale, and number of petunias, snapdragons, and marigolds sold: DATA sales; INFILE 'D:\My Documents\My Class\TA\MyCode\05code and data\Flowers.dat'; INPUT CustomerID $ SaleDate MMDDYY10. Petunia SnapDragon Marigold; Month = MONTH(SaleDate); PROC SORT DATA = sales; BY Month; * Calculate means by Month for flower sales; PROC MEANS DATA = sales; BY Month; BY Month; VAR Petunia SnapDragon Marigold; VAR Petunia SnapDragon Marigold; TITLE 'Summary of Flower Sales by Month'; TITLE 'Summary of Flower Sales by Month'; RUN;

12 Exercise Download the dataset “Flowers.dat” from the folder “ 05 code and data” in our blackboard. Summarizing this dataset Using PROC MEANS by CustomerID. (This result does not need to submit. ) 756-01 05/04/2008 120 80 110 834-01 05/12/2008 90 160 60 901-02 05/18/2008 50 100 75 834-01 06/01/2008 80 60 100 756-01 06/11/2008 100 160 75 901-02 06/19/2008 60 60 60 756-01 06/25/2008 85 110 100 The data file which follows contains the customer ID, date of sale, and number of petunias, snapdragons, and marigolds sold: /* This is the Sample Code with red filled part*/ DATA dataname; INFILE ‘Locate your dataset here'; INPUT identify your data with right format; PROC function_name DATA = dataname; BY variable_name; VAR othervariable you want to show in your output;

13 Exercise Result DATA sales; INFILE 'D:\My Documents\My Class\TA\MyCode\05code and data\Flowers.dat'; INPUT CustomerID $ SaleDate MMDDYY10. Petunia SnapDragon Marigold; PROC SORT DATA = sales; BY CustomerID; * Calculate means by CustomerID, output sum and mean to new data set; PROC MEANS DATA = sales; BY CustomerID; VAR Petunia SnapDragon Marigold;


Download ppt "Lecture 5 Sorting, Printing, and Summarizing Your Data."

Similar presentations


Ads by Google