![]() Chapter Contents |
![]() Previous |
![]() Next |
| Creating a SAS Data File or a SAS Data View |
The following DATA statement creates a data view called MONTHLY_SALES.
data monthly_sales / view=monthly_sales;
The following DATA statement creates a data file called TEST_RESULTS.
data test_results;
| Sources of Input Data |
Usually DATA steps read input data records from only one of the first three sources of input. However, DATA steps can use a combination of some or all of the sources.
| Reading Raw Data |
data weight; [1] infile 'your-input-file'; [2] input IDnumber $ Week1 Week16; [3] WeightLoss=Week1-Week16; [4] run; [5] proc print data=weight; [6] run; [7]
Begin the DATA step and create a SAS data
set called WEIGHT. | |
Specify the external file that contains your
data. | |
Read a record and assign values to three variables. | |
Calculate a value for variable
WeightLoss. | |
Execute the DATA step. | |
Print data set WEIGHT using the PRINT
procedure. | |
Execute the PRINT procedure. |
This example reads raw data from instream data lines.
data weight2; [1] input IDnumber $ Week1 Week16; [2] WeightLoss2=Week1-Week16; [3] datalines; [4] 2477 195 163 2431 220 198 2456 173 155 2412 135 116 ; [5] proc print data=weight2; [6] run; [7]
Begin the DATA step and create SAS data set
WEIGHT2. | |
Read a data line and assign values to three
variables. | |
Calculate a value for variable WeightLoss2. | |
Begin the data
lines. | |
Signal end of data lines with a semicolon
and execute the DATA step. | |
Print data set WEIGHT2 using the PRINT procedure. | |
Execute the PRINT
procedure. |
data weight2; infile datalines missover; [1] input IDnumber $ Week1 Week16; WeightLoss2=Week1-Week16; datalines; [2] 2477 195 163 2431 2456 173 155 2412 135 116 ; [3] proc print data=weight2; [4] run; [5]
Use the MISSOVER option to assign missing
values to variables that do not contain values. | |
Begin data lines. | |
Signal end of data lines and
execute the DATA
step. | |
Print data set WEIGHT2 using the PRINT procedure. | |
Execute the PRINT
procedure. |
options pageno=1 nodate linesize=60 pagesize=80;
data all_errors;
length filelocation $ 60;
input filelocation; /* reads instream data */
infile daily filevar=filelocation
filename=daily end=done;
do while (not done);
input Station $ Shift $ Employee $ NumberOfFlaws;
output;
end;
put 'Finished reading ' daily=;
datalines;
. . .myfile_A. . .
. . .myfile_B. . .
. . .myfile_C. . .
;
proc sort data=all_errors out=sorted_errors;
by Station;
run;
proc print data = sorted_errors;
title 'Flaws Report sorted by Station';
run;
Multiple Input Files in Instream Data
Flaws Report sorted by Station 1
Number
Obs Station Shift Employee OfFlaws
1 Amherst 2 Lynne 0
2 Goshen 2 Seth 4
3 Hadley 2 Jon 3
4 Holyoke 1 Walter 0
5 Holyoke 1 Barb 3
6 Orange 2 Carol 5
7 Otis 1 Kay 0
8 Pelham 2 Mike 4
9 Stanford 1 Sam 1
10 Suffield 2 Lisa 1 |
| Reading Data from SAS Data Sets |
data average_loss; [1] set weight; [2] Percent=round((AverageLoss * 100) / Week1); [3] run; [4]
Begin the DATA step and create a SAS data
set called AVERAGE_LOSS. | |
Read an observation from SAS data set WEIGHT. | |
Calculate a value for
variable Percent. | |
Execute the DATA step. |
| Generating Data from Programming Statements |
data investment; [1]
begin='01JAN1990'd;
end='31DEC2009'd;
do year=year(begin) to year(end); [2]
Capital+2000 + .07*(Capital+2000);
output; [3]
end;
put 'The number of DATA step iterations is '_n_; [4]
run; [5]
proc print data=investment; [6]
format Capital dollar12.2; [7]
run; [8]
Begin the DATA step and create a SAS data
set called INVESTMENT. | |
Calculate a value based on a $2,000 capital
investment and 7% interest each year from 1990 to 2009. Calculate variable
values for one observation per iteration of the DO loop. | |
Write each observation to data set
INVESTMENT. | |
Write a note to the SAS log proving that the
DATA step iterates only once. | |
Execute the DATA step. | |
To see your output, print the INVESTMENT
data
set with the PRINT procedure. | |
Use the FORMAT statement to write numeric
values with dollar signs, commas, and decimal points. | |
Execute the PRINT procedure. |
![]() Chapter Contents |
![]() Previous |
![]() Next |
![]() Top of Page |
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.