Download presentation
Presentation is loading. Please wait.
Published byAvis Jenkins Modified over 9 years ago
1
Introduction to SAS Essentials Mastering SAS for Data Analytics Alan Elliott and Wayne Woodward SAS Essentials - Elliott & Woodward1
2
Intro to SAS Chapter 2
3
LEARNING OBJECTIVES To enter data using freeform list input To enter data using the compact method To enter data using column input To enter data using formatted input To enter data using the INFILE technique To enter multiple-line data
5
2.1 Using SAS Data Steps The DATA step is used to define a SAS data set, and to manipulate it and prepare it for analysis. In the SAS language, the DATA statement signals the creation of a new data set. For example, a DATA statement in SAS code may look like this: DATA MYDATA;
6
DATA Statement Signals the beginning of the DATA step. Assigns a name (of your choice) to the data set created in the DATA Step. The general form of the DATA statement is: DATA datasetname;
7
Temporary Data Set Names The SAS “datasetname” in the DATA statement can have several forms: The SAS datasetname can be a single name (used for temporary data sets, kept only during current SAS session). For example: DATA EXAMPLE; DATA OCT2007;
8
Permanent Data Set Names Or, the SAS datasetname can be a two-part name. The two-part name tells SAS that this permanent data set will be stored on disk beyond the current SAS session in a SAS “library” indicated by the prefix name. For example: DATA RESEARCH.JAN2016; DATA CLASS.EX1; DATA JONES.EXP1;
9
Windows Data Set Names A SAS data set name can also refer directly to the Windows name of a file on your hard disk. For example: DATA “C:\SASDATA\SOMEDATA”; DATA “C:\MYFILES\DECEMBER\MEASLES”; (note these refer to SAS data set files with.sasb7dat extensions – thus SOMEDATA.SASB7DAT and MEASLES.SASB7DAT)
10
Tasks done within the DATA Step DATA datasetname; ;
11
2.2 Understanding SAS data set structure ID SBP DBP GENDER AGE WT obs 1 1 120 80 M 15 115 obs 2 2 130 70 F 25 180......... obs 100 100 125 80 F 20 110 Columns are data variables (each named) and rows are subjects, observations, or records.
12
Columns and Rows Each column represents a variable and is designated with a variable name (ID, SBP, etc.) Every SAS variable (column) must have a name, and the names must follow certain naming rules. Each row, marked here as obs1, obs2, etc., indicate observations or records. An observation consists of data observed from one subject or entity.
13
2.3 Rules for SAS variable names SAS Variable names must be 1 ‑ 32 characters long – but must not include any blanks. must start with the letters A through Z or the _ (underscore). A name cannot include a blank. may include numbers (but not as first character in name). may include upper and lower case characters (variable names are case insensitive). should be descriptive of the variable (optional but recommended).
14
Correct SAS variable names are GENDERAGE_IN_1999 AGEin1999 _OUTCOME HEIGHT_IN_CM WT_IN_LBS Incorrect SAS variable names are (WHY?) AGE IN 20002000MeaslesCount S S NumberQuestion 5 WEIGHT IN KGAGE-In-2000
15
2.4 Understanding SAS Variable Types Numeric Variables (Default): A numeric variable is used to designate values that could be used in arithmetic calculations or are grouping codes for categorical variables. For example, the variables SBP (systolic blood pressure), AGE, and WEIGHT are numeric. However, an ID number, phone number, or Social Security number should not be designated as a numeric variable. For one thing, you typically would not want to use them in a calculation. Moreover, for ID numbers, if ID = 00012 were stored as a number, it would lose the zeros and become 12.
16
Character (Text, String) Variables Character (Text, String) Variables: Character variables are used for values that are not used in arithmetic calculations. For example, a variable that uses M and F as codes for gender would be a character variable. For character variables, case matters, because to the computer a lowercase f is a different character from an uppercase F. It is important to note that a character variable may contain numerical digits. As mentioned previously, a Social Security number (e.g., 450-67-7823) or an ID number (e.g., 143212) should be designated as a character variable because their values should never be used in mathematical calculations. When designating a character variable in SAS, you must indicate to SAS that it is of character type. This is shown in upcoming examples.
17
Date Variables Date Variables: A date value may be entered into SAS using a variety of formats, such as 10/15/09, 01/05/2010, JAN052010, and so on. As you will see in upcoming examples, dates are handled in SAS using format specifications that tell SAS how to read or display the date values. For more information about dates in SAS, see Appendix B.
18
2.5 Methods of reading data into SAS Reading data using freeform list input Reading data using the compact method Reading data using column input Reading data using formatted input.
19
Freeform Data Entry Open the file DFREEFORM.SAS DATA MYDATA; INPUT ID $ SBP DBP GENDER $ AGE WT; DATALINES; 1 120 80 M 15 115 2 130 70 F 25 180 3 140 100 M 89 170 4 120 80 F 30 150 5 125 80 F 20 110 ; PROC PRINT; RUN; The INPUT statement defines the variables (some character, designated by $ after name) The data must match the INPUT statement – the same number of values per line, separated with blanks. DATALINE indicates that data are listed next. DATA ends with a semicolon.
20
Data set created from code Obs ID SBP DBP GENDE R AGE WT 1112080M15115 2213070F25180 33140100M89170 4412080F20150 510012580F20110
21
Advantages of freeform list input Easy, very little to specify. No rigid column positions which makes data entry easy. If you have a data set where the data are separated by blanks, this is the quickest way to get your data into SAS.
22
Restrictions for freeform list input Every variable on each data line must be in the order specified by the INPUT statement. Fields must be separated by at least one blank. Blank spaces representing missing variables are not allowed. Having a blank space in the data causes values to be out of sync. If there are missing values in the data, a dot (.) should be placed in the position of that variable in the data line. For example, a data line with AGE missing might read: 4 120 80 F. 150 No embedded blanks are allowed within the data value for a character field, like MR ED. A character field has a default maximum length of 8 characters in freeform input.
23
Compact Data Format DATA WEIGHT; INPUT TREATMENT LOSS @@; DATALINES; 1 1.0 1 3.0 1 -1.0 1 1.5 1 0.5 1 3.5 2 4.5 2 6.0 2 3.5 2 7.5 2 7.0 2 6.0 2 5.5 3 1.5 3 -2.5 3 -0.5 3 1.0 3.5 ; PROC PRINT; RUN; The @@ symbol in the INPUT statement tells SAS to allow multiple rows of data on each line. You must be careful that the data matches the input definition.
24
Hands-On Example p 27 1. Open the program file DFREEFORM.SAS. (The code was shown above.) Run the program. 2. Observe that the output listing (shown here) illustrates the same information as in Table 2.4. Etc…
25
Column Input (This is DCOLUMN.SAS) DATA MYDATA; INPUT ID $ 1 SBP 2-4 DBP 5-7 GENDER $ 8 AGE 9- 10 WT 11-13; DATALINES; 1120 80M15115 2130 70F25180 3140100M89170 4120 80F30150 5125 80F20110 ; RUN; PROC MEANS; RUN; Note how data are in specific columns. In the INPUT statement, the columns are specified by the ranges following a variable name. INPUT variable startcol ‑ endcol...;
26
Advantages of column input Data fields can be defined and read in any order in the INPUT statement and unneeded columns of data can be skipped. Blanks are not needed to separate fields. Character values can range from 1 to 200 characters. For example: INPUT DIAGNOSE $ 1 ‑ 200; For character data values, embedded blanks are no problem, e.g., John Smith Input only the variables you need -- skip the rest. This is handy when your data set (perhaps downloaded from a large database) contains variables you’re not interested in using. Only read the variables you need.
27
Rules and restrictions for column Input Data values must be in fixed column positions. Blank fields are read as missing. Character fields are read right justified in the field. Column input has more specifications than list input. You must specify the column ranges for each variable.
28
How SAS interprets column data INPUT GENDER $ 1 ‑ 3; 1 2 3 4 5 6 71 2 3 4 M M---> All read as M M
29
Numbers in Columns INPUT X 1 ‑ 6; 1 2 3 4 5 6 7READ AS 2 3 0230 2 3. 023.0 2. 3 E 12.3E1 or 23 2 323 - 2 3-23 Do Hands-On Exercise p 31
30
Reading Data Using Formatted Input DATA MYDATA; INPUT @col variable1 format. @col variable2 format....; Example: DATA MYDATA INPUT @1 SBP 3. @4 DBP 3. @7 GENDER $1. @8 WT 3. @12 OWE COMMA9.;
31
Three Components In Formatted Input @1 SBP 3. The @ is the starting column “pointer” so @1 means start in column 1 The variable name. The “informat” defines what kind of data to input. In this case “3.” indicates an integer with at most 3 digits.
32
Input Formats Table 2.6, p 33
33
Informats & Formats In SAS INFORMATS are used to read data into a SAS data set FORMATS are used to specify how to output (write) data values Mosts Format specifications (such as MMDDYY10.) can be used as EITHER an Informat or a format.
34
More about formats Formats usually end with a dot (.) or a dot followed by a number 5. : A number up to five digits, no decimals, so could take on values from -9999 to 99999. 5.2 : A number up to five digits, and up to 2 decimals – so could take on values from -9.99 to 99.99 $5. : A character value of up to five digits, such as ABCDE, abcde, 12345 or (*&6%
35
Advantages & restrictions for using formatted input Advantages and restrictions are similar to those for column input. The primary difference is the ability to read in data using INFORMAT specifications. Is particularly handy for reading dates and dollar values. Restrictions are similar to those for column input. Do Hands-On Exercise, p 36
36
Some Common Output Formats
37
Using the SAS INFORMAT Statement There is a SAS statement named INFORMAT that you could use in the freeform data entry case. For example, (p 37) DATA PEOPLE; INFORMAT LASTNAME FIRSTNAME $12. AGE 3. SCORE 4.2; INPUT LASTNAME FIRSTNAME AGE SCORE; datalines; Lincoln George 35 3.45 Ryan Lacy 33 5.5 ; PROC PRINT DATA=PEOPLE; RUN;
38
Reading External Data Using INFILE Suppose you have text data in a file in this format 101 A 12 22.3 25.3 28.2 30.6 5 0 102 A 11 22.8 27.5 33.3 35.8 5 0 104 B 12 22.8 30.0 32.8 31.0 4 0 110 A 12 18.5 26.0 29.0 27.9 5 1 How can you read this data into SAS?
39
INFILE Statement INSTEAD of the DATALINES statement (followed by data), you use the INFILE Statement: DATA MYDATA; INFILE 'C:\SASDATA\EXAMPLE.DAT'; INPUT ID $ 1-3 GP $ 5 AGE 6-9 TIME1 10-14 TIME2 15-19 TIME3 20-24; RUN; PROC MEANS; RUN; NOTE: There is no DATALINES statement. This is where the DATA step ends and the PROC step begins. INFILE replaces DATALINES, and defines where the data are located on disk.
40
DATALINES vs INFILE When data are in the program code: Use DATALINES statement When data are read from external source: Use INFILE statement. ALSO NOTE: Do not confuse INFILE with the INPUT statement. Do Hands-On Example p 38
41
2.6 Going Deeper – More Techniques Reading Multiple Records per Observation If your data for each record extends multiple lines: You can use more than one INPUT statement INPUT ID $ SEX $ AGE WT; INPUT SBP DBP BLDCHL; INPUT OBS1 OBS2 OBS3; Or you can read the data using the / (advance) indicator: INPUT ID $ SEX $ AGE WT/ SBP DBP BLDCHL/ OBS1 OBS2 OBS3; Do Hands-On Example p 40. (MULTILINE.SAS)
42
Input Pointer Controls
43
Using Advanced INFILE Options DLM: This option allows you to define a delimiter to be something other than a blank. For example, if data are separated by commas, include the option DLM = ‘,’ in the INFILE statement. FIRSTOBS= # :Tells SAS on what line you want it to start reading your raw data file. This is handy if your data file contains one or more header lines or if you want to skip the first portion of the data lines. OBS= # :Indicates which line in your raw data file should be treated as the last record to be read by SAS. See Table 2.13 p 41
44
Do Hands-On Example, p 42 (DINFILE2.SAS) DATA MYDATA; INFILE 'C:\SASDATA\EXAMPLE.CSV' DLM=', ' FIRSTOBS=2 OBS=26; INPUT GROUP $ AGE TIME1 TIME2 TIME3 Time4 SOCIO; PROC MEANS; RUN ;
45
Do Hands On Exercise DINFILE3.SAS p 43 1. Open the program file DINFILE3.SAS. DATA PLACES; INFILE DATALINES DLMSTR='! ∼ !'; INPUT CITY $ STATE $ ZIP; DATALINES; DALLAS! ∼ !TEXAS! ∼ !75208 LIHUE! ∼ !HI! ∼ !96766 MALIBU! ∼ !CA! ∼ !90265 ; PROC PRINT; Etc… Note strange delimiters
46
Input a data set where there are blanks
47
Do Hands On Example p 44 (DINFILE4.SAS) DATA TEST; INFILE "C:\SASDATA\DINFILEDAT.TXT"; INPUT LAST $1-21 FIRST $ 22-30 ID $ 31- 36 ROLE $ 37-44; RUN; PROC PRINT DATA=TEST;RUN; Etc…
48
Summary One of the most powerful features of SAS, and a reason it is used in many research and corporate environments, is that it is very adaptable for reading in data from a number of sources. This chapter showed only the tip of the iceberg. More information about getting data into SAS is provided in the following chapter. For more advanced techniques, see the SAS documentation.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.