Script for SAS session for Stat 430 =================================== (I) Lecture 1, 8/30/06 In the text, the first approach to data entry is done by copying or typing, e.g. data examp0; input subject gender $ Exam1 Exam2 Hwgrade $; datalines; 10 M 80 84 A 7 M 85 89 A 4 F 90 86 B 20 M 82 85 B 25 F 94 94 A 14 F 88 84 C ; run; But it is usually more convenient to refer to existing files of data, and that is what we do next. testdat (text) file contains: 10 M 80 84 A 7 M 85 89 A 4 F 90 86 B 20 M 82 85 B 25 F 94 94 A 14 F 88 84 C data examp1; infile "testdat"; input subject gender $ Exam1 Exam2 Hwgrade $; run; To display the data, you can do: proc print; /* could also say proc print data=examp1 */ run; Inputting data by column numbers: data examp1B; infile "testdat"; input subject 1-2 gender $ 4 Exam1 7-8 Exam2 11-12 Hwgrade $ 14; run; LOTS OF MISSING DATA BECAUSE I DID NOT COUNT THE INITIAL BLANK COLUMN! data examp1c; infile "testdat"; input subject 2-3 gender $ 5 Exam1 8-9 Exam2 12-13 Hwgrade $ 15; run; * OK now ; Now saved in testdat2: 10 M 80 84 A 7 M 85 89 A 4 F 90 86 B 20 M 82 85 B 25 F 94 94 A 14 F 88 84 C The columns are ragged, and we try again, mixing fixed and free format : data examp2; infile "testdat2"; input subject 1-4 gender $ Exam1 Exam2 Hwgrade $; run; TO SAVE A PERMANENT SAS DATASET WE NEED TO SPECIFY A DIRECTORY AS A "LIBNAME" and then use it as a prefix libname myhom "."; data myhom.examp2; infile "testdat2"; input subject 1-4 gender $ Exam1 Exam2 Hwgrade $; run; * examp2.sas7bdat now appears as a file in home directory (from which SAS was invoked) ; and everything worked OK, as printed output shows. Here we can print more selectively using PROC PRINT, using the optional "where" specification". PROC PRINT data=examp2; where hwgrade = "A"; run; * prints obs 1,2,5; The help files to understand the options are not very easy to use, as witness the fact that it is almost impossible to find out how, directly, to print just a specified number of records. Here is one way to do it, by writing a little program to subset the data first: data examp2B; set examp2; if _N_ <= 4; PROC PRINT; run; But we can also get the first 4 observations by: PROC PRINT data=examp2 (obs=4); run; If we had wanted the 3 observations, say, beginning with the 2nd, we could have written PROC PRINT data=examp2 (firstobs=2 obs=4);