68
SAS ® Viya ® Programming: Getting Started 2020.1 - 2020.1.3* * This document might apply to additional versions of the software. Open this document in SAS Help Center and click on the version in the banner to see all available versions. SAS ® Documentation February 8, 2021

SAS Viya Programming: Getting Started

  • Upload
    others

  • View
    14

  • Download
    0

Embed Size (px)

Citation preview

Page 1: SAS Viya Programming: Getting Started

SAS® Viya® Programming: Getting Started2020.1 - 2020.1.3*

* This document might apply to additional versions of the software. Open this document in SAS Help Center and click on the version in the banner to see all available versions.

SAS® DocumentationFebruary 8, 2021

Page 2: SAS Viya Programming: Getting Started

The correct bibliographic citation for this manual is as follows: SAS Institute Inc. 2020. SAS® Viya® Programming: Getting Started. Cary, NC: SAS Institute Inc.

SAS® Viya® Programming: Getting Started

Copyright © 2020, SAS Institute Inc., Cary, NC, USA

All Rights Reserved. Produced in the United States of America.

For a hard copy book: No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, or otherwise, without the prior written permission of the publisher, SAS Institute Inc.

For a web download or e-book: Your use of this publication shall be governed by the terms established by the vendor at the time you acquire this publication.

The scanning, uploading, and distribution of this book via the Internet or any other means without the permission of the publisher is illegal and punishable by law. Please purchase only authorized electronic editions and do not participate in or encourage electronic piracy of copyrighted materials. Your support of others' rights is appreciated.

U.S. Government License Rights; Restricted Rights: The Software and its documentation is commercial computer software developed at private expense and is provided with RESTRICTED RIGHTS to the United States Government. Use, duplication, or disclosure of the Software by the United States Government is subject to the license terms of this Agreement pursuant to, as applicable, FAR 12.212, DFAR 227.7202-1(a), DFAR 227.7202-3(a), and DFAR 227.7202-4, and, to the extent required under U.S. federal law, the minimum restricted rights as set out in FAR 52.227-19 (DEC 2007). If FAR 52.227-19 is applicable, this provision serves as notice under clause (c) thereof and no other notice is required to be affixed to the Software or documentation. The Government’s rights in Software and documentation shall be only those set forth in this Agreement.

SAS Institute Inc., SAS Campus Drive, Cary, NC 27513-2414

February 2021

SAS® and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration.

Other brand and product names are trademarks of their respective companies.

v_001-P1:pgmgs

Page 3: SAS Viya Programming: Getting Started

Contents

About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v

Chapter 1 / Before You Begin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1SAS Viya Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1SAS Viya Programming Interface: SAS Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3Other SAS Viya Programming Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3SAS Viya Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4Processing Code in CAS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9CAS Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Learn More . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

Chapter 2 / About the Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Business Case Scenario . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15About the Variables in the Data Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

Chapter 3 / Accessing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19Starting a CAS Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19Adding a Caslib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20Ways to Access Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20Loading a CSV File by Using the CASUTIL Procedure . . . . . . . . . . . . . . . . . . . . . . . . 21Downloading and Loading a File to the CAS Server . . . . . . . . . . . . . . . . . . . . . . . . . . 22

Chapter 4 / Exploring Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25Tasks Performed by CAS Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25Retrieving Table Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26View Data from a CAS Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27Displaying Column Information of a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

Chapter 5 / Preparing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Tasks Performed by CAS Actions and the DATA Step . . . . . . . . . . . . . . . . . . . . . . . . 31Applying Labels and Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32Creating Variables and Filtering Data by Using the DATA Step . . . . . . . . . . . . . . . . . 33

Chapter 6 / Analyzing and Reporting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Tasks Performed by CAS Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Summarizing Multiple Numeric Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38Creating a Group-By Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39Creating a Gradient Map of the United States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42Creating a Frequency Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

Chapter 7 / Exporting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47Tasks Performed by Statements and Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47Creating ODS EXCEL Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48Creating PDF Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50Exporting to a CSV File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

Page 4: SAS Viya Programming: Getting Started

Appendix 1 / Learn More . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53SAS Viya Files Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56DATA Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56Macro Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57DS2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58FedSQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59User-Defined Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

iv Contents

Page 5: SAS Viya Programming: Getting Started

About This Book

Intended AudienceThis book is intended for new or beginner-level SAS users who are new to programming in SAS Viya. The getting started tasks cover accessing, exploring, preparing, analyzing, reporting, and exporting data on the CAS server.

Prerequisitesn access to SAS Studio

n access to SAS Cloud Analytic Services (CAS)

n For a basic understanding of SAS Viya concepts, see Chapter 1, “Before You Begin,” on page 1.

v

Page 6: SAS Viya Programming: Getting Started

vi About This Book

Page 7: SAS Viya Programming: Getting Started

1Before You Begin

SAS Viya Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

SAS Viya Programming Interface: SAS Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Other SAS Viya Programming Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

SAS Viya Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4An Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4SAS Cloud Analytic Services (CAS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5SAS Compute Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

Processing Code in CAS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

CAS Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Caslib Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Casuser Caslib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12Assigning a Library Reference to Caslibs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12Predefined Caslibs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

Learn More . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

SAS Viya OverviewSAS Viya is the latest enhancement of the SAS Platform.

n SAS Viya is cloud enabled and allows scalable, web-based access for your particular data processing needs.

n SAS Viya includes an in-memory engine and parallel processing capabilities for enhanced execution speed.

n SAS Viya enables you to use familiar SAS code that you use to write in SAS®9. However, with a few modifications, your code can take advantage of SAS Viya in-memory processing for accelerated performance.

1

Page 8: SAS Viya Programming: Getting Started

n SAS Viya provides integration with open-source tools such as Python, R, and other open-source languages.

Figure 1.1 SAS Viya Data

In SAS Viya, you can access a variety of data sources in addition to traditional SAS tables. SAS Viya can directly access familiar file formats such as XML, JSON, CSV, or XLSX. With data connectors, you can process data from relational databases, Hadoop, and other data stores (for example, structured tables and semi-structured and unstructured text files). You can easily access data that is stored on premises or in the cloud.

Figure 1.2 SAS Viya Applications

2 Chapter 1 / Before You Begin

Page 9: SAS Viya Programming: Getting Started

SAS Viya includes several services and applications.

n SAS Drive for organizing and accessing all SAS content

n SAS Visual Analytics for web-based reporting and dashboards

n SAS Visual Data Mining and Machine Learning

n SAS Studio

SAS Viya Programming Interface: SAS Studio

SAS Studio is a web-based application that includes a variety of helpful features for developing SAS code. In addition to a modern Program Editor for writing code and viewing the log and results, you can also access files on the local machine or SAS servers, take advantage of stored code for common actions, and generate code with point-and-click tasks. SAS Studio 5.2 is specifically designed for use in SAS Viya.

See Alson Getting Started with Programming in SAS Studio

n SAS Studio: User’s Guide

Other SAS Viya Programming InterfacesAlthough SAS Studio is the default programming interface for SAS Viya, it is not your only option for developing and submitting code. You can use open-source applications such as Jupyter Notebook or R Studio. Or if your SAS Platform includes SAS®9 maintenance release 5 or later, you can use SAS®9 applications, which include SAS Studio, SAS Enterprise Guide, or the SAS windowing environment.

See Alson Getting Started with SAS Viya for Java

n Getting Started with SAS Viya for Lua

n Getting Started with SAS Viya for Python

Other SAS Viya Programming Interfaces 3

Page 10: SAS Viya Programming: Getting Started

n Getting Started with SAS Viya for R

SAS Viya Servers

An OverviewSAS Viya includes multiple servers to execute SAS code. The two primary servers are the SAS Compute Server and SAS Cloud Analytic Services, or CAS.

Figure 1.3 SAS Viya Servers

You can think of the SAS Compute Server as equivalent to the SAS®9 workspace server. Your familiar SAS®9 programs can be submitted as is to SAS Viya. By default, the programs run on the Compute Server. There is no need to learn new syntax to use the Compute Server.

SAS Cloud Analytic Services (CAS) is the high-performance server that allows for parallel processing on in-memory data. When your data is large or the computations complex, running your programs in CAS can significantly boost performance. Often, only minor code modifications are required for programs to run in CAS.

4 Chapter 1 / Before You Begin

Page 11: SAS Viya Programming: Getting Started

SAS Cloud Analytic Services (CAS)

Programming in CASThere are different ways to submit requests to CAS for in-memory parallel processing. You can use PROC and DATA steps that are CAS enabled, or you can learn the native CAS language, which is called CASL.

Figure 1.4 CASL and CAS Actions

CASL is a scripting language that is used to call CAS actions. CAS actions perform specific tasks such as loading a table. Many other actions are available to transform data, perform analytics, and create output. Actions that perform similar tasks are grouped into action sets. Your CASL code runs in CAS.

In SAS, you submit CAS actions by using PROC CAS and CASL statements. You can also call CAS actions by using open-source languages, which include Python, Lua, Java, and REST APIs.

It is not necessary to learn CASL to process data in CAS. Some Base SAS procedures and DATA step features are CAS enabled, which means that SAS translates compliant code into CAS actions behind the scenes. The luxury of using CAS-enabled statements and steps is that you do not need to learn a new language; instead you learn the requirements to ensure that your code is CAS compliant. DATA steps and PROC steps that are not CAS enabled automatically run on the SAS Compute Server.

SAS Viya Servers 5

Page 12: SAS Viya Programming: Getting Started

Parallel Processing in CASFigure 1.5 Parallel Processing: Program Submitted

When a program is submitted to CAS, it first goes to the CAS controller. The controller distributes the data to separate worker nodes. The nodes perform coordinated parallel processing on their portion of the data. This means that the nodes are executing the same action at the same time.

Figure 1.6 Parallel Processing: Results Returned

As worker nodes complete processing, they return results to the controller in the order in which they finish. Multiple nodes perform the work much faster than processing the job on one machine.

CAS can be configured to run on a single machine, taking advantage of multiple CPUs or threads to speed up processing. CAS can also be configured to run on multiple machines, with one controller node and several worker nodes. Regardless of the configuration, the code that you write is the same, and parallel processing in CAS provides speedy results.

Performance and StrengthsCAS and the Compute Server each has its unique strengths. The size of your file and the nature of your code determine which steps to use to exploit those strengths. It might be most efficient for your code to execute with a combination of the two servers.

6 Chapter 1 / Before You Begin

Page 13: SAS Viya Programming: Getting Started

There is a certain amount of overhead processing that occurs when code runs in CAS. Services must start up. If the data is not already in-memory, it must be explicitly loaded into memory. Next, the code that is required to process the data is distributed by the controller node to the workers. The worker nodes then execute the code and return information to the controller node to combine and deliver the final results.

For large data and complex processes, this overhead might be negligible compared to the accelerated execution time in CAS. For smaller tables and processes, it might be more efficient for the code to run on the Compute Server. To help understand when CAS might be the best option for processing, we will discuss its primary strengths.

All of the Foundation procedures that you are familiar with still run on the SAS Compute Server. Some of the Foundation procedures have even been updated in SAS Viya to execute either on the Compute Server or in CAS. However, the CAS-specific procedures have been optimized for parallel, in-memory processing.

Figure 1.7 CAS Integration with Open-Source Languages

CAS integrates with common open-source languages. CAS actions can be called directly from Java, Lua, Python, R, or REST APIs. Integrating SAS and open-source technologies is often helpful for two main reasons: programmatically accessing SAS Viya using open-source software, or bringing open-source models into SAS Viya.

Best PracticesCAS generally outperforms the Compute Server in these instances:

n data sources that are larger than 50 GB

n steps that run for 30 minutes or longer

n procedures that are demanding computationally

n DATA steps with extremely long or complex logic

Benchmarking is required for production jobs where performance is critical.

SAS Viya Servers 7

Page 14: SAS Viya Programming: Getting Started

SAS Compute ServerSAS Viya provides SAS Compute Server as the programming run-time server for processing data that is not performed on the CAS server.

The SAS Compute Server uses the SAS Viya Compute service to enable clients to submit SAS programs in the form of jobs for processing. For every job that is processed, the SAS Compute Server writes a logging message to the SAS log. If the job produces ODS results, output data sets, files, and so on, the output is associated with the job.

n The DATA step can run on the SAS Compute Server or on the CAS server. The DATA step determines whether it is possible to run in CAS based on what LIBREFS, statements, and features are included in the DATA step code. If the DATA step cannot run on the CAS server, the DATA step runs on the SAS Compute Server. For more information, see Running the Data Step in CAS.

n The data management and utility procedures and macros run on the SAS Compute Server. Many SAS procedures run on the CAS server. For more information, see SAS Viya Procedures. SAS procedures that do not run on the CAS server can transfer data from in-memory tables from the CAS server to the compute server for processing.

n FedSQL and DS2 run on the SAS Compute Server when they access SAS libraries. They run on the CAS server when instructed by the SESSREF= or SESSUID= procedure options.

n The SAS Compute Server supports SAS catalogs.

n High-performance SAS Viya procedures and other supporting procedures use a CAS LIBNAME engine libref to identify in-memory tables and then use CAS actions to perform data analysis and processing on the CAS server. The SAS Compute Server does not perform data analysis for high-performance SAS Viya procedures.

8 Chapter 1 / Before You Begin

Page 15: SAS Viya Programming: Getting Started

Processing Code in CASFigure 1.8 Processing Code in CAS Workflow

This documentation uses the following recommended steps to execute SAS code in CAS:

1 Create a CAS session.

2 Create a caslib to access your data.

3 Load data into in-memory tables.

4 Use CAS where appropriate to improve processing speed.

5 Save and drop in-memory tables.

6 End your CAS session.

Processing Code in CAS 9

Page 16: SAS Viya Programming: Getting Started

Figure 1.9 SAS Viya Processing Environments

SAS Viya programs and jobs that are not run on the CAS server run on the SAS Compute Server.

CAS writes a note to the log to indicate where the program or job ran:

Example Code 1.1 Log

NOTE: Could not execute DATA step code in Cloud Analytic Services. Running DATA step in the SAS client.NOTE: Running DATA step in Cloud Analytic Services.

CAS LibrariesBefore you begin on the examples in this documentation, understanding caslibs is important.

DefinitionA CAS library, or caslib, is a container with two main areas: an area for in-memory tables and another area for data source files. Data source files can be SAS, CSV, Hadoop, Microsoft Excel, and a variety of databases.

10 Chapter 1 / Before You Begin

Page 17: SAS Viya Programming: Getting Started

Caslib AttributesEach caslib is defined by several attributes that establish the connection to a data source and files and that define user access.

Figure 1.10 Caslib Scopes

The LOCAL attribute of a caslib indicates the scope of the caslib. Caslibs can have either session or global scope. When LOCAL=YES is set, the caslib is session scope. A session-scope caslib is available only to the CAS session where it was created. When the CAS session ends, the session-scope caslib is deleted. Session-scope caslibs are useful when you do not need to share data across sessions.

TIP The caslibs that you create while running the examples in this documentation are session-scope caslibs.

When LOCAL=NO, the caslib is global scope. A global-scope caslib is available whenever and however you connect to a CAS session. A global-scope caslib is accessible across users and across applications. For example, if you later reconnect to CAS from SAS Visual Analytics or from Jupyter Notebook, the global caslib is available for immediate use.

The ACTIVE attribute indicates the default caslib. The default caslib is ACTIVE=YES, which means that it is not required to specify the caslib in code when referencing a data source. Caslibs with ACTIVE=NO can also be used in code. However, the caslib name must be specified along with the data source.

CAS Libraries 11

Page 18: SAS Viya Programming: Getting Started

Figure 1.11 Caslib Attribute: Personal

When the PERSONAL attribute is YES for a caslib, it is available only to you. Think of this as your personal place to work with data in CAS. When you start a session, your personal caslib is added by default. Initially, your personal caslib is the active caslib if your server is configured with personal caslibs. If not, the first defined global caslib is the active caslib, which enables you to access files and in-memory tables from any session that you start. However, because the caslib is personal, only your user ID can access the data. Your personal caslib is typically Casuser.

Casuser CaslibWhen you connect to a CAS session, your personal global-scope caslib named Casuser is available. Unless you specify a different active caslib with the CASLIB= option, Casuser is the default active caslib. If your program references an in-memory table or data source file without specifying the associated caslib, the active caslib is assumed. A CAS session has only one active caslib, but you can easily specify another caslib when creating or accessing data.

Assigning a Library Reference to CaslibsAlthough Casuser is automatically available as the default caslib when your CAS session begins, you cannot use it in place of a library reference in a SAS program. To access in-memory tables in your code, you must first assign a libref to your caslib. Here is the syntax for the LIBNAME statement:

LIBNAME libref CAS <CASLIB= caslib-name>;

Note: Libref names are limited to 8 characters.

You can assign a library reference to your caslib in two ways. First, you can use the LIBNAME statement to create a library reference that points to your caslib. CAS is the required engine. Here are two examples:

12 Chapter 1 / Before You Begin

Page 19: SAS Viya Programming: Getting Started

Example Code 1.1 Example 1

libname mycas cas caslib=mycas;

Example Code 1.2 Example 2

libname mycas cas;

Second, you can use the CASLIB statement to assign a libref to a caslib. Here are two examples :

Example Code 1.3 Example 3

caslib MyCaslibName path="/home/myid" libref=cas;

The following CASLIB statement automatically assigns librefs for all existing caslibs:

Example Code 1.4 Example 4

caslib _all_ assign;

Librefs that point to caslibs are visible and available to use in SAS programs to reference in-memory tables. After librefs are assigned to caslibs, you see them listed in the Libraries section with a cloud icon. You can use standard syntax to reference in-memory tables such as libref.tablename.

Predefined CaslibsCaslibs and corresponding librefs might be predefined for you by your administrator.

Figure 1.12 Predefined Caslibs

Predefined caslibs have global scope. The administrator sets data access controls. Predefined caslibs typically contain popular shared data sources for your site.

Learn MoreFor more information, see the Learn More on page 53 Appendix chapter. These topics are covered:

n “DATA Step” on page 56

n “Data Types” on page 53

Learn More 13

Page 20: SAS Viya Programming: Getting Started

n “DS2” on page 58

n “FedSQL” on page 59

n “Macro Language” on page 57

n “SAS Viya Files Service” on page 56

n “User-Defined Formats” on page 60

14 Chapter 1 / Before You Begin

Page 21: SAS Viya Programming: Getting Started

2About the Example

Business Case Scenario . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

About the Variables in the Data Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

Business Case ScenarioThis section walks you through the specific tasks that are accomplished thorough the examples in this book.

Assume you are given a CSV file, which contains 5,000 rows of data pertaining to customers from all across the United States. Based on the data provided, you are the asked to complete the following tasks.

1 Determine the average credit score for the following age ranges:

n 18-25

n 26-35

n 36-45

n 46-55

n 56-64

n 65+

2 Determine the customer’s eligibility for a new credit card offer based on their FICO credit rating.

Table 2.1 FICO Credit Rating

Credit score range FICO credit rating

300-579 Very poor

580-669 Fair

15

Page 22: SAS Viya Programming: Getting Started

Credit score range FICO credit rating

670-739 Good

740-799 Very good

800-850 Exceptional

3 Based on the customer’s FICO credit rating, determine what type of credit card offer each customer is qualified for.

n If the customer has a FICO credit rating of 'Very good' or 'Exceptional', the customer qualifies for a 'Platinum' card.

n If the customer has a FICO credit rating of 'Good', then the customer qualifies for a 'Gold' card.

n If the customer has a FICO credit rating of 'Fair', then the customer qualifies for a 'Silver' card.

n If the customer has a FICO credit of 'Very Poor' and a credit score between 550 and 579, then'' the customer qualifies for a Secured Card offer.

4 Determine the percentage of people who qualified for each credit card offer.

5 Summarize each group’s eligibility criteria. Eligibility criterion are Income, Payment History, and Credit Score.

6 Determine the average credit score amongst all age ranges for each state and generate a U.S. map

7 Export reports, graphs, and maps to PDF and EXCEL using ODS statements.

8 Export the data set with new variables as a CSV.

About the Variables in the Data SetThe following table describes the variables that are in the CSV file.

Table 2.2 Variables

Variable Name Data Type

Customer_Name Character

Street_Address Character

City Character

State Character

16 Chapter 2 / About the Example

Page 23: SAS Viya Programming: Getting Started

Variable Name Data Type

Zipcode Numeric

Age Numeric

Income Numeric

Payment_History Numeric

Number_of_Open_Credit_Cards Numeric

Credit_Score Numeric

Total_Debt Numeric

State_FIPS Numeric

Region_FIPS Numeric

About the Variables in the Data Set 17

Page 24: SAS Viya Programming: Getting Started

18 Chapter 2 / About the Example

Page 25: SAS Viya Programming: Getting Started

3Accessing Data

Starting a CAS Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

Adding a Caslib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

Ways to Access Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

Loading a CSV File by Using the CASUTIL Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . 21See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

Downloading and Loading a File to the CAS Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

Starting a CAS SessionBefore you can begin accessing your data, you must have an active CAS session.

The following example starts a CAS session. The CAS statement is used to connect to the Cloud Analytic Services.

cas mySession;

The CAS session name is mySession. If you do not specify a session name, the default session name is Casauto.

The following is printed to the log.

Example Code 3.1 Log

NOTE: The session MYSESSION connected successfully to Cloud Analytic Services using port 5570. The UUID is 650af61d-4576-da4b-a4a5-9069737d5897. The user is sasdemo and the active caslib is CASUSER(sasdemo).NOTE: The SAS option SESSREF was updated with the value MYSESSION.NOTE: The SAS macro _SESSREF_ was updated with the value MYSESSION.NOTE: The session is using 0 workers.

19

Page 26: SAS Viya Programming: Getting Started

See AlsoCAS Statement

Adding a CaslibNote: This example assumes that you have permissions to add a Caslib. For more information, see “CASLIB Statement” in SAS Cloud Analytic Services: User’s Guide and “Adjust Caslib Management Privileges” in SAS Environment Manager: User’s Guide.

The following example adds a caslib using the “CASLIB Statement” in SAS Cloud Analytic Services: User’s Guide CAS LIBNAME statement. The LIBNAME statement associates the libref with the caslib by using the CAS engine.

caslib myCaslib datasource=(srctype="path") path="/data/path/" sessref=mysession subdirs;libname myCaslib cas;

Example Code 3.2 Log

NOTE: Libref MYCASLIB was successfully assigned as follows: Engine: CAS Physical Name: 650af61d-4576-da4b-a4a5-9069737d5897

See Alson “CASLIB Statement” in SAS Cloud Analytic Services: User’s Guide

n “LIBNAME Statement: CAS Engine” in SAS Cloud Analytic Services: User’s Guide

Ways to Access DataYou can import your data to the CAS server or the SAS Compute Server by using CAS actions, the CAS procedure, or the SAS DATA step. These examples demonstrate different ways to import data.

20 Chapter 3 / Accessing Data

Page 27: SAS Viya Programming: Getting Started

Table 3.1 Tasks

Task Action or Procedure Example

Load a CSV file to CAS CASUTIL procedure “Loading a CSV File by Using the CASUTIL Procedure” on page 21

Load a client-side file HTTP procedure “Downloading and Loading a File to the CAS Server” on page 22

Loading a CSV File by Using the CASUTIL Procedure

This example loads a CSV file to the CAS server by using the CASUTIL procedure.

proc casutil; /* 1 */ load file="/data/path/creditscores.csv" /* 2 */ importoptions= /* 3 */ (filetype="csv" getnames="true") outcaslib="mycaslib" casout="creditscores" /* 4 */ replace; /* 5 */quit;

1 The CASUTIL procedure manages tables and files in SAS Cloud Analytic Services.

2 The LOAD statement reads the Creditscores CSV file and loads it to the CAS server. The FILE= argument specifies Creditscores as the external CSV file, which is accessible to the CAS server.

3 The IMPORTOPTIONS= parameter specifies the file format and options. Specify CSV as the file format. The GETNAMES= parameter specifies the use of the first row in the CSV file as the variable name.

4 The OUTCASLIB= parameter specifies MyCaslib as the caslib to use for the in-memory table, Creditscores. The CASOUT= parameter specifies Creditscores as the name for the in-memory table.

5 The REPLACE parameter replaces any existing in-memory table that is named CreditScores.

The following notes are printed to the log:

Example Code 3.3 Log

NOTE: Cloud Analytic Services made the uploaded file available as table CREDITSCORES in caslib mycaslib.NOTE: The table CREDITSCORES has been created in caslib mycaslib from binary data uploaded to Cloud Analytic Services.

Loading a CSV File by Using the CASUTIL Procedure 21

Page 28: SAS Viya Programming: Getting Started

TIP The LOAD statement in PROC CASUTIL is the recommended way to load files to in-memory tables. For more information about loading client-side data source files, see “CASUTIL Procedure” in SAS Cloud Analytic Services: User’s Guide.

See AlsoCASUTIL Procedure

Downloading and Loading a File to the CAS Server

The following example downloads a file from the SAS website, and then loads the file to the CAS server. The HTTP procedure downloads the CreditScores data set to a local temporary file. The UPLOAD statement uploads the table to the CAS server. The file is then received by CAS and stored as a temporary file until the file is loaded as an in-memory table. After the table is loaded, the file is removed.

filename scrdata temp; /* 1 */proc http /* 2 */ url='http://support.sas.com/documentation/ onlinedoc/viya/exampledatasets/creditscores.csv' out=scrdata; run;

proc cas; upload /* 3 */ path="%sysfunc(pathname(scrdata))" casOut={caslib="mycaslib", name="creditscores"} importOptions="csv";run;

table.tableInfo / /* 4 */ caslib="mycaslib", table="creditscores";quit;

filename scrdata clear; /* 5 */

1 The FILENAME statement creates the fileref Scrdata for the SAS temporary folder so that the Creditscores.csv file can be stored in this folder..

2 The HTTP procedure creates a GET request for the Creditscores.csv file.

3 The UPLOAD statement uploads the file Creditscores.csv to CAS as the in-memory table Creditscores. The PATH= argument uses the %SYSFUNC function to specify the path to the input file. The CASOUT= option specifies that

22 Chapter 3 / Accessing Data

Page 29: SAS Viya Programming: Getting Started

the in-memory table name is Creditscores and that it is stored in MyCaslib. The IMPORTOPTIONS= option specifies the file type of the input file.

4 The tableInfo action displays information about the in-memory table Creditscores. Although this step is not necessary, it is recommended to check the contents of the table after it has been loaded to the CAS server.

5 The FILENAME statement clears the Scrdata fileref.

Example Code 3.4 Log

NOTE: Cloud Analytic Services made the uploaded file available as table CREDITSCORES in caslib MYCASLIB.NOTE: The table CREDITSCORES has been created in caslib MYCASLIB from binary data uploaded to Cloud Analytic Services.

Output 3.1 tableInfo Action Results

See Alson “FILENAME Statement” in SAS Global Statements: Reference

n HTTP Procedure

n “Table information” in SAS Viya: System Programming Guide

n “UPLOAD Statement” in SAS Cloud Analytic Services: CASL Reference

Downloading and Loading a File to the CAS Server 23

Page 30: SAS Viya Programming: Getting Started

24 Chapter 3 / Accessing Data

Page 31: SAS Viya Programming: Getting Started

4Exploring Data

Tasks Performed by CAS Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

Retrieving Table Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

View Data from a CAS Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

Displaying Column Information of a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

Tasks Performed by CAS ActionsExploring your data can reveal anomalies within your data and also help you discover any issues. The following examples explain a few basic exploratory tasks.

Table 4.1 Tasks

Task Action Example

Retrieve CAS table information.

tableInfo “Retrieving Table Information” on page 26

Retrieve detailed CAS table information.

tableDetails

Retrieve rows from a CAS table.

fetch “View Data from a CAS Table” on page 27

Display column information.

columnInfo “Displaying Column Information of a Table” on page 28

25

Page 32: SAS Viya Programming: Getting Started

Retrieving Table InformationThis example illustrates retrieving table information by using the tableInfo action and retrieving detailed information by using the tableDetails action.

proc cas; /* 1 */ table.tableInfo / name="creditscores"; /* 2 */ table.tableDetails / name="creditscores"; /* 3 */quit;

1 The PROC CAS statement enables you to program and schedule SAS Cloud Analytic Services actions.

2 The tableInfo action displays information about the CAS table, such as table name, label, number of rows, number of columns, and so on.

3 The tableDetails action displays detailed information about the CreditScores table, such as the node, blocks, number of rows, and so on.

Output 4.1 tableInfo Action Results

Output 4.2 tableDetails Action Results

See Alson “Table details” in SAS Viya: System Programming Guide

n “Table information” in SAS Viya: System Programming Guide

26 Chapter 4 / Exploring Data

Page 33: SAS Viya Programming: Getting Started

View Data from a CAS TableThis example illustrates retrieving rows from the table by using the Fetch action. The Fetch action is similar to the PRINT procedure where you can view your data on the Results tab.

proc cas; /* 1 */ table.fetch/table="creditscores", /* 2 */ fetchVars={"Customer_Name", "State", "Age", /* 3 */ "Income", "Payment_History", "Credit_Score", "Total_Debt", "State_FIPS", "Region_FIPS"}, sortby={{name="State", /* 4 */ order="descending"}}, /* 5 */ index=false; /* 6 */quit;

1 The PROC CAS statement enables you to program and schedule SAS Cloud Analytic Services actions.

2 The Fetch action from the tables action set fetches rows from the CreditScores table. The Fetch action is conceptually similar to PROC PRINT and by default, displays the first 20 rows of a CAS table.

3 The FETCHVARS parameter specifies the specific variables to retrieve from the CreditScores table.

4 The SORTBY parameter specifies State as the variable to sort the CreditScores table.

5 The ORDER parameter specifies sorting the State variable in descending order.

6 When the INDEX parameter is true (the default), a column named Index, which identifies each row, is added to the CreditScores table. When the INDEX parameter is set to false, an Index column is not added to the CreditScores table.

View Data from a CAS Table 27

Page 34: SAS Viya Programming: Getting Started

Output 4.3 table.fetch Action Results (Partial Output)

See Also“Fetch rows” in SAS Viya: System Programming Guide

Displaying Column Information of a TableThis example illustrates displaying column information of a table by using the columnInfo action.

proc cas; /* 1 */ table.columninfo result=collInfo /* 2 */ / table={name="creditscores"}; collist=collInfo["ColumnInfo"]; /* 3 */ saveresult colList casout="colList"; /* 4 */

table.fetch / table="colList"; /* 5 */quit;

1 The PROC CAS statement enables you to program and schedule SAS Cloud Analytic Services actions.

28 Chapter 4 / Exploring Data

Page 35: SAS Viya Programming: Getting Started

2 The columnInfo action from the tables action set displays column information for the CreditScores table. The RESULT parameter stores the result of the columnInfo action in the collInfo table. The TABLE parameter specifies CreditScores as the input table.

3 The ASSIGNMENT statement creates the ColList variable. CollInfo is a CASL variable, which contains column data from the columnInfo action that is put into ColList.

4 The SAVERESULT statement sends the data to a CAS table named ColList. The CASOUT= parameter specifies ColList as the name of the table and stores it as an in-memory table.

5 The Fetch action retrieves the rows from the ColList result table. Without the Fetch action, you cannot view the ColList CAS table.

Output 4.4 table.fetch Action Results of ColList

See Alson Assignment Statement

n “Column information” in SAS Viya: System Programming Guide

n SAVERESULT Statement

Displaying Column Information of a Table 29

Page 36: SAS Viya Programming: Getting Started

30 Chapter 4 / Exploring Data

Page 37: SAS Viya Programming: Getting Started

5Preparing Data

Tasks Performed by CAS Actions and the DATA Step . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

Applying Labels and Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

Creating Variables and Filtering Data by Using the DATA Step . . . . . . . . . . . . . . . . . . 33See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

Tasks Performed by CAS Actions and the DATA Step

Preparing your data is the process of collecting, cleaning, and transforming data prior to analysis. The following examples explain a few basic preparatory tasks.

Table 5.1 Tasks

Task Action or Statement Example

Apply labels and formats. alterTable “Applying Labels and Formats” on page 32

Create a data set. DATA statement “Creating Variables and Filtering Data by Using the DATA Step” on page 33Conditionally select

observations.IF-THEN/ELSE statement

Specify a length for a variable.

LENGTH statement

31

Page 38: SAS Viya Programming: Getting Started

Applying Labels and FormatsThis example applies labels and formats by using the ALTERTABLE action.

proc cas; /* 1 */ table.alterTable / columns={ /* 2 */ {label="Customer Name", name="Customer_Name"}, /* 3 */ {label="Payment History", format="percent.", name="Payment_History"}, /* 4 */ {label="Credit Score", name="Credit_Score"}, {label="Total Debt", format="dollar10.", name="Total_Debt"}, {label="State FIPS", name="State_FIPS"}, {format="dollar10.", name="Income"}, {label="Region FIPS", name="Region_FIPS"}}, name="creditscores"; table.fetch / table="creditscores" /* 5 */ fetchvars={"Customer_Name", "State", "Age", "Income", /* 6 */ "Payment_History", "Credit_Score", "Total_Debt", "State_FIPS", "Region_FIPS"},index=false;quit;

1 The PROC CAS statement enables you to program and schedule SAS Cloud Analytic Services actions from the SAS client.

2 The alterTable action enables you to rename tables, apply labels and formats, and drop columns.

3 The LABEL= parameter specifies a new label for each specified column, or variable, as defined in the action.

4 The FORMAT= parameter specifies a format for the column, or variable, as defined in the action.

5 The Fetch action from the tables action set fetches rows from the CreditScores table. The Fetch action is conceptually similar to PROC PRINT and by default, displays the first 20 rows of a CAS table.

6 The FETCHVARS parameter specifies the specific variables to retrieve from the CreditScores table.

32 Chapter 5 / Preparing Data

Page 39: SAS Viya Programming: Getting Started

Output 5.1 table.fetch Action Results

See Alson “Alter table” in SAS Viya: System Programming Guide

n “Fetch rows” in SAS Viya: System Programming Guide

Creating Variables and Filtering Data by Using the DATA Step

This example uses the DATA step to create a data set named CreditQualify. The DATA step creates variables by using the LENGTH statement and conditionally filters data by using the IF-THEN/ELSE statement. The DATA step is used on the CAS server.

data mycaslib.creditqualify / sessref=mysession; /* 1 */ set mycaslib.creditscores; /* 2 */

Creating Variables and Filtering Data by Using the DATA Step 33

Page 40: SAS Viya Programming: Getting Started

length Age_Range $8; /* 3 */ if Age in (18, 19, 20, 21, 22, 23, 24, 25) then Age_Range="18-25"; /* 4 */ else if Age in (26, 27, 28, 29, 30, 31, 32, 33, 34, 35) then Age_Range="26-35"; /* 5 */ else if Age in (36, 37, 38, 39, 40, 41, 42, 43, 44, 45) then Age_Range="36-45"; else if Age in (46, 47, 48, 49, 50, 51, 52, 53, 54, 55) then Age_Range="46-55"; else if Age in (56, 57, 58, 59, 60, 61, 62, 63, 64) then Age_Range="56-64"; else if Age>=65 then Age_Range="65+"; length FICO_Rating $12; /* 6 */ if 300<=Credit_Score<=570 then FICO_Rating="Very Poor"; /* 7 */ else if 580<=Credit_Score<=669 then FICO_Rating="Fair"; else if 670<=Credit_Score<=739 then FICO_Rating="Good"; else if 740<=Credit_Score<=799 then FICO_Rating="Very Good"; else if Credit_Score>=800 then FICO_Rating="Exceptional"; length Credit_Qualification $12; /* 8 */ if Credit_Score>=740 then Credit_Qualification="Platinum"; /* 9 */ else if 650<=Credit_Score<=739 then Credit_Qualification="Gold"; else if 450<=Credit_Score<=649 then Credit_Qualification="Secured Card"; else if Credit_Score<=449 then Credit_Qualification="N/A";run;

1 The DATA statement begins a DATA step and creates a CAS table named CreditQualify in the Mycaslib caslib in the MYSESSION CAS session.

2 The SET statement reads observations from the CreditScores CAS table from the MyCaslib caslib.

3 The LENGTH statement creates a character variable, Age_Range, and assigns a length of 8 to the variable.

4 The IF-THEN/ELSE statement selects the rows where the value of Age is 18, 19, 20, 21, 22, 23, 24, 25 and assigns the value 18–25 to the Age_Range variable.

5 After SAS looks through the initial observation for the first set of age ranges, it moves onto the next observtion using the ELSE-IF statement and assigns the values to the Age_Range variable.

6 The LENGTH statement creates a character variable, FICO_Rating, and assigns a length of 12 to the variable.

7 The IF-THEN/ELSE statement selects the rows where the value of Score is between 300 and 570 and assigns the value of Very Poor to the FICO_Rating variable. After SAS looks through the initial observation for the first set of score ranges, it moves onto the next observation using the ELSE-IF statement and assigns the values to the FICO_Rating variable.

8 The LENGTH statement creates a character variable, Credit_Qualification, and assigns a length of 12 to the variable.

34 Chapter 5 / Preparing Data

Page 41: SAS Viya Programming: Getting Started

9 The IF-THEN/ELSE statement selects the rows where the value of Credit_Score is greater than 740, and then assigns the value of Platinum to the Credit_Qualification variable. After SAS looks through the initial observation for the first set of score ranges, it moves onto the next observation using the ELSE-IF statement and assigns the values to the Credit_Qualification variable.

You can view the CreditQualify data set by using the Fetch action.

proc cas; table.fetch/table="creditqualify" index=false;quit;

Output 5.2 table.fetch Action Results (Partial Output)

See Alson DATA Statement

n IF-THEN/ELSE Statement

n LENGTH Statement

n SET Statement

n “Fetch rows” in SAS Viya: System Programming Guide

Creating Variables and Filtering Data by Using the DATA Step 35

Page 42: SAS Viya Programming: Getting Started

36 Chapter 5 / Preparing Data

Page 43: SAS Viya Programming: Getting Started

6Analyzing and Reporting Data

Tasks Performed by CAS Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

Summarizing Multiple Numeric Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

Creating a Group-By Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

Creating a Gradient Map of the United States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42Converting X and Y Variables from Radians to Degrees . . . . . . . . . . . . . . . . . . . . . . . . . 42Creating Plot Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42Assigning a Title and Footnote . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42Creating the Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

Creating a Frequency Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44Counting the Number of People Qualified . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44Creating a Frequency Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

Tasks Performed by CAS ActionsAnalyzing and reporting on your data involves creating summaries or reports based on the data that you prepared in Chapter 5, “Preparing Data,” on page 31 . The following table demonstrates a few basic analytic and reporting tasks.

Table 6.1 Tasks

Task Action or Procedure Example

Summarize multiple numeric variables.

mdSummary action “Summarizing Multiple Numeric Variables” on page 38

37

Page 44: SAS Viya Programming: Getting Started

Task Action or Procedure Example

Create a Group-by table. groupBy action “Creating a Group-By Table” on page 39

Create a gradient map. SGMAP procedure “Creating a Gradient Map of the United States” on page 42

Create a frequency table. freqTab action “Creating a Frequency Table” on page 44

Summarizing Multiple Numeric VariablesThis example summarizes each age group’s eligibility criteria (Income, Payment_History, Credit_Score) and determines the minimum, maximum, and mean values for each criterion. This type of summarization can help monitor each age group’s credit score for future credit qualification.

proc cas; /* 1 */ session mysession; simple.mdSummary result=creditsummary status=s / /* 2 */ inputs={"Income", "Credit_Score", "Payment_History"} sets={{groupBy={"Age_Range"}}} table={name="creditqualify"} subSet={"MAX", "MIN", "MEAN"} ;run;print creditsummary; /* 3 */run;quit;

1 The PROC CAS statement enables you to program and schedule SAS Cloud Analytic Services actions from the SAS client.

2 The mdSummary action calculates summaries of the numeric variables Income, Payment_History, and Credit_Score. The action also groups the MIN, MAX, and MEAN results for the variables in the CreditSummary table for each age range.

3 The PRINT statement displays the values of the result table, CreditSummary, to the current output location.

38 Chapter 6 / Analyzing and Reporting Data

Page 45: SAS Viya Programming: Getting Started

Output 6.1 simple.mdSummary Action Results (Partial Output)

See Alson “Multidimensional summary” in SAS Visual Analytics: Programming Guide

n “PRINT Statement” in SAS Cloud Analytic Services: CASL Reference

Creating a Group-By TableThis example groups the CreditQualify table by the State_Name and State variables and uses Credit_Score as the weighting variable. The example then saves the Group-by table to ScorePerState.sashdat in the active caslib for later use.

proc cas; /* 1 */ session mysession; simple.groupBy result=r status=s / /* 2 */ inputs={"State", "State_FIPS"}, /* 3 */ weight="Credit_Score", /* 4 */ aggregator="MEAN", /* 5 */ scoregt=0, /* 6 */ scorelt=900, table={name="creditqualify"}, /* 7 */ casout={name="ScorePerState", /* 8 */ replace=true};run; if (s.severity = 0) then do; /* 9 */

Creating a Group-By Table 39

Page 46: SAS Viya Programming: Getting Started

table.alterTable / columns={ {label="Credit Score", format="5.2" name="_Score_"}}, name="ScorePerState"; table.fetch / format=True, fetchVars={"State", "State_FIPS", {name="_Score_",format="5.2"}}, table={name="ScorePerState"} index=false;

table.save / /* 10 */ table={name="ScorePerState"}, name="ScorePerState.sashdat", replace=True; end;run;quit;

1 The PROC CAS statement enables you to program and schedule SAS Cloud Analytic Services actions from the SAS client.

2 The groupBy action builds BY groups for the CreditQualify table.

3 The INPUTS= parameter specifies State_Name and State as the input variables. These variables are used to group the table.

4 The WEIGHT= parameter specifies Credit_Score as the weighting variable.

5 The AGGREGATOR= parameter specifies MEAN as the aggregator.

6 The SCOREGT= and SCORELT= options specify the upper and lower bounds of the numeric rank order scores of the distinct groupings to return.

7 The TABLE= parameter specifies CreditQualify as the input table.

8 The CASOUT= parameter specifies ScorePerState as the output table in the active caslib. The REPLACE parameter replaces the output table if it already exists.

9 The IF-THEN/DO statement uses the alterTable action to alter the Credit_Score column. The IF-THEN/DO statement then uses the fetch action to fetch the variables State, State_FIPS, and _SCORE_ from the ScorePerState table. The action also applies the SAS format 5.2 to the _SCORE_ variable, and then prints the result table.

10 The save action saves the ScorePerState to a file as ScorePerState.sashdat in the active caslib for later use.

40 Chapter 6 / Analyzing and Reporting Data

Page 47: SAS Viya Programming: Getting Started

Output 6.2 table.fetch Action Results

See Alson “Alter table” in SAS Viya: System Programming Guide

n “Fetch rows” in SAS Viya: System Programming Guide

n “Group-by” in SAS Visual Analytics: Programming Guide

n “Save table” in SAS Viya: System Programming Guide

Creating a Group-By Table 41

Page 48: SAS Viya Programming: Getting Started

Creating a Gradient Map of the United States

Converting X and Y Variables from Radians to Degrees

The CHOROMAP statement plots the X (longitude) and Y (latitude) variables from the MAPS data set that contains unprojected latitude and longitude values. To match the projection types, convert the X and Y variables from radians to degrees.

data states; set maps.states; if state ^in(2,15,72); x = -x * 45/atan(1); y = y * 45/atan(1);run;

Creating Plot DataCreate the plot data, which is used by the SGMAP procedure statement and the TEXT statement. Keep only the lower 48 U.S. states, and collect the state Federal Information Processing Standards (FIPS) codes.

data plot_data; set maps.uscenter; if state ^in(2,15,72) and ocean^='Y'; long = -long; statename = fipstate(state);run;

Assigning a Title and FootnoteUse the global TITLE and FOOTNOTE statements.

title 'Average Credit Score in Each State';footnote4 'Map only includes the lower 48 states in the United States';

42 Chapter 6 / Analyzing and Reporting Data

Page 49: SAS Viya Programming: Getting Started

Creating the GraphRun the SGMAP procedure by using as input the states map data set, the U.S. Census Bureau 2010 population response data set, and the plot data set. Use the ESRIMAP statement to create the base map. Use the CHOROMAP statement to plot state populations. Assign state names by using the TEXT statement. Use the GRADLEGEND statement and its options to assign a gradient legend for the states.

proc sgmap mapdata=work.states maprespdata=mycaslib.scoreperstate plotdata=work.plot_data;

esrimap url='http://services.arcgisonline.com/arcgis/rest/services/Canvas/World_Light_Gray_Base';

choromap _Score_ / mapid=state id=state_fips density=1 numlevels=4 leveltype=none colormodel=( sty greenyellow deepskyblue cornflowerblue beige) name='choro';

text x=long y=lat text=statename / textattrs=(size=6pt); gradlegend 'choro' / title='Average Credit Score' extractscale;

run;

quit;

Creating a Gradient Map of the United States 43

Page 50: SAS Viya Programming: Getting Started

Output 6.3 SGMAP Procedure Result

See Alson “FOOTNOTE Statement” in SAS Global Statements: Reference

n “PROC SGMAP Statement” in SAS/GRAPH and Base SAS: Mapping Reference

n “TITLE Statement” in SAS Global Statements: Reference

Creating a Frequency Table

Counting the Number of People QualifiedThis example uses the IF-THEN statement to conditionally apply a count to the number of people who qualified for credit.

data mycaslib.qualifyapps; set mycaslib.creditqualify; if Credit_Qualification='N/A' then Count=0;

44 Chapter 6 / Analyzing and Reporting Data

Page 51: SAS Viya Programming: Getting Started

else Count=1;run;

Creating a Frequency TableThis example uses the freqTab action to determine the percentage of people who qualified for credit.

proc cas; action freqTab.freqTab/ table='qualifyapps', weight='Count', tabulate={'Credit_Qualification'};quit;

Output 6.4 Results from freqTab Action

See AlsofreqTab Action

Creating a Frequency Table 45

Page 52: SAS Viya Programming: Getting Started

46 Chapter 6 / Analyzing and Reporting Data

Page 53: SAS Viya Programming: Getting Started

7Exporting Data

Tasks Performed by Statements and Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

Creating ODS EXCEL Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

Creating PDF Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

Exporting to a CSV File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

Tasks Performed by Statements and Procedures

Exporting your data can include tasks such as creating PDF or ODS EXCEL output as well as exporting your data to a file such as CSV or a SAS data set. The following examples explain a few basic exporting tasks.

Table 7.1 Tasks

Task Procedure or Statement Example

Create EXCEL output. ODS EXCEL statement “Creating ODS EXCEL Output” on page 48

Create PDF output. ODS PDF statement “Creating PDF Output” on page 50

Export to a CSV file. EXPORT procedure “Exporting to a CSV File” on page 51

47

Page 54: SAS Viya Programming: Getting Started

Creating ODS EXCEL OutputThis example opens the ODS destination for Microsoft Excel, which produces Excel spreadsheet files that are compatible with Microsoft Office 2010 and later. For the purposes of this example, rerun the “Creating a Frequency Table” on page 45 example with the ODS EXCEL statement.

filename outfile "/tmp/freqtable.xlsx";ods excel file=outfile options(sheet_label="CreditQualification" embedded_titles="yes" embed_titles_once="yes");proc cas; action freqTab.freqTab/ table='qualifyapps', weight='Count', tabulate={'Credit_Qualification'};quit;

You can download the EXCEL output from SAS Studio directly to your local machine.

48 Chapter 7 / Exporting Data

Page 55: SAS Viya Programming: Getting Started

Output 7.1 EXCEL Output of FreqTab Action

See Also“ODS EXCEL Statement” in SAS Output Delivery System: User’s Guide

Creating ODS EXCEL Output 49

Page 56: SAS Viya Programming: Getting Started

Creating PDF OutputThis example opens the PDF destination, which produces PDF output. For the purposes of this example, rerun the “Creating the Graph” on page 43 example with the ODS PDF statement.

ods pdf (id=SapphireStyle) style=Sapphire file='your-file-path/usamap.pdf';title 'Average Credit Score in Each State';footnote4 'Map only includes the lower 48 states in the United States';

proc sgmap mapdata=work.states maprespdata=mycaslib.scoreperstate plotdata=work.plot_data; esrimap url='http://services.arcgisonline.com/arcgis/rest/services/Canvas/World_Light_Gray_Base'; choromap _Score_ / mapid=state id=state_fips density=1 numlevels=4 leveltype=none colormodel=(sty greenyellow deepskyblue cornflowerblue beige) name='choro'; text x=long y=lat text=statename / textattrs=(size=6pt); gradlegend 'choro' / title='Average Credit Score' extractscale;run;quit;

You can download the PDF from SAS Studio directly to your local machine.

50 Chapter 7 / Exporting Data

Page 57: SAS Viya Programming: Getting Started

Output 7.2 PDF Output of SGMAP Procedure

See Also“ODS PDF Statement” in SAS Output Delivery System: User’s Guide

Exporting to a CSV FileThis example exports the SAS data set, CreditQualify, as a CSV file.

proc export data=mycaslib.creditqualify /* 1 */ outfile="your-file-path\CreditQualify.csv" /* 2 */ dbms=csv /* 3 */ replace; /* 4 */run;

1 The DATA= option identifies the input SAS data set as MyCaslib.CreditQualify.

Exporting to a CSV File 51

Page 58: SAS Viya Programming: Getting Started

2 The OUTFILE= option specifies the output file.

3 The DBMS= option specifies that the output file is a CSV file.

4 The REPLACE option overwrites the target CSV if it exists.

The EXPORT procedure produces this external CSV file:

Output 7.3 CreditQualify CSV File

See AlsoEXPORT Procedure

52 Chapter 7 / Exporting Data

Page 59: SAS Viya Programming: Getting Started

Appendix 1Learn More

Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

SAS Viya Files Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

DATA Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

Macro Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

DS2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

FedSQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59An Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59FedSQL and the Compute Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59FedSQL and the CAS Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

User-Defined Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

Data TypesThe CAS server supports these data types:

CHARACTER (CHAR) NUMERIC (DOUBLE)INT32 VARBINARYINT64 VARCHAR

The CHARACTER and NUMERIC data types are traditionally supported by SAS. You use the VARBINARY data type for image, audio, and document files.

When you load data into SAS Viya, data types that are supported by the CAS server are read in without changing the data type. If you have values in a data type that are not supported, those values are automatically converted to one of the data types that are supported by the CAS server.

53

Page 60: SAS Viya Programming: Getting Started

Variables that are created by using the VARCHAR data type are varying width and use character semantics, rather than being fixed-width and using the byte semantics of the traditional CHARACTER data type. This means that for VARCHAR data, lengths are in terms of characters; for CHARACTER data, lengths are in terms of bytes.

Variables that are created or loaded using the INT32 or INT64 data types support more digits of precision than the traditional NUMERIC data type. All calculations that occur on the CAS engine maintain the INT32 or INT64 data type. Calculations in DATA steps or procedures that run on the SAS®9 engine are converted to NUMERIC values.

Note: The SAS NUMERIC data type is the same as the DOUBLE data type.

The CHARACTER and NUMERIC data types continue to be the supported data types for processing in the SAS Compute Server. The SAS Compute Server performs calculations for SAS® 9.4.

The table in the following section lists some common data types that you might be using as input to SAS Viya. In the table, the letter Y indicates the data types that are supported for programming on the CAS server. In the last column, Y indicates data types that are supported on the SAS Compute Server, if you are connecting to the CAS server from a SAS® 9.4 client.

Table A1.1 Data Types That Are Used by SAS Viya

Data Type

CAS Action

sCASL

Data Connect

ors

Procedures and DATA

StepDS2

FedSQL

Compute Server

Processing

BIGINT Y Y1

BOOLEAN Y Y

CHARACTER (CHAR) Y Y Y Y Y Y Y

DATE Y Y Y1

DATETIME Y Y

DOUBLE2 Y Y Y Y Y Y

FLOAT Y Y1

INTEGER Y Y1

INT32 Y Y Y Y

INT64 Y Y Y Y

ITEMS Y

LISTS Y

NCHAR Y Y1

54 Appendix 1 / Learn More

Page 61: SAS Viya Programming: Getting Started

Data Type

CAS Action

sCASL

Data Connect

ors

Procedures and DATA

StepDS2

FedSQL

Compute Server

Processing

NUMERIC

(NUM)2

Y Y 2

NVARCHAR Y Y1

SMALLINT Y Y1

STRING UTF-8 Y

TABLE Y

TIME Y Y Y1

TIMESTAMP Y Y1

TINYINT Y Y1

VARBINARY Y Y Y Y

VARCHAR Y Y Y Y Y Y

1 DS2 supports data definition with these data types in CAS. However, the types are converted to CAS native types. For information about DS2 data type conversions for CAS, see “Data Types” on page 53.

2 DS2 and FedSQL do not support data that is declared as a SAS NUMERIC. However, DS2 and FedSQL do support the DOUBLE data type.

There is specific information about all of the data types that are supported in the documentation for different SAS features. Here are some key topics:

n Some of the SAS utility procedures read CAS tables or metadata about the tables, but the utility procedures do not run on the CAS server.

n Using the VARCHAR data type in the DATA step on the CAS server has some restrictions. For more information, see “Restrictions for the VARCHAR Data Type in the CAS Engine” in SAS Cloud Analytic Services: User’s Guide.

n SAS Data Connectors support more data types than those listed in the preceding table. Check the data connector documentation for your data source to see the complete list of supported data types. There is also information about data type conversions that occur when data is loaded to CAS and when CAS data is written back to your data source. For more information, see “Data Connectors” in SAS Cloud Analytic Services: User’s Guide.

n The DS2 language supports several additional data types. On the CAS server, DS2 converts non-native data types to CAS-native types. For information about CAS data types, see “Data Types” in SAS Cloud Analytic Services: User’s Guide.

n The CVP LIBNAME engine converts the CHAR data type to VARCHAR if you specify the CVPVARCHAR=YES LIBNAME option.

n The DS2 language supports several additional data types. On the CAS server, DS2 converts non-native data types to CHARACTER, NUMERIC, or VARCHAR. For information about data types that are supported for specific data sources, see “Data Type Reference” in SAS DS2 Language Reference.

Data Types 55

Page 62: SAS Viya Programming: Getting Started

SAS Viya Files ServiceThe SAS Viya Files service enables you to store, retrieve, and delete files maintained in the SAS Infrastructure Data Server database repository. The repository is not considered a complete 'file system'. Rather, the repository contains individual files that are directly accessible by their file identifier. This file identifier contains a universally unique identifier (UUID) generated by the Files service when a file is created.

The Files service also assigns a unique name to each file in the repository, but it is not human-friendly name. A user can change the name, but there is a risk that the name might not be unique within the repository.

You can access a file in the Files service using the file identifier. The file identifier is contained in the URI stored in the file information. Use the system-generated name or user-assigned name to find the file URI and the file identifier. Once you have the file identifier, you can then use it to access the file directly in the Files service.

The Files service does not have a concept of 'folders' in its repository. However, you can associate files using a PARENTURI= option. A PARENTURI= option specifies a relative URI for any object in SAS Viya. You can create a collection of files by specifying the same value of the PARENTURI= option for each file.

This example creates the file class.csv and attaches the file to a job named in the fileref Jobout.

filename jobout filesrvc parenturi='/jobExecution/jobs/5a308aa7-1c3a-4465-a14c-fd69a9091926';data _null_; set sashelp.class; file jobout('class.csv'); put name "," sex "," age "," height "," weight;run;

For more information, see “FILENAME Statement: FILESRVC Access Method” in SAS Global Statements: Reference.

DATA StepThe DATA step can run in your SAS session as well as on the CAS server. In CAS, the DATA step runs in a CAS server session on a single server (called symmetric multiprocessing, or SMP) or across multiple computers in parallel (called massively parallel processing, or MPP). By default, the DATA step runs in multiple threads. When the DATA step runs in multiple threads, the same DATA step program runs in multiple threads on all nodes in the environment. For more information, see “Processing Modes” in SAS Cloud Analytic Services: DATA Step Programming.

56 Appendix 1 / Learn More

Page 63: SAS Viya Programming: Getting Started

When in-memory CAS tables are used exclusively, the DATA step takes advantage of the in-memory speeds. Some language element restrictions and VARCHAR data type restrictions apply when the DATA step runs on the CAS server.

For an overview of running the DATA step in CAS, see “Basics” in SAS Cloud Analytic Services: DATA Step Programming .

Not all DATA step language elements that you used in previous releases of SAS are appropriate for distributed processing in the CAS environment. For a list of language elements that run on the CAS server, see the category table in the respective documentation:

n Hash objects and Java objects

n Data Set Options

n Formats

n Functions

n Statements: DATA step and global

If the language element specifies a category of CAS in the documentation, it can run on the CAS server.

Some restrictions also apply when you use the DATA step on the CAS server. For a list of these restrictions, see the following topics:

n “Restrictions” in SAS Cloud Analytic Services: DATA Step Programming

n “Restrictions for the VARCHAR Data Type in the CAS Engine” in SAS Cloud Analytic Services: User’s Guide

The BY statement in a SAS DATA step divides table data into groups of rows that share the same values for the BY variables. When you use the BY statement in a distributed server, CAS groups the rows based on the first BY variable. When CAS distributes the table across multiple nodes, it keeps the BY groups intact. That is, rows that share the same BY variables are stored together on a single machine. For information about BY processing behavior in CAS, see these topics:

n “How CAS Groups Data with BY Variables” in SAS Cloud Analytic Services: DATA Step Programming

n “How Multithreaded DATA Step Execution Affects BY–Groups in CAS” in SAS Cloud Analytic Services: DATA Step Programming

Note: You can also submit DATA step code by using the CAS action dataStep.runCode in PROC CAS.

Video: Using the DATA Step in SAS Viya

Video: Processing Data in Groups with the DATA Step in SAS Viya

Macro LanguageMacros are supported in SAS Viya, but only in the SAS session. Macros can be useful in generating code and submitting the code to the CAS server to run. However, the macro language itself does not run on the CAS server.

Macro Language 57

Page 64: SAS Viya Programming: Getting Started

See Alson SAS Macro Language: Reference

DS2DS2 runs in SAS Viya as it has in previous releases of SAS. A DS2 program that sets from a DS2 thread runs by using multiple threads. When your threaded DS2 program runs in CAS, parallel execution speeds the processing of big data by running the program on multiple CAS workers and dividing processing workload among threads on the workers.

For more information, see “How DS2 Runs in CAS” in SAS DS2 Programmer’s Guide.

In addition to using PROC DS2 to run DS2 code, you can also use the ds2.runDS2 action by using PROC CAS or a supported third-party language. You can also specify FedSQL statements in the DS2 SET statement.

The DS2 action set has the ability to publish and run DATA step and DS2 models in CAS, Hadoop, or Teradata.

Scoring models in Hadoop can be run with either MapReduce or the Spark2 engine.

DS2 supports the same data sources in SAS Viya as it does in SAS 9.4, with two additions. DS2 supports Apache Spark and JDBC-compliant databases. You can access the data sources through the SAS Compute Server by using SAS/ACCESS software. You can access the data sources from the CAS server with SAS data connectors.

The following language elements are not supported in CAS. Note that these language elements are supported in a SAS session in a SAS Viya client.

n DS2 SQLSTMT package

n DS2 SQLEXEC function

n DS2 HASH package input from a table or output to a table (All other services of the DS2 HASH package are supported in CAS.)

When a DS2 program runs in CAS, the DS2 program does not have access to the librefs that are created in the SAS client. Only caslibs that are created in the CAS session are available to the DS2 program.

See Alson SAS DS2 Programmer’s Guide

n SAS DS2 Language Reference

58 Appendix 1 / Learn More

Page 65: SAS Viya Programming: Getting Started

FedSQL

An IntroductionSAS FedSQL is a SAS proprietary implementation of the ANSI SQL:1999 core standard. It provides support for new data types and other ANSI 1999 core compliance features and proprietary extensions. FedSQL provides a scalable, threaded, high-performance way to access, manage, and share relational data from multiple data sources. When possible, FedSQL queries are optimized with multithreaded algorithms in order to resolve large-scale operations. For applications, FedSQL provides a common SQL syntax across all data sources. That is, FedSQL is a vendor-neutral SQL dialect that accesses data from various data sources without having to submit queries in the SQL dialect that is specific to the data source. In addition, a single FedSQL query can target data in several data sources and return a single result set.

FedSQL supports the same data sources in SAS Viya as it does in SAS 9.4 when accessing data through a SAS library. Appropriate SAS/ACCESS software is required.

FedSQL and the Compute ServerWhen FedSQL requests are submitted through the SAS Compute Server, FedSQL supports the same statements in SAS Viya as it does in SAS 9.4. FedSQL uses its extended data type support to process the third-party data. FedSQL supports full and partial implicit SQL pass-through. It supports explicit SQL pass-through in the CONNECTION TO component of the SELECT statement and via the EXECUTE statement.

You submit FedSQL statements to the SAS Compute Server by using PROC FEDSQL. In documentation, this is referred to as submitting requests to a SAS library. PROC FEDSQL submits requests to a SAS library by default. You assign a SAS library reference (libref) to access SAS and third-party data. You reference tables in FedSQL statements by using a two-part name in the form libref.table-name. If you refer to tables simply by table-name, the procedure assumes that you are using the SAS Work library, which persists only for the duration of the SAS session.

For more information about the FedSQL functionality that is supported for SAS libraries, see SAS FedSQL Language Reference.

FedSQL 59

Page 66: SAS Viya Programming: Getting Started

FedSQL and the CAS ServerOn the CAS server, only the following statements are supported: CREATE TABLE with the AS expression, SELECT, and DROP TABLE. FedSQL supports full-query implicit SQL pass-through in CAS. That is, the entire request must be capable of being passed to the data source for processing. FedSQL supports limited explicit SQL pass-through. You can submit native SQL syntax in a CONNECTION TO component of the FedSQL SELECT statement FROM clause. Data processed on the CAS server uses CAS data types.

You can submit FedSQL statements to the CAS server by using PROC FEDSQL or by using the fedSql.execDirect action. PROC FEDSQL requests are directed to the CAS server when you specify a CAS session name in the SESSREF= or SESSUUID= procedure option. The fedSQL.execDirect action enables you to submit FedSQL requests from a CASL, Python, Lua, and R session.

In CAS, you can preload data into your CAS session by using PROC CASUTIL or the table.loadTable action and then query it with FedSQL. Or, you can load CAS and third-party data on demand by using a global or a local caslib. Reference tables in FedSQL statements by using a two-part name in the form caslib.table-name. It is not necessary to specify a caslib when querying data that was preloaded into the active caslib. All data in a CAS session exists in-memory, for local use, for the duration of the CAS user session. To promote tables so that they are available to other users, or save the tables, you must use another CAS action.

In CAS, FedSQL supports the data sources for which there are SAS data connectors. Appropriate data connector software is required.

For more information about the FedSQL functionality that is supported on the CAS server, see SAS Viya: FedSQL Programming for SAS Cloud Analytic Services.

See Alson “CAS Procedure” in SAS Cloud Analytic Services: CASL Reference

n “FEDSQL Procedure” in Base SAS Procedures Guide

n “FedSQL Action Set” in SAS Viya: System Programming Guide

n “Dynamically Executing FedSQL Statements from DS2” in SAS DS2 Programmer’s Guide

User-Defined FormatsIn SAS Viya, you can store user-defined formats in catalogs to use in a SAS session, or you can store the user-defined formats in a format library on the CAS server. Format libraries are associated with a CAS session, or they can be promoted

60 Appendix 1 / Learn More

Page 67: SAS Viya Programming: Getting Started

to global scope to be available to all CAS sessions. User-defined formats in a format library are server-side formats that the server uses when an analysis is performed according to formatted values. You can migrate existing user-defined formats from SAS to SAS Viya. For more information, see SAS Cloud Analytic Services: User-Defined Formats.

See Alson SAS Cloud Analytic Services: User-Defined Formats

User-Defined Formats 61

Page 68: SAS Viya Programming: Getting Started

62 Appendix 1 / Learn More