Easy Way to Import the Csv File in Sas

Importing Data into SAS

This tutorial will show you how to read data into SAS. It also covers how to import external data to SAS. It includes examples of importing most common formats such as CSV, Excel File and Text Files etc. After finishing this tutorial, you would be comfortable how to extract data into SAS.

import data sas
Import Data to SAS

I. Entering Data Directly in SAS Program
You can enter your lines of data directly in your SAS program by using a DATALINES statement.

Let's start out by clarifying the main keywords associated with the following program.

The keywords are as follows:
1. DATA -  The DATA step always begins with a DATA statement. The purpose of the DATA statement is to tell SAS that you are creating a new data set i.e. outdata.
2. INPUT -To define the variables used in data set.
3. Dollar sign ($) - To identify variable as character.

4. DATALINES - To indicate that lines following DATALINES statement a real data.
5. PROC PRINT - To print out the contents of data set in output window.
6. RUN - The DATA step ends with a RUN statement.

You can also use CARDS instead of DATALINES. Both means the same. There is no difference between these two keywords. See the program below -

DATA outdata;
INPUT age gender $ dept obs1 obs2 obs3;
   CARDS;
1 F 3 17 6 24
;
proc print;
run;

Reading Delimited Data

The default delimiter is blank. If you have a data file with other delimiters such as comma or tab you need to define the delimiter before defining the variables using INFILE andDLM = options.

Syntax : Infile 'file-description' dlm=','
  1. For tab delimiter, the syntax would be infile 'file-description' dlm='09'x
  2. For colon delimiter, the syntax would be infile 'file-description' dlm=':'

Importing External Data into SAS

Method I : PROC IMPORT

PROC IMPORT is a SAS procedure to import external files into SAS. It automates importing process. You don't need to specify variable type and variable length to import an external file. It supports various formats such as excel file, csv, txt etc.

1. Importing an Excel File into SAS
The main keywords used in the following program are :

1. OUT - To specify name of a data set that SAS creates. In the program below, outdata is the data set saved in work library (temporary library)

2. DBMS - To specify the type of data to import.

3. REPLACE - To overwrite an existing SAS data set.

4. SHEET - To import a specific sheet from an excel workbook

5. GETNAMES - To include variable names from the first row of data.


If you are using SAS University Edition
You need to find shared folder which is generally available as Folders > My Folders
In SAS University edition, file location would be
DATAFILE = "/folders/myfolders/sampledata.xls"

2. Importing a Tab-Delimited File into SAS
The program below is similar to the code of importing excel file. The only difference is DBMS = DLM and delimter = '09'x.

3. Importing a Comma-Delimited File with TXT extension

To get comma separated file with a txt extension into SAS, specify delimeter = ','

4. Importing a Comma-Delimited File with CSV extension

To get comma separated file into SAS, specify DBMS= CSV



5. Importing a Space-Delimited File

To extract a space delimited file, specify delimiter = '20'x



6. Importing a file containing multiple delimiter

If two or more delimiters, such as comma and tabs, quote them following delimiter = option



Method II : Get External File - INFILE

In SAS, there is one more method called INFILEto import an external file. It's a manual method of importing an external file as you need to specify variables and its types and length.

1. Reading a CSV File

INFILE statement - To specify path where data file is saved.

DSD - To set the default delimiter from a blank to comma.
FIRSTOBS=2 : To tell SAS that first row contains variable names and data values starts from second row.

2. Reading a TAB Delimited File

We can use DLM='09'x to tell SAS that we are going to import a tab delimited file. The TRUNCOVER statement tells SAS to assign the raw data value to the variable even if the value is shorter than expected by the INPUT statement.

data outdata;
infile 'c:\deepanshu\dummydata.txt' DSD dlm='09'x truncover;
input employee :$30. DOJ :mmddyy8. state :$20.;
run;

How to handle an external file :
Using a FILENAME statement to handle an external file.

FILENAME sample 'c:\deepanshu\sampledata.csv' ;
DATA outdata;
infile sample dsd;
INPUT age gender $ dept obs1 obs2 obs3;
run;

About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he has worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and Human Resource.

Next → ← Prev

dominquezbosion38.blogspot.com

Source: https://www.listendata.com/2013/04/sas-reading-importing-raw-data-into-sas.html

0 Response to "Easy Way to Import the Csv File in Sas"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel