Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The SAS reads the original data


May 27, 2021 SAS


Table of contents


SAS can read data from a variety of sources, including many file formats. T he file format used in the SAS environment is described below.

  • ASCII (text) dataset
  • Separate the data
  • Excel data
  • Tiered data

Read the ASCII (text) dataset

These are files that contain text formatting data. D ata is usually separated by spaces, but there can also be different types of separators that SAS can handle. L et's consider an ASCII file that contains employee data. W e read this file using the Infile statement provided in SAS.

Cases

In the following example, we read a data file named emp_data.txt local environment.

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

When executing the above code, we can get the following output.

The SAS reads the original data

Read the separated data

These are data files where column values are separated by bounding characters, such as commas or pipes. I n this case, we use the dlm option in the infile statement.

Cases

In the following example, we read a data file called emp .csv local environment.

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

When executing the above code, we can get the following output.

The SAS reads the original data

Read Excel data

SAS can use the import tool to read excel files directly. A s shown in the chapter SAS data set, it can handle a wide variety of file types, including MS excel. A ssume that the file emp .xls available locally in the SAS environment.

Cases

FILENAME REFFILE
"/folders/myfolders/TutorialsPoint/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 the excel file and gives the same output as the two file types above.

Read the hierarchical file

In these files, the data exists in a hierarchical format. F or a given observation, there is a header record, with many detailed records mentioned below it. T he number of detailed records can be observed from one observation to another. Below is an illustration of a hierarchical file.
In the following file, the details of each employee under each department are listed. The first record refers to the department's head record, and the next record starts with DTLS and several records are detailed records.

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

Cases

To read a hierarchical file, we use the following code, where we use the IF clause to identify header records and use do loops to process detail records.

data employees(drop=Type);
   length Type $ 3  Department
          empID $ 3 empName $ 10 Empsal 3 ;
   retain Department;
   infile 
   '/folders/myfolders/TutorialsPoint/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 executing the above code, we can get the following output.

The SAS reads the original data