/**************************************************************************** /home/lsimon/stat597c/sas/input1.sas Written By: Laura J. Simon Date: January 14, 1996 This program illustrates creating SAS datasets by: * reading data directly through the CARDS statement * reading data in from an ascii dataset via the INFILE statement * reading data from another SAS dataset. The program illustrates the creation of both TEMPORARY and PERMANENT SAS datasets. After each DATA step, the SAS print procedure, PROC PRINT is used to print the resulting SAS data set. ****************************************************************************/ OPTIONS PS=60 LS=80 NODATE; *Set line size to 80 chars and page size to 60 lines. Also show no date in output; LIBNAME stat597c '/home/lsimon/stat597c/data'; *Specify the SAS data library (directory); FILENAME temp2dat '/home/lsimon/stat597c/data/temp2.dat'; *Specify 'FILEREF'; /**************************************************************************** DATA temp1; This DATA step creates a temporary SAS dataset called 'temp1'. It is temporary, because there is only a one-level name in the data statement. Because it is temporary, the dataset exists only until the end of the current SAS session. The SAS System places the data set in a SAS data library referred to as WORK. So, the data statement 'DATA temp1' creates a SAS dataset named WORK.TEMP1. The SAS System always refers to datasets with their two-level names, regardless of whether they are temporary or permanent. The CARDS statement indicates that data lines follow. The single semicolon (;), which must be present, marks the end of the data. The CARDS statement and data lines must occur last in the DATA step statement. The CARDS statement is useful when you have only a few data lines. The INPUT statement identifies the fields to be read from the input data, and names the SAS variables to be created from them. (The input statement uses the 'list input' style.) The RUN statement tells the SAS System to execute the preceding statements. *****************************************************************************/ DATA temp1; input subj gender height weight; CARDS; 1024 1 65 125 1167 1 68 140 1168 2 68 190 1201 2 72 190 1302 1 63 115 ; RUN; PROC PRINT data=temp1; title 'Output dataset: TEMP1'; RUN; /**************************************************************************** DATA temp2; This DATA step creates a permanent SAS dataset called STAT597C.TEMP2. It is PERMANENT because there is a two-level name in the data statement. The LIBNAME statement above identifies the directory to which the data should be written. Therefore, the resulting SAS dataset is written to /home/lsimon/stat597c/data. Note: The contents and structure of WORK.TEMP1 is identical to STAT597C.TEMP2. The only thing that differs between the two datasets is that TEMP1 is temporary and TEMP2 is permanent. **************************************************************************/ DATA stat597c.temp2; input subj gender height weight; CARDS; 1024 1 65 125 1167 1 68 140 1168 2 68 190 1201 2 72 190 1302 1 63 115 ; RUN; PROC PRINT data=stat597c.temp2; title 'Output dataset: STAT597C.TEMP2'; RUN; /************************************************************************* DATA temp3; This DATA step creates a temporary SAS dataset called WORK.TEMP3, which is identical to WORK.TEMP1. The data are read in from an ascii file (/home/lsimon/stat597c/data/temp2.dat) using the INFILE statement rather than directly through the CARDS statement. The infile statement must precede the input statement. *************************************************************************/ DATA temp3; infile '/home/lsimon/stat597c/data/temp2.dat'; input subj gender height weight; RUN; PROC PRINT data=temp3; title 'Output dataset: TEMP3'; RUN; /************************************************************************* DATA temp4; This DATA step creates a temporary SAS dataset called WORK.TEMP4, which is identical to WORK.TEMP1. The data are read in from an ascii file (/home/lsimon/stat597c/data/temp2.dat) using a FILEREF in an INFILE statement. The FILEREF functions as a shorthand way of referring to an external file. Once defined in a FILENAME statement, the FILEREF can be used in later SAS statements that reference the file, such as the FILE or INFILE statement. Setting up a FILEREF is most advantageous when you need to refer to the filename several times in a program. *************************************************************************/ DATA temp4; infile temp2dat; input subj gender height weight; RUN; PROC PRINT data=temp4; title 'Output dataset: TEMP4'; RUN; /************************************************************************* DATA temp5; This DATA step creates a temporary SAS dataset called WORK.TEMP5, which is identical to WORK.TEMP2. The data are read in from the existing SAS dataset (/home/lsimon/stat597c/data/temp2.ssd01). To read in the data, the existing SAS dataset's name is indicated in the SET statement. Note that because the variables in the existing dataset have already been named, NO input statement is necessary. The SET statement indicates that the data being read are already in the structure of a SAS dataset, and gives the name of the dataset. *************************************************************************/ DATA temp5; set stat597c.temp2; RUN; PROC PRINT data=temp5; title 'Output dataset: TEMP5'; RUN;