****
M4 Workshop
Objectives:
To review key concepts about sas procedures(PROCs)
To review general options and statements associated with following SAS procedures
PROC PRINT, PROC SORT, PROC MEANS, and PROC FREQ
To understand the results of these procedures
;
*********************************************************************************;
*Q1 – this section explores the use of PROC PRINT and its options.
The first block of code creates a dataset on US presidents. ;
data work.printme;
input rank 1-2 president $ 4-26 inauguration mmddyy10. @37 left_office mmddyy10.;
datalines;
1 George Washington 3/30/1789 3-4-1797
2 John Adams 3/4/1797 3-4-1801
3 Thomas Jefferson 3/4/1801 3-4-1809
4 James Madison 3/4/1809 3-4-1817
5 James Monroe 3/4/1817 3-4-1825
6 John Quincy Adams 3/4/1825 3-4-1829
7 Andrew Jackson 3/4/1829 3-4-1837
8 Martin Van Buren 3/4/1837 3-4-1841
9 William Henry Harrison 3/4/1841 4-4-1841
10 John Tyler 4/4/1841 3-4-1845
;
run;
**Q1.1 (5pts each) Use one data step to do the following
a. Add a FORMAT statement to format the variable left_office to look like 04Mar1797.
b. Add a LABEL statement to give the variable president a label: President’s Name;
**Q1.2 (5pts each) Write a PROC PRINT step using the data created in Q1.1
a. use proper statements to print the presidents who left the office after 1820.
Print only their names and dates they left office.
b. use a FORMAT statement in your PROC PRINT to show the year when they left without
month and day.
c. use proper statements to add a title to the printed results to indicate what you
are printing.
d. use proper options to display the column header using the label you created in Q1.1
instead of the variable name.;
**Q1.3 (10pts) Write another PROC PRINT step to print the complete dataset you created in Q1.1.
Do not add any other statements. What is the format of left_office now? Is it the
same as displayed in Q1.2? why or why not?;
*****************************************************************************;
*Q2 This part is on PROC FREQ and its options.
**Q2.1 (5pts)Using proc IMPORT, read in the example data set m4_fish;
**Q2.2 (5pts)Use proc freq to create a frequency table of species;
**Q2.3 (5pts each)Create a cross-tabulation table of species and water type. Use proper codes to
ensure the following :
a. Show each species in one row, and each water type in one column.
b. Display only the counts and row percent.
c. Include missing values.;
**Q2.4 (a:5pts,b:10pts)Look at the results table you created in Q2.3, and answer the following questions:
a. which species live in freshwater? and which species live in saltwater?
b. How many observations are missing both Species and Water Type?
How many observations have missing values for Water Type regardless of Species?
How many observations don’t have missing values?
For the species “Roach”, what is the missing rate of Water Type in percentage?;
**Q2.5 (5pts)Try to use BY statement in your PROC FREQ. It’s similar to the use in PROC MEANS.
Display a series of tables, one for each Water Type. Each table is a cross-tabulation
table of Species and Weight Category. Display only the frequencies. ;
*****************************************************************************;
*Q3 Self-learning on PROC TABULATE.
There is no way we can go over all the SAS PROCs in a course, not even the popular ones.
Therefore, it’s important to know how to research on such topics. Google and use SAS online
documentations to study about PROC TABULATE. Let’s practice it using the same data in Q2;
**Q3.1 (10pts)Use PROC TABULATE to create a cross-tabulation table of species and Weight Category.
Any difference in the result comparing to a basic PROC FREQ?;
**Q3.2 (5pts)Use another PROC TABULATE to calculate the mean weight instead of the counts for the
tabulation of Q3.1.;