*---+----1----+----2----+----3----+----4----+----5----+----6----+---;

*** c02s2d1.sas ***;

dm "output;clear;log;clear";

data work.staff;

   infile 'C:\_SASCourse2006\Course1\Sas_Data\emplist.dat';

   input LastName $ 1-20 FirstName $ 21-30

         JobTitle $ 36-43 Salary 54-59;

run;

 

proc print data=work.staff;

run;

 

proc means data=work.staff;

   class Jobtitle;

   var Salary;

run;

 

proc contents data=work.staff;

run;

 

*---+----1----+----2----+----3----+----4----+----5----+----6----+---;

*** c02s2d3.sas ***;

dm "output;clear;log;clear";

 

/* Create work.staff data set */

data work.staff;

   infile 'C:\_SASCourse2006\Course1\Sas_Data\emplist.dat';

   input LastName $ 1-20 FirstName $ 21-30

         JobTitle $ 36-43 Salary 54-59;

run;

 

 /* Produce listing report of work.staff */

proc print data=work.staff;

run;

 

* An alternative form of comment statement can be

  created by starting a SAS statement with an

  asterisk, entering the comment, and ending the

  comment with a semicolon;

 

*************************************************;

*** I often start my programs with a box      ***;

*** that describes the program and references ***;

*** sources where appropriate.                ***;

*************************************************;

 

* Everything between a leading asterisk and a semicolon is a comment;

 


*---+----1----+----2----+----3----+----4----+----5----+----6----+---;

*** c02s4d1.sas ***;

dm "output;clear;log;clear";

daat work.staff;

   infile 'C:\_SASCourse2006\Course1\Sas_Data\emplist.dat';

   input LastName $ 1-20 FirstName $ 21-30

         JobTitle $ 36-43 Salary 54-59;

run;

 

proc print data=work.staff

run;

 

proc means data=work.staff average max;

   class JobTitle;

   var Salary;

run;

 

*** c02s4d2.sas ***;

 

proc means data=work.staff mean max;

   class JobTitle;

   var Salary;

run;

 

*---+----1----+----2----+----3----+----4----+----5----+----6----+---;

*** c03s1d1.sas ***;

dm "output;clear;log;clear";

 

*** Creating a library ***;

libname IA 'C:\_SasCourse2006\Course1\SAS_Data\IA';

data IA.empdata;

 infile 'C:\_SasCourse2006\Course1\SAS_Data\emplist.dat';

 input LastName $ 1-20 FirstName $ 21-30

       JobTitle $ 36-43 Salary 54-59;

run;

 

*** Examining the contents of a library ***;

proc contents data=ia._all_ nods;

run;

 

proc contents data=ia.empdata;

run;

 


*---+----1----+----2----+----3----+----4----+----5----+----6----+---;

*** c04s1d1.sas ***;

dm "output;clear;log;clear";

 

*** Creating a library ***;

libname IA 'C:\_SasCourse2006\Course1\SAS_Data\IA';

data IA.empdata2;

 infile 'C:\_SasCourse2006\Course1\SAS_Data\employe2.dat';

 input EmpID $  1-4 LastName $ 5-17 FirstName $ 18-30

       JobTitle $ 31-35 JobLevel 36 Salary 37-45;

run;

 

proc print data=ia.empdata2;

run;

proc print data=ia.empdata2;

   var JobTitle FirstName LastName Salary;

run;

proc print data=ia.empdata2 noobs;

   var JobTitle FirstName LastName Salary;

run;

proc print data=ia.empdata2 noobs;

   var JobTitle FirstName LastName Salary;

   where JobTitle='PILOT';

run;

proc print data=ia.empdata2 noobs;

   var JobTitle EmpID Salary;

   sum Salary;

run;

 

*---+----1----+----2----+----3----+----4----+----5----+----6----+---;

*** c05s2d1.sas ***;

dm "output;clear;log;clear";

 

Libname ia "C:\_SasCourse2006\Course1\SAS_Data";   

 

*** Original print instruction ***;

proc print data=ia.empdata split=' ';

   label LastName='Last Name'

         FirstName='First Name'

         Salary='Annual Salary';

   title1 'Salary Report';

run;

 

*** Print instruction with formatted salary ***;

proc print data=ia.empdata split=' ';

   label LastName='Last Name'

         FirstName='First Name'

         Salary='Annual Salary';

   format Salary dollar11.2;

   title1 'Salary Report';

run;

 

*** Print instruction with user defined formats ***;

proc format;

   value $codefmt

      'FLTAT'='Flight Attendant'

      'PILOT'='Pilot';

   value money low-<25000 ='Less than 25,000'

               25000-50000='25,000 to 50,000'

               50000<-high='More than 50,000';

run;

 

proc print data=ia.empdata split=' ' noobs;

   label LastName='Last Name'

         FirstName='First Name'

         Salary='Annual Salary';

   format Salary money. Jobcode $codefmt.;

   title1 'Salary Report in Categories';

run;

 

*---+----1----+----2----+----3----+----4----+----5----+----6----+---;

*** c05s3d1.sas ***;

dm "output;clear;log;clear";

 

Libname ia "c:\_SasCourse2006\Course1\SAS_Data";   

 

proc format;

   value $codefmt

      'FLTAT'='Flight Attendant'

      'PILOT'='Pilot';

   value money low-<25000 ='Less than 25,000'

               25000-50000='25,000 to 50,000'

               50000<-high='More than 50,000';

run;

 

*** Creating a HTML document (default) ***;

ods html file='C:\_SasCourse2006\Course1\SAS_Data\c05s3d1.html';

 

proc print data=ia.empdata label noobs;

   label Salary='Annual Salary';

   format Salary money. Jobcode $codefmt.;

   title1 'Salary Report';

run;

 

ods html close;

 


*** Creating a HTML document (minimal) ***;

ODS HTML style=minimal file='C:\_SasCourse2006\Course1\SAS_Data\c05s3d1b.html';

 

proc print data=ia.empdata label noobs;

   label Salary='Annual Salary';

   format Salary money. Jobcode $codefmt.;

   title1 'Salary Report';

   title2 'HTML listing with simplified formatting';

run;

 

ods html close;

 

*---+----1----+----2----+----3----+----4----+----5----+----6----+---;

** c06s1d1.sas ***;

dm "output;clear;log;clear";

options ps=256 ls=100 nocenter nodate nonumber nolabel;

 

*---+----1----+----2----+----3----+----4----+----5;

/* Raw data

43912/11/00LAX 20137

92112/11/00DFW 20131

11412/12/00LAX 15170

98212/12/00dfw  5 85

43912/13/00LAX 14196

98212/13/00DFW 15116

43112/14/00LaX 17166

98212/14/00DFW  7 88

11412/15/00LAX   187

98212/15/00DFW 14 31

*/

 

*** Original read - into SAS work library by default ***;

data dfwlax;

   infile 'C:\_SasCourse2006\Course1\SAS_Data\dfwlax.dat';

   input Flight $ 1-3 Date $ 4-11

         Dest $ 12-14 FirstClass 15-17

         Economy 18-20;

run;

 

proc print data=dfwlax;

   Title1 'Print from work directory';

run;

 


*** Data step creating a permanent SAS data set ***;

Libname ia "C:\_SasCourse2006\Course1\SAS_Data\IA";   

 

data ia.dfwlax;

   infile 'C:\_SasCourse2006\Course1\SAS_Data\dfwlax.dat';

   input Flight $ 1-3 Date $ 4-11

         Dest $ 12-14 FirstClass 15-17

         Economy 18-20;

run;

proc print data=ia.dfwlax;

   Title1 'Print from permanent directory';

run;

 

*---+----1----+----2----+----3----+----4----+----5----+----6----+---;

** c06s2d1.sas ***;

dm "output;clear;log;clear";

options ps=256 ls=100 nocenter nodate nonumber nolabel;

 

*---+----1----+----2----+----3----+----4;

/* Raw data

43912/11/00LAX 20137

92112/11/00DFW 20131

11412/12/00LAX 15170

98212/12/00dfw  5 85

43912/13/00LAX 14196

98212/13/00DFW 15116

43112/14/00LaX 17166

98212/14/00DFW  7 88

11412/15/00LAX   187

98212/15/00DFW 14 31

*/

 

*** Read into SAS work library with formatted date ***;

dm "output;clear;log;clear";

 

data work.dfwlax;

   infile 'C:\_SasCourse2006\Course1\SAS_Data\dfwlax.dat';

   input @1 Flight $3. @4 Date mmddyy8.

         @12 Dest $3. @15 FirstClass 3.

         @18 Economy 3.;

run;

 

proc print data=work.dfwlax;

   Title1 'Print from work directory with unformatted date output';

run;

 

** c06s2d2.sas ***;

proc print data=work.dfwlax;

   Title1 'Print from work directory with formatted date output';

   format Date date9.;

run;

 

*---+----1----+----2----+----3----+----4----+----5----+----6----+---;

** c06s3d1.sas ***;

dm "output;clear;log;clear";

options ps=256 ls=100 nocenter nodate nonumber nolabel;

 

*---+----1----+----2----+----3----+----4----+----5;

/* Raw data

0031GOLDENBERG   DESIREE      PILOT1 50221.62

0040WILLIAMS     ARLENE M.    FLTAT1 23666.12

0071PERRY        ROBERT A.    FLTAT1 21957.71

0082MCGWIER-WATTSCHRISTINA    PILOT3 96387.39

0091SCOTT        HARVEY F.    FLTAT2 32278.40

0106THACKER      DAVID S.     FLTAT1 24161.14

0275GRAHAM       DEBORAH S.   FLTAT2 32024.93

0286DREWRY       SUSAN        PILOT1 55377.00

0309HORTON       THOMAS L.    FLTAT1 23705.12

0334DOWN         EDWARD       PILOT1 56584.87

0347CHERVENY     BRENDA B.    FLTAT2 38563.45

0355BELL         THOMAS B.    PILOT1 59803.16

0366GLENN        MARTHA S.    PILOT3120202.38

0730BELL         CARLA        PILOT1 37397.93

0739SAYRE        MARCO        PILOT1 59268.61

*/

 

Libname ia "C:\_SasCourse2006\Course1\SAS_Data\ia"

data work.empdata2;

   infile 'C:\_SasCourse2006\Course1\SAS_Data\employe2.dat';

   input EmpID $ 1-4 LastName $ 5-17 FirstName $ 18-30

         JobCode $ 31-36 Salary 37-45;

run;

 

proc print data=work.empdata2;

   Title1 'Listing of employee data';

run;

 

** c06s3d2.sas ***;

data work.empdata2;

   infile 'C:\_SasCourse2006\Course1\SAS_Data\employe2.dat';

   input EmpID $ 1-4 LastName $ 5-17 FirstName $ 18-30

         JobCode 31-36 Salary 37-45;

run;

 

proc print data=work.empdata2;

   Title1 'Listing of employee data with missing data.  Why?';

run;

 


*---+----1----+----2----+----3----+----4----+----5----+----6----+---;

*** c06s4d1.sas ***;

dm "output;clear;log;clear";

 

/* Raw data

43912/11/00LAX 20137

92112/11/00DFW 20131

11412/12/00LAX 15170

98212/12/00dfw  5 85

43912/13/00LAX 14196

98212/13/00DFW 15116

43112/14/00LaX 17166

98212/14/00DFW  7 88

11412/15/00LAX   187

98212/15/00DFW 14 31

*/

 

Libname ia "C:\_SasCourse2006\Course1\SAS_Data\IA"

 

data ia.dfwlax;

   infile 'C:\_SasCourse2006\Course1\SAS_Data\dfwlax.dat';

   input @1 Flight $3. @4 Date mmddyy8.

         @12 Dest $3. @15 FirstClass 3.

         @18 Economy 3.;

run;

 

proc contents data=ia.dfwlax;

   TITLE1 'Basic dataset';

run;

 

proc print data=ia.dfwlax label;

   TITLE1 'Data listing with formatted datasets';

   format Date mmddyy10.;

   label Dest='Destination'

         FirstClass='First Class Passengers'

         Economy='Economy Passengers';

run;

 

data ia.dfwlax;

   infile 'C:\_SasCourse2006\Course1\SAS_Data\dfwlax.dat';

   input @1 Flight $3. @4 Date mmddyy8.

         @12 Dest $3. @15 FirstClass 3.

         @18 Economy 3.;

   format Date mmddyy10.;

   label Dest='Destination'

         FirstClass='First Class Passengers'

         Economy='Economy Passengers';

run;

 


proc contents data=ia.dfwlax;

   TITLE1 'Contents of dataset with formatting and labeling done in the DATA step';

run;

 

proc print data=ia.dfwlax label;

   TITLE1 'Listing of dataset with formatting and labeling done in the DATA step';

run;

 

proc print data=ia.dfwlax label;

   TITLE1 'Listing of dataset with formatting and labeling done in the DATA step';

   TITLE2 'Note that date format is altered in the print statement';

   format Date date9.;

run;

 

*** c06s4d2.sas ***;

*** Generating a new dataset with formatting and labels included ***;

data ia.dfwlax;

   infile 'dfwlax.dat'; *PC and Unix;

   *infile '.prog1.rawdata(dfwlax)'; *TSO;

   input @1 Flight $3. @4 Date mmddyy8.

         @12 Dest $3. @15 FirstClass 3.

         @18 Economy 3.;

   format Date mmddyy10.;

   label Dest='Destination'

         FirstClass='First Class Passengers'

         Economy='Economy Passengers';

run;

 

proc contents data=ia.dfwlax;

   TITLE1 'Contents of dataset with formatting and labeling done in the DATA step';

run;

 

proc print data=ia.dfwlax label;

   TITLE1 'Listing of dataset';

run;

 

 

*** c06s4d2.sas ***;

proc print data=ia.dfwlax label;

   TITLE1 'Listing of dataset with altered date format';

   format Date date9.;

run;

 


*---+----1----+----2----+----3----+----4----+----5----+----6----+---;

*** c06s5d1.sas ***;

dm "output;clear;log;clear";

 

Libname ia "C:\_SasCourse2006\Course1\SAS_Data\IA"

 

proc contents data=ia.dfwlax;

run;

 

proc datasets library=ia;

   modify dfwlax;

   rename Dest=Destination;

run;

 

proc contents data=ia.dfwlax;

run;

 

/* Restoring variable name to support subsequent examples

   that assume the original name */

proc datasets library=ia;

   modify dfwlax;

   rename Destination=Dest;

run;

 

*---+----1----+----2----+----3----+----4----+----5----+----6----+---;

*** C06S6D1 ***;

dm "output;clear;log;clear";

 

Libname ia "C:\_SasCourse2006\Course1\SAS_Data\IA"

 

proc print data=work.dfwlax;

run;

 

PROC IMPORT OUT= WORK.DFWLAX

     DATAFILE = 'C:\_SasCourse2006\Course1\SAS_Data\DallasLA.xls'

                DBMS=EXCEL2000 REPLACE;

     MIXED=YES;

RUN;

PROC IMPORT OUT= WORK.DFWLAX

     DATAFILE = 'C:\_SasCourse2006\Course1\SAS_Data\DallasLA.txt'

                DBMS=TAB REPLACE;

RUN;

 

libname xlsdata 'C:\_SasCourse2006\Course1\SAS_Data\DallasLA.xls';

proc print data=xlsdata.dfwlax;

run;

 


*---+----1----+----2----+----3----+----4----+----5----+----6----+---;

 

*** C06S6D1 ***;

dm "output;clear;log;clear";

 

Libname ia "C:\_SasCourse2006\Course1\SAS_Data\IA"

proc print data=work.dfwlax;

   Title1 'Database does not exist';

run;

 

*** C06S6D2 ***;

/* raw data

Flight

Date

Dest

FirstClass

Economy

439

12/11/00

LAX

20

137

921

12/11/00

DFW

20

131

114

12/12/00

LAX

15

170

982

12/12/00

dfw

5

85

439

12/13/00

LAX

14

196

982

12/13/00

DFW

15

116

431

12/14/00

LaX

17

166

982

12/14/00

DFW

7

88

114

12/15/00

LAX

 

187

982

12/15/00

DFW

14

31

*/

 

PROC IMPORT OUT= WORK.DFWLAX

     DATAFILE = 'C:\_SasCourse2006\Course1\SAS_Data\DallasLA.xls'

                DBMS=EXCEL2000 REPLACE;

     MIXED=YES;

RUN;

 

proc print data=work.dfwlax;

   Title1 'Listing of data created from EXCEL database';

run;

 

*** C06S6D3 ***;

PROC IMPORT OUT= WORK.DFWLAX

     DATAFILE = 'C:\_SasCourse2006\Course1\SAS_Data\DallasLA.txt'

                DBMS=TAB REPLACE;

RUN;

 

proc print data=work.dfwlax;

   Title1 'Listing of data created from tab separated text file';

run;

 


*** C06S6D4 ***;

/* raw data

Flight    Date Dest FirstClass    Economy

439  12/11/2000    LAX  20   137

921  12/11/2000    DFW  20   131

114  12/12/2000    LAX  15   170

982  12/12/2000    dfw  5    85

439  12/13/2000    LAX  14   196

982  12/13/2000    DFW  15   116

431  12/14/2000    LaX  17   166

982  12/14/2000    DFW  7    88

114  12/15/2000    LAX       187

982  12/15/2000    DFW  14   31

*/

 

libname xlsdata 'C:\_SasCourse2006\Course1\SAS_Data\DallasLA.xls';

proc print data=xlsdata.dfwlax;

   Title1 'Listing of data created from tab separated text file';

run;

 

*** C06S6D- ***;

/* raw data

Flight,Date,Dest,FirstClass,Economy

439,12/11/00,LAX,20,137

921,12/11/00,DFW,20,131

114,12/12/00,LAX,15,170

982,12/12/00,dfw,5,85

439,12/13/00,LAX,14,196

982,12/13/00,DFW,15,116

431,12/14/00,LaX,17,166

982,12/14/00,DFW,7,88

114,12/15/00,LAX,,187

982,12/15/00,DFW,14,31

*/

 

PROC IMPORT OUT= WORK.DFWLAX

     DATAFILE = 'C:\_SasCourse2006\Course1\SAS_Data\DallasLA.csv'

                DBMS=CSV REPLACE;

RUN;

 

proc print data=work.dfwlax;

   Title1 'Listing of data created from tab separated text file';

run;

 


*---+----1----+----2----+----3----+----4----+----5----+----6----+---;

*** c07s1d1.sas ***;

dm "output;clear;log;clear";

 

Libname ia "C:\_SasCourse2006\Course1\SAS_Data"

 

** c06s2d1.sas ***;

*** create the permanent SAS dataset DFWLAX ***;

data IA.dfwlax;

   infile 'C:\_SasCourse2006\Course1\SAS_Data\dfwlax.dat';

   input @1 Flight $3. @4 Date mmddyy8.

         @12 Dest $3. @15 FirstClass 3.

         @18 Economy 3.;

run;

 

proc print data=IA.dfwlax;

   format Date date9.;

run;

 

*** create the temporary SAS dataset ONBOARD ***;

data onboard;

   set ia.dfwlax;

   Total=FirstClass+Economy;

run;

 

proc print data=onboard;

   format Date date9.;

run;

 

** c07s1d2.sas ***;

data onboard;

   set ia.dfwlax;

   Total=sum(FirstClass,Economy);

run;

 

proc print data=onboard;

   format Date date9.;

run;

 

** c07s1d3.sas ***;

data onboard;

   set ia.dfwlax;

   Total=FirstClass+Economy;

   DayOfWeek=weekday(Date);

run;

 

proc print data=onboard;

   var Flight Dest Total DayOfWeek Date;

   format Date weekdate.;

run;

 

** c07s1d4.sas ***;

data onboard;

   set ia.dfwlax;

   drop FirstClass Economy;

   Total=FirstClass+Economy;

run;

 

proc print data=onboard;

   format Date date9.;

run;

 

*---+----1----+----2----+----3----+----4----+----5----+----6----+---;