/* SAS PROGRAMMING HANDOUT #2 THE DATA STEP SAS programs generally consist of a series of "DATA STEP"s and "PROC"s. The "DATA STEP" are used to enter and manipulate data. The "PROC"s are used to perform preprogrammed statistical procedures. DATA STEP: The easiest way to enter data into a data step is as follows: */ DATA ONE; *Here ONE is the name of the data set. (no funny symbols); INPUT X A $; *This data set has 2 variables. X is numeric and A is character; X2=X*X; *New variables can be defined after the INPUT statement.; CARDS; *COMMENTS go here; 21 ABC 23.2 A .3 XYZ PROC PRINT DATA=ONE; *USE CURSOR TO HIGHLITE PROGRAM AND SUBMIT; RUN; Data can also be read from an ASCI file on your diskette. Save the above three lines of data as a file on A: (or C:) called ONE.DAT Click on FILE, SAVE AS, A:(or C:), *.dat type, then give filename DATA TWO; INFILE 'A:\ONE.DAT'; INPUT X A $; PROC PRINT DATA=TWO; RUN; Data can also be saved as a "permanent SAS data set". LIBNAME BOB 'A:\'; DATA BOB.THREE; SET TWO; RUN; This will save the data set on A; as THREE.SAS7BDAT In version 6 the extension was *.SD2 DATA FOUR; SET BOB.THREE; PROC PRINT DATA=FOUR; RUN; This will create a new data set using THREE.SAS7BDAT Data can also be written from a SAS data set to an ASCI file DATA FIVE; SET FOUR; FILE 'A:\SIX.DAT'; PUT @1 X @10 "xxx" @20 A; RUN; There are several other ways to enter data into the SAS system 1) INFILE 'A:\COMMA.CSV' DELIMITER=','; This will read a comma delimited data set. 2) Click on FILE, IMPORT, and follow directions This will let you use EXCEL files etc You should first create a LIBNAME. The feature does not always read the data correctly, so be sure to check your data. 3) You can also use PROC IMPORT. The feature does not always read the data correctly, so be sure to check your data. You can also export files to different data types. For example: PROC IMPORT OUT=WORK.jim REPLACE DATAFILE='a:\data1.xls'; SHEET="sheet1"; GETNAMES=NO; proc print data=work.jim; var f1 f2; run; This will read sheet1 in data1.xls into a sas data set called jim in the temporary work library, I am assuming the excel spreadsheet does not have variable names. SAS will use F1, etc Use GETNAME=YES; if the excel spreadsheet has variable names in the first row. 4) PROC INSIGHT allows a spreadsheet form of data entry. NOTE: Good SAS programming requires proper indentation. DATA and PROC start in column 1, everything else starts in Column 2 or higher. Always specify the data set in a PROC Include comments, use sensible data set names and variable names revised 10/15/2002