2. Data Input

Input may be done directly in PROG window or may come from another file.

 

2.1 Direct Input

<<Syntax>>

Data <<data set name>>;

Input <<variablename1 variable name 2 …>>;

Cards;

<<data>>

;

run;

example1.

Data sales; [1]

input expen rev; [2]

cards; [3]

1 1

2 1

3 2

4 2

5 4

; [4]

run;

[1] Begin the DATA step and create SAS data set Sales.

[2] Read a data line and assign values to two variables.

[3] Begin the data lines.

[4] Signal end of data lines with a semicolon and execute the DATA step

example2.

data weight;

input name $ Week1 Week16; [5]

cards;

A 195 163

B 220 198

C 173 155

D 135 116

;

run;

[5] Note that name is followed by a dollar sign ($) indicating that this is a character variable.

example3.

data weight2;

input name $ week1 week16 @@; [6]

cards;

A 195 163 B 220 198 C 173 155 D 135 116

;

[6] You can also read multiple observations in each line of data. Simply add the @@ symbol at the end of the input statement. (Compare this to example3)

 

2.2 Reading data from another files

To read another file in a SAS DATA step use the infile statement before the INPUT statement.

<<Syntax>>

Data <<data set name>>;

Infile <<’your input file’>>;

Input <<variablename1 variable name 2 …>>;

run;

 

example4.

Data sales; [1]

Infile ‘A:\sales.dat’; [2]

input expen rev; [3]

run;

[1] Begin the DATA step and create SAS data set Sales.

[2] Specify the external file that contains your data. In this example sales.dat in A drive contains data.

[3] Read a record and assign values to two variables.