Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Take our 85+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

Publication

SAS Python Interaction
Latest

SAS Python Interaction

Last Updated on April 14, 2021 by Editorial Team

Author(s): Vivek Chaudhary

Programming

SAS Py

The objective of this article is to understand Python 3.x interaction with SAS 9.4 university edition. Read SAS datasets using python pandas library and manipulate datasets and write the result back toΒ SAS.

SAS University Edition is free SAS software that can be used for teaching and learning statistics and quantitative methods. The scope of this article is however limited to ETL operations with SAS andΒ Python.

#Note: Assuming SAS University edition is installed.

  1. SAS Library andΒ Datasets

A SAS library is a collection of one or more SAS files/datasets that are recognized by SAS and that are referenced and stored as a unit. Whenever a new session is created SAS automatically creates two libraries Work, temporary library, and SASUSER, permanent library.

A SAS dataset is a table with columns and rows. In SAS, the table is called a data set, a column is called a variable, and a row is called an observation. In each observation, each variable has a specificΒ value.

In simple terms or DB terms, a SAS library can be construed as a schema whereas a SAS dataset can be understood as a DB table with a matrix structure having rows andΒ columns.

2. Create Library andΒ Dataset

SAS command to create aΒ library:

libname mylib β€˜<path>’;

SAS command to create aΒ dataset:

DATA mylib.emp;
infile β€˜<path>/emp.csv’
dlm=’,’
FIRSTOBS=2 DSD;
input EMPNO ENAME $ SAL DEPTNO COMM;
run;
Emp dataset
DATA mylib.dept;
infile β€˜<path>/dept.csv’
dlm=’,’
FIRSTOBS=2 DSD;
input DEPTNO DNAME $ LOC $;
run;
Dept dataset

Datasets in SAS are stored on the disk with SAS data format: sas7bdat.

3. Read SAS dataset usingΒ Python

import pandas as pd
emp_df= pd.read_sas(r’D:\VirtualMs\SAS University Edition\myfolders\emp.sas7bdat’,encoding=’utf-8')
emp_df.head(10)
emp dataset
dept_df= pd.read_sas(r’D:\VirtualMs\SAS University Edition\myfolders\dept.sas7bdat’,encoding=’utf-8')
dept_df.head(10)
dept dataset

Data Manipulation step, apply an equijoin to merge emp and dept datasets.

final_df=pd.merge(emp_df,dept_df[[β€˜DEPTNO’,’DNAME’,’LOC’]],on=’DEPTNO’,how=’inner’)
final_df.head(10)
merged data

Write Pandas dataframe toΒ disk:

final_df.to_csv(β€˜D:\VirtualMs\SAS University Edition\myfolders\emppy.csv’,index=False)

4. Create a SAS Table from the CSVΒ file

SAS program to create a table from the CSVΒ file:

DATA mylib.emp_py;
infile β€˜/folders/myfolders/emppy.csv’
dlm=’,’
FIRSTOBS=2 DSD;
input EMPNO ENAME $ SAL DEPTNO COMM DNAME $ LOC $;
run;
SAS tableΒ data

Explanation of the aboveΒ program:

Data Step: This step involves loading the required data set into SAS memory and identifying the variables (also called columns) of the data set. It also captures the records also called observations.

Infile: specify the input file path and name along with delimiter dlm, in our case β€˜,’ due to the CSVΒ file.

FIRSTOBS: specify the line to start reading from, 2 means skip header and read from the first observation, which is the actualΒ header.

To summarize, we have successfully read datasets from SAS using the Python Pandas library and written back the dataset to the disk, and then created a table out of the same dataset. That’s all for thisΒ blog.

Thank you for supporting theΒ content.


SAS Python Interaction was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.

Published via Towards AI

Feedback ↓