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: pub@towardsai.net
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 VeloxTrend Ultrarix Capital Partners 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

Our 15 AI experts built the most comprehensive, practical, 90+ lesson courses to master AI Engineering - we have pathways for any experience at Towards AI Academy. Cohorts still open - use COHORT10 for 10% off.

Publication

SAS Python Interaction
Latest

SAS Python Interaction

Last Updated on April 14, 2021 by Editorial Team

Author(s): Vivek Chaudhary

Programming

SAS Python Interaction
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


Take our 90+ 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!

Towards AI has published Building LLMs for Production—our 470+ page guide to mastering LLMs with practical projects and expert insights!


Discover Your Dream AI Career at Towards AI Jobs

Towards AI has built a jobs board tailored specifically to Machine Learning and Data Science Jobs and Skills. Our software searches for live AI jobs each hour, labels and categorises them and makes them easily searchable. Explore over 40,000 live jobs today with Towards AI Jobs!

Note: Content contains the views of the contributing authors and not Towards AI.