Mike's SAS Tutorials Lesson 3
This video series is intended to help you learn how to program using SAS for your statistical needs. Lesson 3 introduces the concept of permanent or external data sets and how to import them into SAS. I provide basic methods of importing permanent data sets using the INFILE statement and the IMPORT procedure (PROC IMPORT) for non-SAS based data files. I also discuss libraries and the LIBNAME statement to import SAS data directly using the SET statement. Finally, I show how one can save a SAS data set from the data step using LIBNAMEs in the DATA step.
Helpful Notes:
1. The LIBNAME statement is used to point SAS towards a specific folder on your computer.
2. The INFILE statement "reads" data into SAS if it is of a certain format (usually comma, space, or tab delimited).
3. PROC IMPORT - imports data of any of several different file formats into SAS.
4. The SET statement imports data from a library into SAS at the DATA STEP.
5. The library name in a data step's data name "writes" data from SAS into your library folder using SAS's own file format system.
Today's Code:
data main;
input x y z;
cards;
1 2 3
7 8 9
;
run;
proc contents data=main;
run;
proc print data=main;
run;
/* TEMPLATED CODE: .txt file type, with or without delimiters */
data [appropriate data set name here]; infile "[your file location here, including .txt extension]" LRECL=[a logical length of your data to emcompass ENTIRE data] DLM=',';
input
[variable names here]
;
run;
data infile_main;
infile "C:\My SAS Files\main.txt";
input x y z;
run;
proc print data=infile_main;
run;
/* TEMPLATED CODE: Microsoft Excel (.xls) file type */
proc import out=[your data set name here]
datafile='[your file location here, including .xls extension]'
dbms=excel replace;
*Optional statements are below; sheet='[specify sheet to obtain]'; getnames=[yes/no - first row = variable names]; mixed=[yes/no - refers to data types, if num AND char varibles, use yes]; usedate=[yes/no - read date formatted data as date formatted SAS data]; scantime=[yes/no - read in time formatted data as long as variable is not date format];
run;
proc import out=imported_excel
datafile='C:\My SAS Files\main.xls'
dbms=excel replace;
*Optional statements are below; sheet='Sheet1'; getnames=yes;
run;
proc print data=imported_excel;
run;
libname home "C:\My SAS Files\";
data sas_format; set home.main;
run;
data home.sas_format; set infile_main;
run;
Thanks so much Mike.
I have a SAS midterm in a few hours and watched all three videos so far for an excellent review of what we've been doing in class. Just subscribed, looking forward to upcoming vids.
TheGoltra 4 months ago in playlist More videos from mbate001
@TheGoltra Good luck on your exams!
mbate001 4 months ago
Does somebody has an idea what could be the problem? When importing Excel file, I get an error message: "ERROR: DBMS TYPE EXCEL NOT VALID FOR IMPORT"
Could this be because of my "not so official" excel program?
Cedricvanrusselt 4 months ago
@Cedricvanrusselt Did you make sure that the excel file you're trying to import is saved as a .xls and not a .xlsx? If you're using Microsoft Office 2007, Excel saves worksheets under the default .xlsx which does not work with proc import's excel method, so far as I know. You can adjust the Excel file type if you use File > Save As, then select "Excel 97-2003 Workbook" and continue the import as normal.
mbate001 4 months ago