DM 'LOG;CLEAR;OUT;CLEAR;';
/* CLEARING LOG & OUTPUT WINDOWS */
%PUT &SYSDATE &SYSTIME; * PUTS DATE AND TIME;
PROC DATASETS LIBRARY=WORK KILL; RUN;
OPTIONS NOCENTER FORMDLIM='-' PAGENO=MIN;
/*****************************************************************/
/*PROJECT: SAS Short Course
*/
/*
DATE: SEPT 12 2011
*/
/* NOTES: Codes used for the short course
*/
/*****************************************************************/
LIBNAME SAVE 'H:\2011FALL';
* DATA FROM AN EXCEL FILE ;
PROC IMPORT OUT= SAVE.CLASSDAT
DATAFILE= "H:\2011FALL\RA_SAS_Short_Course_INTRO_XLS.xls"
DBMS=EXCEL REPLACE;
RANGE="Sheet1$";
GETNAMES=YES;
MIXED=NO;
SCANTEXT=YES;
USEDATE=YES;
SCANTIME=YES;
RUN;
*****************************************************;
* DATA FROM A TEXT FILE ;
DATA WORK.CLASSDATTXT; INFILE 'H:\2011FALL\RA_SAS_Short_Course_INTRO_TXT.TXT';
INPUT ID $ 1-3
NAME $
5-9 SEX $ 11 EXAM1 13-14 GRADE $16;
RUN;
*****************************************************;
PROC CONTENTS DATA=save.CLASSDAT VARNUM; RUN;
***************
PROC PRINT
*************************;
PROC PRINT DATA=SAVE.CLASSDAT;
TITLE 'SAS SHORT COURSE';
RUN;
PROC PRINT DATA=SAVE.CLASSDAT (obs =2);
TITLE 'SAS SHORT COURSE';
RUN;
PROC PRINT DATA=SAVE.CLASSDAT (obs =2);
TITLE 'SAS SHORT COURSE';
VAR NAME GRADE;
RUN;
*********** PROC FREQ **************************;
PROC FREQ DATA = SAVE.CLASSDAT;
RUN;
PROC FREQ DATA = SAVE.CLASSDAT;
TABLES EXAM1;
RUN;
* FREQ DISTRIBUTION FOR A SPECIFIC VARIABLE TABLES;
PROC FREQ DATA = SAVE.CLASSDAT;
TABLES EXAM1*GRADE;
RUN;
PROC FREQ DATA = SAVE.CLASSDAT;
TABLES EXAM1*GRADE/ NOROW NOCOL NOPERCENT;
RUN;
ODS RTF FILE = 'H:\2011FALL\OUTFREQ.RTF' STARTPAGE=NO STYLE=OCEAN;
PROC FREQ DATA = SAVE.CLASSDAT;
TABLES EXAM1*GRADE ;
RUN;
ODS RTF CLOSE; * MAKE SURE ODS IS AFTER RUN STATEMENT;
RUN;
*********************
PROC MEANS *********************;
PROC MEANS DATA=SAVE.CLASSDAT;
RUN;
PROC MEANS DATA=SAVE.CLASSDAT;
VAR EXAM1;
RUN;
* DESCRIPTIVE STATISTICS FOR SUBGROUPS BASED ON A CATEGORICAL VARIABLE;
PROC SORT DATA=SAVE.CLASSDAT;
BY SEX;
RUN;
PROC MEANS DATA=SAVE.CLASSDAT;
VAR EXAM1 ;
BY SEX;
RUN;
ODS RTF CLOSE; * MAKE SURE ODS IS AFTER RUN STATEMENT;
RUN;
PROC MEANS DATA=SAVE.CLASSDAT;
VAR EXAM1 ;
CLASS SEX;
RUN;
PROC MEANS DATA=SAVE.CLASSDAT MAXDEC=2 N MEAN STDDEV VAR MEDIAN NONOBS;
VAR EXAM1 ;
CLASS SEX;
RUN;
* TO SAVE THE OUTPUT TO A DATASET;
PROC MEANS DATA=SAVE.CLASSDAT;
VAR EXAM1;
OUTPUT OUT=STATS; * THIS CAN BE USED IN OTHER PROC STATEMENTS ALSO;
RUN;
PROC PRINT DATA=STATS;
RUN;
* OFTEN THE OUTPUT DATASET WILL HAVE VARIABLES YOU DON'T WANT. TO GET
RID OF THESE USE THE DROP STATEMENT;
DATA STATS; SET STATS;
DROP _TYPE_ _FREQ_;
RUN;
PROC PRINT DATA=STATS;
RUN;
*********************** PROC UNIVARIATE ***********************;
************THIS GIVES A MORE EXTENSIVE OUTPUT *****************;
PROC UNIVARIATE DATA=SAVE.CLASSDAT;
VAR EXAM1;
RUN;
***********
DATA MANIPULATION
*****************************;
DATA MALES; SET SAVE.CLASSDAT;
IF SEX = 'F' OR SEX = ' ' THEN DELETE;
RUN;
PROC PRINT DATA=MALES;
RUN;
DATA MALES; SET SAVE.CLASSDAT;
IF SEX = 'F' AND GRADE = 'A' THEN DELETE;
RUN;
PROC PRINT DATA=MALES;
RUN;
DATA A B ; SET SAVE.CLASSDAT;
IF SEX ='M' THEN OUTPUT A;
IF SEX = 'F' THEN OUTPUT B;
RUN;
PROC PRINT DATA=A;
PROC PRINT DATA=B;
RUN;
* CREATE NEW VARIABLE;
DATA NEW; SET SAVE.CLASSDAT;
BONUS = EXAM1 + 5; **************NEW VAR= OLD VARIABLE;
RUN;
PROC PRINT DATA=NEW;
RUN;
*RENAME A VARIABLE
old variable = new variable;
DATA NEW; SET NEW;
RENAME BONUS = NEW_SCORE;
RUN;
PROC PRINT DATA=NEW;
RUN;
DATA NEW ; SET NEW;
IF NEW_SCORE GE 90 THEN GRADE='A';
ELSE GRADE = 'F';
RUN;
PROC PRINT DATA=NEW;
RUN;
DATA NEW ; SET NEW;
IF SEX = 'F' AND GRADE ='A' THEN STATUS = 'X';
ELSE IF SEX = 'M' AND GRADE ='A' THEN STATUS = 'Y';
ELSE IF SEX = 'F' AND GRADE ='F' THEN STATUS = 'Z';
RUN;
**********
MISSING VALUES
********************;
PROC MEANS DATA=SAVE.CLASSDAT N NMISS;
VAR _NUMERIC_;
RUN;
******** NUMBER OF MISSING VALUES IN EACH OBSERVATION
DATA MISS; SET SAVE.CLASSDAT;
MISS_N = CMISS (OF ID--GRADE);
RUN;
*********;
PROC PRINT DATA=MISS;
RUN;
************** GENERAL STATEMENTS ***************;
/*
%PUT &SYSDATE &SYSTIME;
* PUTS DATE AND TIME;*/
/*
PROC DATASETS LIBRARY=WORK KILL; RUN;
*/
/*
OPTIONS NOCENTER FORMDLIM='-' PAGENO=MIN;
*/
/********************************************************/