How to Read raw Data in SAS

Last updated on Sep 13 2022
Nitin Pawar

Table of Contents

How to Read raw Data in SAS

SAS can read data from various sources which includes many file formats. The file formats used in SAS environment is discussed below.

  • ASCII(Text) Data Set
  • Delimited Data
  • Excel Data
  • Hierarchical Data

Reading ASCII(Text) Data Set

These are the files which contain the data on text format. The data is usually delimited by a space, but there can be different types of delimiters also which SAS can handle. Let’s consider an ASCII file containing the employee data. We read this file using the Infile statement available in SAS.

Example

In the below example we read the data file named emp_data.txt from the local environment.

data TEMP;
infile
'/folders/myfolders/sasuser.v94/Tecklearn/emp_data.txt';
input empID empName $ Salary Dept $ DOJ date9. ;
format DOJ date9.;
run;
PROC PRINT DATA = TEMP;
RUN;

When the above code is executed, we get the following output.

sas 53

Reading Delimited Data

These are the data files in which the column values are separated by a delimiting character like a comma or pipeline etc. In this case we use the dlm option in the infile statement.

Example

In the below example we read the data file named emp.csv from the local environment.

data TEMP;
infile
'/folders/myfolders/sasuser.v94/Tecklearn/emp.csv' dlm=",";
input empID empName $ Salary Dept $ DOJ date9. ;
format DOJ date9.;
run;
PROC PRINT DATA = TEMP;
RUN;

When the above code is executed, we get the following output.

sas 54

Reading Excel Data

SAS can directly read an excel file using the import facility. As seen in the chapter SAS data sets, it can handle a wide variety of file types including MS excel. Assuming the file emp.xls is available locally in the SAS environment.

Example

FILENAME REFFILE
"/folders/myfolders/Tecklearn/emp.xls"
TERMSTR = CR;


PROC IMPORT DATAFILE = REFFILE
DBMS = XLS
OUT = WORK.IMPORT;
GETNAMES = YES;
RUN;
PROC PRINT DATA = WORK.IMPORT RUN;

The above code reads the data from excel file and gives the same output as above two file types.

Reading Hierarchical Files

In these files the data is present in hierarchical format. For a given observation there is a header record below which many detail records are mentioned. The number of details records can vary from one observation to another. Below is an illustration of a hierarchical file.

In the below file the details of each employee under each department is listed. The first record is the header record mentioning the department and the next record few records starting with DTLS are the details record.

DEPT:IT
DTLS:1:Rick:623
DTLS:3:Mike:611
DTLS:6:Tusar:578
DEPT:OPS
DTLS:7:Pranab:632
DTLS:2:Dan:452
DEPT:HR
DTLS:4:Ryan:487
DTLS:2:Siyona:452

Example

To read the hierarchical file we use the below code in which we identify the header record with an IF clause and use a do loop to process the details record.

data employees(drop = Type);
length Type $ 3  Department
empID $ 3 empName $ 10 Empsal 3 ;
retain Department;
infile
'/folders/myfolders/Tecklearn/empdtls.txt' dlm = ':';
input Type $ @;
if Type = 'DEP' then
input Department $;
else do;
input empID  empName $ Empsal ;
output;
end;
run;
PROC PRINT DATA = employees;
RUN;

When the above code is executed, we get the following output.

sas 55

So, this brings us to the end of blog. This Tecklearn ‘How to Read raw Data in SAS’ blog helps you with commonly asked questions if you are looking out for a job in SAS. If you wish to learn SAS and build a career in Data Analytics domain, then check out our interactive, SAS Training for SAS BASE Certification Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/sas-training-for-sas-base-certification/

SAS Training for SAS BASE Certification Training

About the Course

SAS Certification Training is intended to make you an expert in SAS programming and Analytics. You will be able to analyse and write SAS code for real problems, learn to use SAS to work with datasets, perform advanced statistical techniques to obtain optimized results with Advanced SAS programming.  In this SAS online training course, you will also learn SAS macros, Machine Learning, PROC SQL, procedure, statistical analysis and decision trees. You will also work on real-life projects and prepare for the SAS Certified Base Programmer certification exam. Upon the completion of this SAS online training, you will have enough proficiency in reading spreadsheets, databases, using SAS functions for manipulating this data and debugging it.

Why Should you take SAS Training?

  • The average salary for a Business Intelligence Developer skilled in SAS is $100k (PayScale salary data)
  • SAS, Google, Facebook, Twitter, Netflix, Accenture & other MNCs worldwide are using SAS for their Data analysis activities and advance their existing systems.
  • SAS is a Leader in 2017 Gartner Magic Quadrant for Data Science Platform.

What you will Learn in this Course?

Introduction to SAS 

  • Introduction to SAS
  • Installation of SAS
  • SAS windows
  • Working with data sets
  • Walk through of SAS windows like output, search, editor etc

SAS Enterprise Guide

  • How to read and subset the data sets
  • SET Statement
  • Infile and Infile Options
  • SAS Format -Format Vs Informat

SAS Operators and Functions

  • Using Variables
  • Defining and using KEEP and DROP statements
  • Output Statement
  • Retain Statement
  • SUM Statement

Advanced SAS Procedures

  • PROC Import
  • PROC Print
  • Data Step Vs Proc
  • Deep Dive into Proc

Customizing Datasets

  • SAS Arrays
  • Useful SAS Functions
  • PUT/INPUT Functions
  • Date/Time Functions
  • Numeric Functions
  • Character Functions

SAS Format and SAS Graphs

  • SAS Format statements
  • Understanding PROC GCHART, various graphs, bar charts: pie, bar

Sorting Techniques

  • NODUP
  • NODUKEY
  • NODUP Vs NODUKEY

Data Transformation Function

  • Character functions, numeric functions and converting variable type
  • Use functions in data transformation

Deep Dive into SAS Procedures, Functions and Statements

  • Find Function
  • Scan Function
  • MERGE Statement
  • BY Statement
  • Joins
  • Procedures Vs Function
  • Where Vs If
  • What is Missover
  • NMISS
  • CMISS

PROC SQL

  • SELECT statement
  • Sorting of Data
  • CASE expression
  • Other SELECT statement clauses
  • JOINS and UNIONS

Using SAS Macros

  • Benefits of SAS Macros
  • Macro Variables
  • Macro Code Constituents and Macro Step
  • Positional Parameters to Macros

Got a question for us? Please mention it in the comments section and we will get back to you.

 

0 responses on "How to Read raw Data in SAS"

Leave a Message

Your email address will not be published. Required fields are marked *