4
/***********************************************/ /* Author: Nicholas Reeves */ /* Title: Assignment 13 */ /* created on: April 25, 2015 */ /* Purpose: To complete Assignment 13 */ /* Last successful run: April 30, 2015 */ /***********************************************/ ods _all_ close; filename hw_out 'C:\Users\nick\Documents\MS Stats\657\Assignments\HW13\nlreeves13_HW13_output'; libname dta 'C:\Users\nick\Documents\MS Stats\657\Assignments\data'; libname orion 'C:\Users\nick\Documents\MS Stats\657\SQL\SQL Files' access=readonly; options MLogic symbolgen mprint nonumber msglevel=I nodate; /* 1. Use mbb04 dataset as source to create a format*/ /* a. Convert commands from p307d01 to SQL to create*/ /* a similar dataset from mbb04. Use SQL instead of */ /* data step because need distinct values in data set*/ /* for format.*/ proc sql; /*lecture 21 page 57*/ create table seedy (start char(18), label char, fmtname char); /* add columns to a table*/ insert into seedy /*populate new columns*/ /* b. The start value of format will come from the Team column*/ (start,label,fmtname) /* c. Use seed_ as label in format. Labels are text but seed_ field*/ /* is numeric. Use put function to convert number to text. */ /* d. Give new format a name*/ select distinct team, put(seed_,2.), '$SEEDY' from dta.mbb04 ; /* e. After creating new dataset, use SQL to insert a row at end. */ /* Values of start='other', label='NA', and name of format.*/ insert into seedy set start='other',label='NA', fmtname='$SEEDY' ; quit; /* f. Use format procedure to create from this dataset a user defined format*/ /* in the work library.*/ proc format library=work.myfmts /* without library= it would still go to work.myfmts*/ cntlin=seedy; select $seedy; run; /* g. Use the format procedure to write the contents of the new format out*/ /* to your output document. This output only displays first 16 characters,*/ /* this is OK.*/ ods pdf file= hw_out bookmarklist=no; proc format library=work.myfmts /* without library= it would still go to work.myfmts*/ cntlin=seedy fmtlib; select $seedy; run; /* 2. Create a picture format that can be applied to RPG field. Allow space*/

nlreeves13_HW13_prog

Embed Size (px)

Citation preview

Page 1: nlreeves13_HW13_prog

/***********************************************/

/* Author: Nicholas Reeves */

/* Title: Assignment 13 */

/* created on: April 25, 2015 */

/* Purpose: To complete Assignment 13 */

/* Last successful run: April 30, 2015 */

/***********************************************/

ods _all_ close;

filename hw_out 'C:\Users\nick\Documents\MS Stats\657\Assignments\HW13\nlreeves13_HW13_output';

libname dta 'C:\Users\nick\Documents\MS Stats\657\Assignments\data';

libname orion 'C:\Users\nick\Documents\MS Stats\657\SQL\SQL Files' access=readonly;

options MLogic symbolgen mprint nonumber msglevel=I nodate;

/* 1. Use mbb04 dataset as source to create a format*/

/* a. Convert commands from p307d01 to SQL to create*/

/* a similar dataset from mbb04. Use SQL instead of */

/* data step because need distinct values in data set*/

/* for format.*/

proc sql; /*lecture 21 page 57*/

create table seedy

(start char(18), label char, fmtname char); /* add columns to a table*/

insert into seedy

/*populate new columns*/

/* b. The start value of format will come from the Team column*/

(start,label,fmtname)

/* c. Use seed_ as label in format. Labels are text but seed_ field*/

/* is numeric. Use put function to convert number to text. */

/* d. Give new format a name*/

select distinct team, put(seed_,2.), '$SEEDY'

from dta.mbb04

;

/* e. After creating new dataset, use SQL to insert a row at end. */

/* Values of start='other', label='NA', and name of format.*/

insert into seedy

set start='other',label='NA', fmtname='$SEEDY'

;

quit;

/* f. Use format procedure to create from this dataset a user defined format*/

/* in the work library.*/

proc format library=work.myfmts /* without library= it would still go to work.myfmts*/

cntlin=seedy;

select $seedy;

run;

/* g. Use the format procedure to write the contents of the new format out*/

/* to your output document. This output only displays first 16 characters,*/

/* this is OK.*/

ods pdf file= hw_out bookmarklist=no;

proc format library=work.myfmts /* without library= it would still go to work.myfmts*/

cntlin=seedy fmtlib;

select $seedy;

run;

/* 2. Create a picture format that can be applied to RPG field. Allow space*/

Page 2: nlreeves13_HW13_prog

/* for RPG to be displayed as two digits along with a single decimal (00.0).*/

/* a. Values of 6.1 or more will be in parenthesis preceded by the word High*/

proc format;

picture rpglev

6.1-high=' 09.9)'

(prefix='High(')

/* b. values of 3.5 or more but less than 6.1 preceded by Med*/

3.5-<6.1=' 09.9)'

(prefix='Med(')

/* c. Values under 3.5 are preceded by the word Low*/

low-<3.5=' 09.9)'

(prefix='Low(');

run;

/* 3. Use a SAS Procedure to place a copy of the mbb06 dataset in WORK library.*/

/* All future references to mbb06 will be to WORK copy.*/

proc sql;

create table mbb06 as

select*

from dta.mbb06;

;

/* 4. Use SQL statements to make the following modifications to mbb06 dataset*/

/* a. Create a character column labeled RPG Rating. Wide enough to accept*/

/* all values of picture format*/

options fmtsearch=(work work.myfmts);

proc sql;

alter table mbb06

add rpgrate char(14) label='RPG Rating'

/* b. Create character column with length of 2 labeled 2004 Seeding*/

add seed04 char(2) label='2004 Seeding'

/* c. Change the length and format of the School column so it is wide enough*/

/* to fully display Wisconsin Milwaukee.*/

modify school char(19) format=$19.;

/* d. Populate the RPG Rating field using the put function to create values*/

/* by applying the picture format to the RPG field.*/

update mbb06 /*use update to populate rpgrate*/

set rpgrate=put(rpg,rpglev.)

/* e. Correct the following school names:*/

/* 'Indania' to 'Indiana'

'Boston Coll' to 'Boston College'

'George mason' to 'George Mason'

'Oral Robt-16' to 'Oral Roberts'

'Wisc. Milwaukee' to 'Wisconsin Milwaukee'*/

, school= /*update will also modify existing columns*/

case school

when 'Indania' then 'Indiana'

when 'Boston Coll' then 'Boston College'

when 'George mason' then 'George Mason'

when 'Oral Robt-16' then 'Oral Roberts'

when 'Wisc. Milwaukee' then 'Wisconsin Milwaukee'

else school

end

/* f. populate the 2004 Seeding Field using put function to create values by*/

/* applying the format from step 1 to school field*/

, seed04=put(school,$seedy.)

Page 3: nlreeves13_HW13_prog

;

quit;

/* 5. Create composite index for mbb06 using the fields player, school,*/

/* and region in that order. Print the descriptor portion of the mbb06 */

/* data set after the index has been created.*/

proc sql;

create index srp

on mbb06 (player,school,region);

quit;

proc contents data=mbb06;

run;

/* 6. Create print procedure that will print the columns shown in the*/

/* sample output from the mbb06 dataset. You need the label option in */

/* order for labels to appear in output. Make 5 copies of the print*/

/* procedure in program. Each copy will be modified with different*/

/* titles, options, and where clauses to demonstrate use of indexes.*/

/* Two of these procedures will generate errors in the log instead*/

/* of producing output. EXPLAIN what happened. Create comment block*/

/* above each section. Write answers to each question, or explanation.*/

/* a. */

/***********************************************************************/

/*Is an index used on this step? Why or why not? Did the most efficient*/

/*action occur? Why or why not? */

/* YES, BECAUSE IDXWHERE=YES WAS USED AND EVEN IF THAT OPTION WAS NOT */

/* USED IT STILL WOULD HAVE BECAUSE SUBSET FROM WHERE REFERS TO THE */

/* INDEX AND SUBSET IS VERY SMALL. YES IT WAS MOST EFFICIENT FOR THE */

/* SAME REASON. */

/***********************************************************************/

proc print data=mbb06 (idxwhere=yes) label;

where player in ('Rudy Gay', 'Al Horford', 'Frank Young');

var player school region seed rpgrate seed04;

title '6a. IDXWHERE on Player';

run;

/* b. */

/************************************************************************/

/* Explain the results of this step. */

/* NO INDEX EXISTS THAT WILL OPTIMIZE THIS WHERE CLAUSE, PROCEDURE DID */

/* NOT SUCCESSFULLY PRINT AND ERROR WAS PRINTED TO LOG. EVEN THOUGH */

/* IDXWHERE=YES IS USED, SINCE THE INDEX IS COMPOSITE AND USING SCHOOL */

/* IN WHERE CLAUSE THE INDEX WOULD NOT WORK. */

/************************************************************************/

proc print data=mbb06 (idxwhere=yes) label;

where school='Connecticut';

var player school region seed rpgrate seed04;

title '6b. IDXWHERE on School';

run;

/* c. */

/************************************************************************/

/* Explain the results of this step. */

/* PRINT PROCEDURE WAS NOT SUCCESSFUL AND DID NOT PRINT BECAUSE THERE */

/* IS NOT AN INDEX TITLED SCHOOL */

Page 4: nlreeves13_HW13_prog

/************************************************************************/

proc print data=mbb06 (idxname=school) label;

where school='Connecticut';

var player school region seed rpgrate seed04;

title '6c. IDXNAME on School';

run;

/* d.*/

/************************************************************************/

/* Explain the usage of an index on this step. */

/* PRINT SUCCESS, BUT AN INDEX WAS NOT USED BECAUSE WHERE CLAUSE USED */

/* A 'OR' FOR A COMPOSITE INDEX */

/************************************************************************/

proc print data=mbb06 (idxwhere=yes) label;

where player in ('Rudy Gay', 'Al Horford', 'Frank Young') or

school='Texas';

var player school region seed rpgrate seed04;

title '6d. IDXWHERE on Player or School';

run;

/* e. */

/***********************************************************************/

/* Explain the difference in index usage between this step and the */

/* previous step. */

/* PRINT SUCCESS, AND SAS USED AN INDEX FOR THIS PROCEDURE BECAUSE */

/* WHERE CLAUSE OPERATORS THAT WORK WITH USING AN COMPOSITE INDEX WERE */

/* USED, MORE SPECIFICALLY 'AND' OPERATOR INSTEAD OF THE 'OR' OPERATOR */

/* AS PART D. USED. */

/***********************************************************************/

proc print data=mbb06 (idxwhere=yes) label;

where substr(player,1,1)='S' and

school in ('Duke', 'Memphis', 'Iona', 'Kansas', 'Gonzaga');

var player school region seed rpgrate seed04;

title '6e. IDXWHERE on Player and School';

run;

ods _all_ close;

title;

footnote;