Loading data into SAS from various sources.
************************************************************************
Load data from a text file.
Using Notepad create the data file C:\MyWork\htwt1.txt
1 M 23 68 155
2 F . 61 102
3 M 55 70 202
Run the following SAS program to load and print the data. Note that the PROC EXPORT command writes your data file to a new file C:\ MyWork\MyData.txt.
* Load and Print data;
DATA HTWT;
INFILE 'C:\MYWORK\HTWT1.TXT';
INPUT ID GENDER $ AGE HEIGHT WEIGHT;
RUN;
PROC PRINT DATA=HTWT;
TITLE 'PRINT DATA';
RUN;
PROC SORT DATA=HTWT;
BY AGE DECENDING GENDER;
RUN;
PROC PRINT DATA=HTWT;
TITLE 'PRINT DATA';
RUN;
PROC EXPORT DATA=HTWT
OUTFILE='C:\MYWORK\MYDATA.TXT' REPLACE;
RUN;
************************************************************************
Loading data from a *.csv data file.
Using Notepad create the data file C:\MyWork\htwt2.csv
1,M,23,68,155
2,F,.,61,102
3,M,55,70,202
Run the following SAS program to load and print the data. Note that the DLM= option tells SAS what the delimiter is.
* Load and Print data;
DATA HTWT;
INFILE 'C:\MYWORK\HTWT2.CSV' DLM=',';
INPUT ID GENDER $ AGE HEIGHT WEIGHT;
RUN;
PROC PRINT DATA=HTWT;
TITLE 'PRINT DATA';
RUN;
************************************************************************
Loading data from a *.csv data file using PROC IMPORT.
Using Notepad create the data file C:\MyWork\htwt3.csv
ID,GENDER,AGE,HEIGHT,WEIGHT
1,M,23,68,155
2,F,.,61,102
3,M,55,70,202
Run the following SAS program to load and print the data.
* Print data;
PROC IMPORT DATAFILE =
'C:\MYWORK\HTWT3.CSV' OUT=HTWT REPLACE;
PROC PRINT DATA=HTWT;
TITLE 'PRINT DATA';
RUN;
************************************************************************
Loading data from a *.xls data file unsing PROC IMPORT.
Using EXCEL create the data file C:\MyWork\htwt4.xls
ID |
GENDER |
AGE |
HEIGHT |
WEIGHT |
1 |
M |
23 |
68 |
155 |
2 |
F |
. |
61 |
102 |
3 |
M |
55 |
70 |
202 |
Run the following SAS program to load and print the data.
* Print data;
PROC IMPORT DATAFILE =
'C:\MYWORK\HTWT4.XLS' OUT=HTWT REPLACE;
PROC PRINT DATA=HTWT;
TITLE 'PRINT DATA';
RUN;