130 likes | 247 Views
Sorting, Printing, and Summarizing Your Data. Lecture 5. Review. Creating and Redefining Variables SAS Functions IF-THEN Statements Grouping Observations with IF-THEN/ELSE Subsetting Data Simplifying Programs with Arrays. Lecture Structure. Using SAS Procedures
E N D
Review • Creating and Redefining Variables • SAS Functions • IF-THEN Statements • Grouping Observations with IF-THEN/ELSE • Subsetting Data • Simplifying Programs with Arrays
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
Using SAS Procedures LABEL ReceiveDate = 'Date order was received' ShipDate = 'Date merchandise was shipped';
Printing Your Data with PROC PRINT PROC PRINT DATA = data-setNOOBS LABEL; • 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. The following are optional statements that sometimes come in handy:
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 DateReturnedMMDDYY10. CandyType $ Quantity; Profit = Quantity * 1.25; PROC SORT DATA = sales; BY Class; PROC PRINT DATA = sales; BY Class; SUM Profit; VAR Name DateReturnedCandyType 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
Changing the Appearance of Printed Values with Formats • FORMAT statement • FORMAT Profit Loss DOLLAR8.2SaleDateMMDDYY8.; • 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.2SaleDateMMDDYY8.;
Changing the Appearance of Printed Values with Formats DATA sales; INFILE 'D:\My Documents\My Class\TA\MyCode\05code and data\Candy.dat'; INPUT Name $ 1-11 Class @15 DateReturnedMMDDYY10. CandyType $ Quantity; Profit = Quantity * 1.25; PROC PRINT DATA = sales; VAR Name DateReturnedCandyType Profit; FORMAT DateReturnedDATE9. Profit DOLLAR6.2; TITLE 'Candy Sale Data Using Formats'; 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
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.
Summarizing Your Data Using PROC MEANS 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 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 $ SaleDateMMDDYY10. 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; VAR Petunia SnapDragon Marigold; TITLE 'Summary of Flower Sales by Month'; RUN; 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
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. ) 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; 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
Exercise Result DATA sales; INFILE 'D:\My Documents\My Class\TA\MyCode\05code and data\Flowers.dat'; INPUT CustomerID $ SaleDateMMDDYY10. 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;