MET MA 603: SAS Programming and Applications
MET MA 603:
SAS Programming and Applications
Entering Data Directly
1
1
Entering Data Directly
Entering data directly is to manually type in or copy/paste each variable name and observation.
This is often the fastest and easiest method when the amount of data that needs to be entered is small. However, it isn’t practical for larger datasets.
Use this method for small datasets, such as for examples and testing.
There are three ways of entering data directly:
Viewtable Data Entry
Data Step Statements
Datalines / Cards Statement
2
2
Viewtable Data Entry
To enter data with the Viewtable method, first open the Viewtable window via Tool > Table Editor. This will open an empty table.
Right-click the column headers and choose Column Attributes. Change the name of the variable and specify the data type and format.
Enter data into the empty cells.
Click File > Save As to save the table. Choose a name for the dataset and specify the library.
3
3
Data Step Statements
The Data step studied in previous lessons is also an example of direct data entry. A statement introduces each variable in the dataset and inputs the value of one observation.
data distance;
Miles = 26.22;
Kilometers = 1.61 * Miles;
run;
This method is restricted (thus far) to only having one observation for each variable, making it useful only in certain situations. Later in the course, Data Steps will usually involve creating and working with datasets where there are more than one observation.
4
4
Datalines / Cards Statement
This method expands on the previous one, allowing for multiple observations to be entered for each variable.
The Input statement tells SAS the names and data types of each variable in the dataset. The dollar sign ($) is used to assign the Character data type to a variable. If the dollar sign is left out the data type for the variable will be Numeric.
The Datalines statement tells SAS that data will be entered, and the semicolon indicates the end of the entered data. All lines in between are written into the dataset.
The keyword Cards has the same function as Datalines. Using either one will produce the same result.
5
5
data city_populations;
input City $ State $ Population;
Datalines;
Boston MA 610000
Chicago IL 2410000
Seattle WA 415000
;
run;
The basic version of this method uses new lines to separate observations and a delimiter to separate variables. The default delimiter is a single space.
Note that the semicolon after the data must be on a new line, and the Datalines or Cards statement must be the last statement in the Data Step (with the exception of “Run”)
Example of Datalines / Cards
6
6
The basic version of the Datalines / Cards method has some limitations. The default length of data is restricted to 8, and data cannot contain embedded spaces.
data city_populations;
input City $ State $ Population;
Datalines;
Boston MA 610000
Chicago IL 2410000
Seattle WA 415000
New York NY 8550405
Philadelphia PA 1567442
;
run;
Limitations of Datalines / Cards
7
7
The default length for new variables is 8. The Length statement is used to specify different lengths. The Length statement must precede the Input statement so that the size isn’t already set before the Length statements is read.
Use the ampersand (&) to tell SAS the data has embedded spaces. Use 2 or more spaces to indicate the next item.
data city_populations;
length City $12 State $2;
input City & State Population;
Datalines;
Boston MA 610000
Chicago IL 2410000
Seattle WA 415000
New York NY 8550405
Philadelphia PA 1567442
;
run;
Modifications to Datalines / Cards
8
8
By default, variables are read assuming there is a delimiter. To read in fixed-width structured data, the columns of each variable must be specified.
Unless otherwise specified, the lengths of the variables are determined by the difference between the starting and ending point of the columns containing the data.
data city_populations;
input City $1-12 State $14-15 Population 18-24;
Datalines;
Boston MA 0610000
Chicago IL 2410000
Seattle WA 0415000
New York NY 8550405
Philadelphia PA 1567442
;
run;
Datalines / Cards for Fixed-Width Data
9
9
Practice
Copy the data below into SAS and use the Datalines method to create a SAS dataset.
Country Capital
Argentina Buenos Aires
Bolivia Sucre
Brazil Brasilia
Chile Santiago
Colombia Bogotá
Ecuador Quito
Guyana Georgetown
Paraguay Asunción
Peru Lima
Suriname Paramaribo
Uruguay Montevideo
Venezuela Caracas
10
10
Practice
Copy the data below into SAS and use the Datalines method to create a SAS dataset.
Country Capital
ArgentinaBuenos Aires
Bolivia Sucre
Brazil Brasilia
Chile Santiago
Colombia Bogotá
Ecuador Quito
Guyana Georgetown
Paraguay Asunción
Peru Lima
Suriname Paramaribo
Uruguay Montevideo
VenezuelaCaracas
11
11
Readings
Textbook sections 2.1, 2.2, 2.4
“Representing Tabular Data”
http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/viewer.htm#a001360509.htm
12
12
/docProps/thumbnail.jpeg