/* Jump to: Table of Contents Chapter 3 */
/*
*/
/* Jump to: Next Section */
DATA preteen; SET sashelp.class; WHERE age<13; LABEL name = 'First Name'; RENAME name = FName; FORMAT height weight 5.1; RUN;
/* Jump to: Top of Section Top of Chapter */
/* Jump to: Next Section */
PROC SQL;
SELECT *
FROM preteen
;
QUIT;
PROC PRINT DATA=preteen;
RUN;
PROC PRINT NOOBS LABEL DATA=preteen;
RUN;
/* Jump to: Top of Section Top of Chapter */
/* Jump to: Next Section */
PROC PRINT NOOBS LABEL DATA=preteen;
VAR fname age;
RUN;
PROC SQL;
SELECT fname, age
FROM preteen
;
QUIT;
/* Jump to: Top of Section Top of Chapter */
/* Jump to: Next Section */
DATA new;
SET preteen;
RUN;
PROC SQL;
CREATE TABLE new AS
SELECT *
FROM preteen
;
QUIT;
/* Jump to: Top of Section Top of Chapter */
/* Jump to: Next Section */
DATA subset;
SET preteen;
KEEP fname sex age;
RUN;
PROC SQL;
CREATE TABLE subset AS
SELECT fname, sex, age
FROM preteen
;
QUIT;
DATA subset;
SET preteen;
DROP height weight;
RUN;
PROC SQL;
CREATE TABLE subset(DROP=height weight) AS
SELECT *
FROM preteen
;
QUIT;
/* Jump to: Top of Section Top of Chapter */
/* Jump to: Next Section */
DATA ratios;
SET preteen;
ATTRIB Ratio FORMAT=5.2 LABEL='Weight:Height Ratio';
ratio = weight / height;
RUN;
PROC SQL;
CREATE TABLE ratios AS
SELECT *,
weight / height AS Ratio
FORMAT=5.2 LABEL='Weight:Height Ratio'
FROM preteen
;
QUIT;
/* Jump to: Top of Section Top of Chapter */
/* Jump to: Next Section */
PROC SUMMARY DATA=preteen;
VAR age height weight;
OUTPUT OUT=overall_averages(DROP = _type_ _freq_)
MIN (age )=Youngest
MAX (age )=Oldest
MEAN(height)=Avg_Height
MEAN(weight)=Avg_Weight;
RUN;
PROC SQL;
CREATE TABLE overall_averages AS
SELECT MIN (age) AS Youngest,
MAX (age) AS Oldest,
MEAN(height) AS Avg_Height FORMAT=5.1,
MEAN(weight) AS Avg_Weight FORMAT=5.1
FROM preteen
;
QUIT;
PROC SUMMARY DATA=preteen NWAY;
CLASS sex;
VAR age height weight;
OUTPUT OUT=group_averages(DROP = _type_ _freq_)
MIN (age )=Youngest
MAX (age )=Oldest
MEAN(height)=Avg_Height
MEAN(weight)=Avg_Weight;
RUN;
PROC SQL;
CREATE TABLE group_averages AS
SELECT sex,
MIN (age) AS Youngest,
MAX (age) AS Oldest,
MEAN(height) AS Avg_Height FORMAT=5.1,
MEAN(weight) AS Avg_Weight FORMAT=5.1
FROM preteen
GROUP BY sex
;
QUIT;
DATA threex3;
INPUT a b c;
CARDS;
1.1 2.0 3.0
6.0 5.0 4.4
7.7 8.0 9.0
;
PROC SQL;
SELECT MEAN(a,b,c) LABEL='Mean of 3'
FROM threex3
;
QUIT;
PROC SQL;
SELECT MEDIAN(a,b,c) LABEL='Median of 3'
FROM threex3
;
QUIT;
PROC SQL;
SELECT MEAN(a) LABEL='Mean of 1'
FROM threex3
;
QUIT;
PROC SQL;
SELECT MEDIAN(a) LABEL='Median of 1'
FROM threex3
;
QUIT;
/* Jump to: Top of Section Top of Chapter */
/* Jump to: Next Section */
DATA trip_list;
SET preteen;
IF age=11 THEN Trip = 'Zoo ';
ELSE IF sex='F' THEN trip = 'Museum';
ELSE trip = '[None]';
KEEP fname age sex trip;
RUN;
PROC SQL;
CREATE TABLE trip_list AS
SELECT fname,
age,
sex,
CASE WHEN age=11 THEN 'Zoo'
WHEN sex='F' THEN 'Museum'
ELSE '[None]'
END
AS Trip
FROM preteen
;
QUIT;
DATA trip_list;
SET preteen;
SELECT;
WHEN (age=11) Trip = 'Zoo ';
WHEN (sex='F') trip = 'Museum';
OTHERWISE trip = '[None]';
END;
KEEP fname age sex trip;
RUN;
/* Jump to: Top of Section Top of Chapter */
/* Jump to: Next Section */
DATA girls;
SET preteen;
WHERE sex='F';
RUN;
PROC SQL;
CREATE TABLE girls AS
SELECT *
FROM preteen
WHERE sex='F'
;
QUIT;
PROC SQL;
SELECT *
FROM preteen
WHERE age=10
;
QUIT;
PROC SQL;
CREATE TABLE tens AS
SELECT *
FROM preteen
WHERE age=10
;
QUIT;
PROC SUMMARY DATA=preteen NWAY;
CLASS sex age;
OUTPUT MAX(height)=Tallest MIN(height)=Shortest
OUT= hilo(DROP = _type_ _freq_);
RUN;
PROC SQL;
CREATE TABLE hilo AS
SELECT sex,
age,
MAX(height) AS Tallest,
MIN(height) AS Shortest
FROM preteen
GROUP BY sex, age
;
QUIT;
PROC SUMMARY DATA=preteen NWAY;
CLASS sex age;
OUTPUT MAX(height)=Tallest MIN(height)=Shortest
OUT=hilo(WHERE = (tallest - shortest > 4)
DROP = _type_ _freq_ );
RUN;
PROC SQL;
CREATE TABLE hilo AS
SELECT sex,
age,
MAX(height) AS Tallest,
MIN(height) AS Shortest
FROM preteen
GROUP BY sex, age
HAVING tallest - shortest > 4
;
QUIT;
/* Jump to: Top of Section Top of Chapter */
/* Jump to: Next Section */
PROC SORT DATA=preteen OUT=age_sort;
BY DESCENDING age fname;
RUN;
PROC SQL;
CREATE TABLE age_sort AS
SELECT *
FROM preteen
ORDER BY age DESCENDING, fname
;
QUIT;
/* Jump to: Top of Section Top of Chapter */
/* Jump to: Next Section */
PROC SQL;
CREATE TABLE sex_age AS
SELECT sex, age
FROM preteen
;
QUIT;
PROC SORT DATA=sex_age OUT=sex_age_distinct NODUPRECS;
BY _ALL_;
RUN;
PROC SQL;
CREATE TABLE sex_age_distinct AS
SELECT DISTINCT *
FROM sex_age
;
QUIT;
/* Jump to: Top of Section Top of Chapter */