SAS Viya

11月 132018
 

In my previous blog post I demonstrated how to create your own CAS actions and action sets.  In this post, we will explore how to create your own CAS functions using the CAS Language (CASL).  A function is a component of the CASL programming language that can accept arguments, perform a computation or other operation, and return a value.  The value that is returned can be used in an assignment statement or elsewhere in expressions.

About SAS functions

SAS provides two types of supplied functions: built-in functions and common functions.  Built-in functions contain functionality that is unique to CASL.  These allow you to perform operations on your result tables, arrays, and dictionaries, and provide run-time support for your CASL programs.  Built-in functions cannot be replaced with user-defined functions.

Conversely, common functions provide functionality that is common to other SAS functions.  When used in a CASL program, SAS functions take a CASL value and a CASL value is returned.  Unlike built-in functions, you can replace these functions with user-defined functions.

Since the capabilities of built-in functions are unique to CASL, let’s look at these in-depth and demonstrate with an example.  Save the following FedSQL code in an external file called hmeqsql.sas.  This code will be read into CAS and stored as a variable.

The execDirect action executes FedSQL code in CAS.  The READPATH built-in function reads the FedSQL code saved in hmeqsql.sas and stores it in the CASL variable sqlcode which is used as input to the query parameter.

The fetch action displays the first 20 rows from the output table hmeq.out.

If you don’t feel like looking through the documentation for a built-in or common function, a list of each can be generated programmatically.  Run the following code to see a list of built-in functions.

Partial list of CASL built-in functions

Run the following code to see a list of common functions.

Partial list of common functions

User-defined CASL functions

In addition to the customizable capabilities of built-in functions supplied by SAS, you can also create your own functions using the FUNCTION statement.  User-defined functions can be called in expressions using CASL and they provide a large amount of flexibility.  The following example creates four different functions for temperature conversion.

After creating these functions, they can be called immediately, or you can store them in an external file and call them via a %include statement.  In this example, the user-defined functions have been stored in an external file called FunctionStore.sas.  You can call one, all, or any number of your user-defined functions.

The output from each function call is displayed in the log.

Lastly, if you want to see all user-defined functions, run the FUNCTIONLIST statement.  A list will be printed to the log.

More about CASL programming and using functions in CASL

Check out these resources for further information on programming in the CASL language and using functions in CASL.

Customize your CASL code with built-in and user-defined functions was published on SAS Users.

11月 072018
 

Migration, version road maps and configurations were the themes of several questions that came up in a recent webinar about combining SAS Grid Manager and SAS Viya. You’ll see in this blog post that we were ready to get into the nitty-gritty details in our answers below – just as we did in the previous FAQs post. We hope you find them useful in your work using SAS Grid Manager and SAS Viya together.

1. Can we migrate SAS programs that are currently on SAS PC environments into the SAS Grid environment – or do we need to rewrite the programs for SAS Grid Manager?

No, you don’t need to rewrite your SAS programs to run on a SAS Grid environment. Many customers migrate their code from other environments (like PCs or servers) and submit them to SAS Grid Manager from SAS Display Manager, SAS Studio or any other application of their choice.

If you already use SAS Enterprise Guide to run jobs on a remote server, the process may be as simple as changing your server configuration to use a grid-launched workspace server (information that your SAS Administrator would provide) and continuing to work in much the same way as always, requiring no changes to your code.

Depending on other changes that take place at the same time SAS Grid Manager is implemented, there may need to be some small adjustments to your programs.  For example, if your organization consolidates source data onto new storage, you may need to change paths associated with your LIBNAME statements.  These should be housekeeping items rather than significant rewrites of the logic in your SAS code.

If you plan to continue to use the programming environment provided by BASE SAS itself (DMS) and have been using SAS/CONNECT, you will need to add the SIGNON statement to start a session on the SAS Grid Manager

  • ENDRSUBMIT statement to end the block of code to be run on the grid
  • Divide and Conquer – Writing Parallel SAS Code to Speed Up Your SAS Program.

    2. Is there a version of SAS Grid Manager that runs on the SAS Viya architecture?

    The SAS Grid Manager roadmap includes a release of SAS Grid Manager on the SAS Viya Architecture late in 2019.

    3. Will I be able to migrate my SAS Grid Manager configuration and jobs from SAS 9.4 to the SAS Viya-based release of SAS Grid Manager?

     The plan to deliver SAS Grid Manager on the SAS Viya architecture includes automation to migrate jobs, flows, and schedule information from your SAS 9.4 environment to your SAS Viya environment.  It is our goal to make this transition as straightforward and easy as possible – especially where there is feature parity between SAS 9 based and SAS Viya-based solutions.  Since each product delivers solution-specific PROCs and other functionality that can be used within a job executed by SAS Grid Manager, each customer should work with their SAS team to understand which jobs can be migrated and which jobs may need to continue to run against your SAS 9.4 environment.  

    * * *

    These were all great questions that we thought deserved more detail than we could offer in a webinar.  If you have more questions that weren’t covered here, or in our previous post on this topic, just post them in the comments section.  We’ll answer them quickly.  Thanks for your interest!

    3 questions about implementing SAS Grid Manager and SAS Viya was published on SAS Users.

  • 11月 062018
     

    This post was also written by SAS' Xiangxiang Meng.

    You can communicate with various clients (SAS, Python, Lua, Java, and REST) in the same place using Pandas Data Analysis Library, CAS actions should come naturally. CAS enables you to subset tables using Python expressions. Using Python, you can create conditions that are based on the data pulled, instead of creating the conditions yourself. SAS® will use the information you want pulled to determine which rows to select.

    For example, rather than using fixed values of rows and columns to select data, SAS can create conditions based on the data in the table to determine which rows to select. This is done using the same syntax as DataFrames. CASColumn objects support Python’s various comparison operators and builds a filter that subsets the rows in the table. You can then use the result of that comparison to index into a CASTable. It sounds much more complicated than it is, so let’s look at an example.

    The examples below are from the Iris flower data set, which is available in the SASHELP library, in all distributions of SAS. The listed code and output are produced using the IPython interface but can be employed with Jupyter Notebook just as easily.

    If we want to get a CASTable that only contains values where petal_length is greater than 7, we can use the following expression to create our filter.


    Behind the scenes, this expression creates a computed column that is used in a WHERE expression on the CASTable. This expression can then be used as an index value for a CASTable. Indexing this way essentially creates a boolean mask. Wherever the expression values are true, the rows of the table are returned. Wherever the expression is false, the rows are filtered out.

    These two steps are more commonly done in one line.


    We can further filter rows out by indexing another comparison.

    Comparisons can be joined using the bitwise comparison operators & (and) and | (or). You do have to be careful with these though due to the operator precedence. Bitwise comparison has a higher precedence than comparisons such as greater-than and less-than, so you need to wrap your comparisons in parentheses.


    In all cases, we are not changing anything about the underlying data in CAS. We are simply constructing a query that is executed with the CASTable when it is used as the parameter in a CAS action. You can see what is happening behind the scenes by displaying the resulting CASTable objects.


    You can also do mathematical operations on columns with constants or other columns within your comparisons.

    The list of supported operations is shown in the table below.

    The supported comparison and operators are shown in the following table.

    As you can see in the tables above, it is possible to do comparisons on character columns as well. This includes using many of Python’s string methods on the column values. These are accessed using the str attribute of the column, just like in DataFrames.

    This easy syntax allows the Python client to manipulate data much easier when working in SAS Viya.

    Another great tip? The Python client allows you to manipulate data on the fly, without moving or copying the data to another location. Creating computed columns allows you to speed up the wrangling of data, while giving you options for how want to get there.

    Want to learn more great tips about integrating Python with SAS Viya? Check out Kevin Smith and Xiangxiang Meng’s SAS Viya: The Python Perspective to learn how Python can be intergraded into SAS® Viya® —and help you manipulate data with ease.

    Great tip for dynamic data selection using SAS Viya and Python was published on SAS Users.

    11月 032018
     

    When you begin to work within the SAS Viya ecosystem, you learn that the central piece is SAS Cloud Analytic Services (CAS). CAS allows all clients in the SAS Viya ecosystem to communicate and run analytic methods. The great part about SAS Viya is that the R client can drive CAS directly using familiar objects and constructs for R programmers.

    The SAS Scripting Wrapper for Analytics Transfer (SWAT) package is an R interface to CAS. With this package, you can load data into memory and apply CAS actions to transform, summarize, model and score the data. You can still retain the ease-of-use of R on the client side to further post process CAS result tables.

    But before you can do any analysis in CAS, you need some data to work with and a way to get to it. There are two data access components in CAS:

    1. Caslibs, definitions that give access to a resource that contains data.
    2. CASTables, for analyzing data from a caslib resource. You load the data into a CASTable, which contains information about the data in the columns.

    Other references you may find of interest include this GitHub repository where you can find more information on installing and configuring CAS and SWAT. Also available is this article on using RStudio with SAS Viya.

    The following excerpt from SAS® Viya® : the R Perspective, the book I co-authored with my SAS colleague Xiangxiang Meng, demonstrates the way the R client in SAS Viya allows you to select data with precision. The examples come from the iris flower data set, which is available in the SASHELP library, in all distributions of SAS. The CASTable object sorttbl is sorted by the Sepal.Width column.

    Rather than using fixed values of rows and columns to select data, we can create conditions that are based on the data in the table to determine which rows to select. The specification of conditions is done using the same syntax as that used by data.frame objectsCASTable objects support R’s various comparison operators and build a filter that subsets the rows in the table. You can then use the result of that comparison to index into a CASTableIt sounds much more complicated than it is, so let’s look at an example.

    This expression creates a computed column that is used in a where expression on the CASTable. This expression can then be used as an index value for a CASTable. Indexing this way essentially creates a Boolean mask. Wherever the expression values are true, the rows of the table are returned. Wherever the expression is false, the rows are filtered out.

    > newtbl <- sorttbl[expr,] > head(newtbl) 
     
      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species 
    1          7.7         2.6          6.9         2.3 virginica 
    2          7.7         2.8          6.7         2.0 virginica 
    3          7.6         3.0          6.6         2.1 virginica 
    4          7.7         3.8          6.7         2.2 virginica

    These two steps are commonly entered on one line.

    > newtbl <- sorttbl[sorttbl$Petal.Length > 6.5,]
    > head(newtbl) 
     
      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species 
    1          7.7         2.6          6.9         2.3 virginica 
    2          7.7         2.8          6.7         2.0 virginica 
    3          7.6         3.0          6.6         2.1 virginica 
    4          7.7         3.8          6.7         2.2 virginica

    We can further filter rows out by indexing another comparison expression.

    > newtbl2 <- newtbl[newtbl$Petal.Width < 2.2,] > head(newtbl2) 
     
      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species 
    1          7.7         2.8          6.7         2.0 virginica 
    2          7.6         3.0          6.6         2.1 virginica

    Comparisons can be joined using the bitwise comparison operators & (and) and | (or). You must be careful with these operators though due to operator precedence. Bitwise comparison has a lower precedence than comparisons such as greater-than and less-than, but it is still safer to enclose your comparisons in parentheses.

    > newtbl3 <- sorttbl[(sorttbl$Petal.Length > 6.5) & (sorttbl$Petal.Width < 2.2),] > head(newtbl3) 
     
      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species 
    1          7.7         2.8          6.7         2.0 virginica 
    2          7.6         3.0          6.6         2.1 virginica

    In all cases, we are not changing anything about the underlying data in CAS. We are simply constructing a query that is executed with the CASTable when it is used as the parameter in a CAS action. You can see what is happening behind the scenes by displaying the attributes of the resulting CASTable objects.

    > attributes(newtbl3) 
     
    $conn 
    CAS(hostname=server-name.mycompany.com, port=8777, username=username, session=11ed56e2-f9dd-9346-8d01-44a496e68880, protocol=http) 
     
    $tname
    [1] "iris" 
     
    $caslib 
    [1] "" 
     
    $where 
    [1] "((\"Petal.Length\"n > 6.5) AND (\"Petal.Width\"n < 2.2))" 
     
    $orderby 
    [1] "Sepal.Width" 
     
    $groupby 
    [1] "" 
     
    $gbmode 
    [1] "" 
     
    $computedOnDemand 
    [1] FALSE 
     
    $computedVars 
    [1] "" 
     
    $computedVarsProgram 
    [1] "" 
     
    $XcomputedVarsProgram 
    [1] "" 
     
    $XcomputedVars 
    [1] "" 
     
    $names 
    [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  
    [5] "Species"      
     
    $class 
    [1] "CASTable" 
    attr(,"package") 
    [1] "swat"

    You can also do mathematical operations on columns with constants or on other columns within your comparisons.

    > iris[(iris$Petal.Length + iris$Petal.Width) * 2 > 17.5,] 
     
        Sepal.Length Sepal.Width Petal.Length Petal.Width   Species 
    118          7.7         3.8          6.7         2.2 virginica 
    119          7.7         2.6          6.9         2.3 virginica

    The list of supported operators is shown in the following table:

    Operator Numeric Data Character Data
    + (add) ✔
    - (subtract) ✔
    * (multiply) ✔
    / (divide) ✔
    %% (modulo) ✔
    %/% (integer division) ✔
    ^ (power) [✔

    The supported comparison operators are shown in the following table.

    Operator Numeric Data Character Data
    == (equality) ✔ ✔
    != (inequality) ✔ ✔
    < (less than) ✔ ✔
    > (greater than) ✔ ✔
    <= (less than or equal to) ✔ ✔
    >= (greater than or equal to) ✔ ✔

     

    As you can see in the preceding tables, you can do comparisons on character columns as well. In the following example, all of the rows in which Species is equal to "virginica" are selected and saved to a new CASTable object virginica. Note that in this case, data is still not duplicated.

    > tbl <- defCasTable(conn, 'iris') > virginica <- tbl[tbl$Species == 'virginica',] > dim(virginica) 
     
    [1] 50  5 
     
    > head(virginica) 
     
      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species 
    1          7.7         3.0          6.1         2.3 virginica 
    2          6.3         3.4          5.6         2.4 virginica 
    3          6.4         3.1          5.5         1.8 virginica 
    4          6.0         3.0          4.8         1.8 virginica 
    5          6.9         3.1          5.4         2.1 virginica 
    6          6.7         3.1          5.6         2.4 virginica

    It’s easy to create powerful filters that are executed in CAS while still using the R syntax. However, the similarities to dataframe don’t end there. CASTable objects can also create computed columns and by groups using similar techniques.

    Want to learn more? Get your copy of SAS Viya: The R Perspective

    How to use SAS® Viya® and R for dynamic data selection was published on SAS Users.

    10月 312018
     

    This article is the first in a series of three posts to address REST APIs and their use in, and with, SAS. Today, I'll present a basic example using SAS Viya REST APIs to download an image from a report in SAS Visual Analytics.

    The second article will show an example of the Cloud Analytics Services (CAS) REST APIs. My third planned article will outline show a simple application that accesses SAS Viya using both sets of REST APIs.

    The inspiration for this example: a visualization of air traffic data

    I ran across a great post from Mike Drutar: How to create animated line charts that "grow" in SAS Visual Analytics. I followed the steps in Mike's example, which creates a visualization of airline traffic. The result was an animated line chart. For this article, I removed the animation, as it will serve me better in my use case.

    SAS Viya APIs and CAS APIs: Two entry points into SAS Viya

    The first thing I'd like to cover is why SAS Viya offers two sets of REST APIs. Let's consider who is using the APIs, and what they are trying to accomplish? SAS Viya APIs target enterprise application developers (who may or may not be versed in analytics), who intend to build on the work of model builders and data scientists. These developers want to deliver apps based on SAS Viya technology -- for example, to call an analytical model to score data. On the other hand, the CAS REST API is used by data scientists and programmers (who are decidedly adept at analytics) and administrators, who need to interact with CAS directly and are knowledgeable about CAS actions. CAS actions are the building blocks of analytical work in SAS Viya.

    How to get started with SAS Viya REST APIs

    The best place to start working with SAS Viya REST APIs is on the SAS Developer's web site. There, you will find links to the API documentation.

    The REST APIs are written to make it easy to integrate the capabilities of SAS Viya to help build applications or create scripts. The APIs are based on URLs, using HTTP Authentication, and HTTP verbs. The API documentation page is split into multiple categories. The following table outlines the breakdown:

    API Category Description
    Visualization Provide access to reports and report images
    Compute Act on SAS compute and analytic servers, including Cloud Analytic Services (CAS)
    Text Analytics Provide analysis and categorization of text documents
    Data Management Enable data manipulation and data quality operations on data sources
    Decision Management Provide access to machine scoring and business rules
    Core Services Provide operations for shared resources such as files and folders

     

    The REST API documentation page is divided into multiple sections.

    SAS Viya REST API doc

    1. The categories are listed in the upper-left side.
    2. Once a you select a category, related services and functions are listed in the lower left pane.
    3. The service appears in the center pane with a description, parameters, responses, and error codes.
    4. The right pane displays how to form a sample request, any optional or required body text, and sample response code.

    The REST API call process

    The example outlined in this article shows how to access a report image from SAS Visual Analytics. To try this out yourself, you will need: a SAS Viya environment (with SAS Visual Analytics configured), an access token, and a REST client. The REST client can be cURL (command line), Postman (a popular REST API environment), or Atom with the rest-client plugin -- or any other scripting language of your choice. Even if you do not have access to an environment right now, read on! As a SAS developer, you're going to want to be aware of these capabilities.

    Get a list of reports from SAS Visual Analytics

    Run the following curl command to get a list of reports on the SAS server:

    curl -X GET http://sasserver.demo.sas.com/reports/reports\
      -H 'Authorization: Bearer &lt;access-token-goes-here&gt;' \
      -H 'Accept: application/vnd.sas.table.column+json'

    Alternatively, use Postman to enter the command and parameters:

    GET Report List API call from Postman

    From the JSON response, find the report object and grab the id of the desired report:

    GET Report List Response

    Create a job

    The next step is to create an asynchronous job to generate the SVG image from the report. I use the following HTTP POST with the /jobs verb:

    curl -X POST <a href="http://sasserver.demo.sas.com/reportImages/jobs/">http://sasserver.demo.sas.com/reportImages/jobs\
      -H 'Authorization: Bearer &lt;access-token-goes-here&gt;' \
      -H 'Accept = application/vnd.sas.report.images.job+json'\
      -H 'Content-Type = application/vnd.sas.report.images.job.request+json'

    Using the following sample Body text

    {
      "reportUri" : "/reports/reports/b555ea27-f204-4d67-9c74-885311220d45",
      "layoutType" : "entireSection",
      "selectionType" : "report",
      "size" : "400x300",
      "version" : 1
    }

    Here is the sample response:

    POST Job Creation Response

    The job creation kicks off an asynchronous action. The response indicates whether the job is completed at response time, or whether it's still pending. As you can see from the above response, our job is still in a 'running' state. The next step is to poll the server for job completion.

    Poll for job completion

    Using the 'id' value from the job creation POST, the command to poll is:

    curl -X GET http://sasserver.demo.sas.com/reportImages/jobs/f7a12533-ac40-4acd-acda-e0c902c6c2c1\
      -H 'Authorization: Bearer ' \ 
      -H ‘Accept = application/vnd.sas.report.images.job+json’

    And the response:

    GET Poll Job Creation Response

    Once the job comes back with a 'completed' state, the response will contain the information we need to fetch the report image.

    Get the image

    I am now ready to get the image. Using the image file name (href field) from the response above, I run the following command:

    curl -X GET http://sasserver.demo.sas.com/reportImages/images/K1870020424B498241567.svg\
      -H 'Authorization: Bearer ' \ 
      -H ‘'Accept: image/svg+xml'

    Postman automatically interprets the response as as an image. If you use the curl command, you'll need to redirect the output to a file.

    SAS Visual Analytics Graph for Air Traffic

    What's Next?

    SAS Visual Analytics is usually considered an interactive, point-and-click application. With these REST APIs we can automate parts of SAS Visual Analytics from a web application, a service, or a script. This opens tremendous opportunities for us to extend SAS Visual Analytics report content outside the bounds of the SAS Visual Analytics app.

    I'll cover more in my next articles. In the meantime, check out the Visualization APIs documentation to see what's possible. Have questions? Post in the comments and I'll try to address in future posts.

    Using SAS Viya REST APIs to access images from SAS Visual Analytics was published on SAS Users.

    10月 312018
     

    This article is the first in a series of three posts to address REST APIs and their use in, and with, SAS. Today, I'll present a basic example using SAS Viya REST APIs to download an image from a report in SAS Visual Analytics.

    The second article will show an example of the Cloud Analytics Services (CAS) REST APIs. My third planned article will outline show a simple application that accesses SAS Viya using both sets of REST APIs.

    The inspiration for this example: a visualization of air traffic data

    I ran across a great post from Mike Drutar: How to create animated line charts that "grow" in SAS Visual Analytics. I followed the steps in Mike's example, which creates a visualization of airline traffic. The result was an animated line chart. For this article, I removed the animation, as it will serve me better in my use case.

    SAS Viya APIs and CAS APIs: Two entry points into SAS Viya

    The first thing I'd like to cover is why SAS Viya offers two sets of REST APIs. Let's consider who is using the APIs, and what they are trying to accomplish? SAS Viya APIs target enterprise application developers (who may or may not be versed in analytics), who intend to build on the work of model builders and data scientists. These developers want to deliver apps based on SAS Viya technology -- for example, to call an analytical model to score data. On the other hand, the CAS REST API is used by data scientists and programmers (who are decidedly adept at analytics) and administrators, who need to interact with CAS directly and are knowledgeable about CAS actions. CAS actions are the building blocks of analytical work in SAS Viya.

    How to get started with SAS Viya REST APIs

    The best place to start working with SAS Viya REST APIs is on the SAS Developer's web site. There, you will find links to the API documentation.

    The REST APIs are written to make it easy to integrate the capabilities of SAS Viya to help build applications or create scripts. The APIs are based on URLs, using HTTP Authentication, and HTTP verbs. The API documentation page is split into multiple categories. The following table outlines the breakdown:

    API Category Description
    Visualization Provide access to reports and report images
    Compute Act on SAS compute and analytic servers, including Cloud Analytic Services (CAS)
    Text Analytics Provide analysis and categorization of text documents
    Data Management Enable data manipulation and data quality operations on data sources
    Decision Management Provide access to machine scoring and business rules
    Core Services Provide operations for shared resources such as files and folders

     

    The REST API documentation page is divided into multiple sections.

    SAS Viya REST API doc

    1. The categories are listed in the upper-left side.
    2. Once a you select a category, related services and functions are listed in the lower left pane.
    3. The service appears in the center pane with a description, parameters, responses, and error codes.
    4. The right pane displays how to form a sample request, any optional or required body text, and sample response code.

    The REST API call process

    The example outlined in this article shows how to access a report image from SAS Visual Analytics. To try this out yourself, you will need: a SAS Viya environment (with SAS Visual Analytics configured), an access token, and a REST client. The REST client can be cURL (command line), Postman (a popular REST API environment), or Atom with the rest-client plugin -- or any other scripting language of your choice. Even if you do not have access to an environment right now, read on! As a SAS developer, you're going to want to be aware of these capabilities.

    Get a list of reports from SAS Visual Analytics

    Run the following curl command to get a list of reports on the SAS server:

    curl -X GET http://sasserver.demo.sas.com/reports/reports\
      -H 'Authorization: Bearer &lt;access-token-goes-here&gt;' \
      -H 'Accept: application/vnd.sas.table.column+json'

    Alternatively, use Postman to enter the command and parameters:

    GET Report List API call from Postman

    From the JSON response, find the report object and grab the id of the desired report:

    GET Report List Response

    Create a job

    The next step is to create an asynchronous job to generate the SVG image from the report. I use the following HTTP POST with the /jobs verb:

    curl -X POST <a href="http://sasserver.demo.sas.com/reportImages/jobs/">http://sasserver.demo.sas.com/reportImages/jobs\
      -H 'Authorization: Bearer &lt;access-token-goes-here&gt;' \
      -H 'Accept = application/vnd.sas.report.images.job+json'\
      -H 'Content-Type = application/vnd.sas.report.images.job.request+json'

    Using the following sample Body text

    {
      "reportUri" : "/reports/reports/b555ea27-f204-4d67-9c74-885311220d45",
      "layoutType" : "entireSection",
      "selectionType" : "report",
      "size" : "400x300",
      "version" : 1
    }

    Here is the sample response:

    POST Job Creation Response

    The job creation kicks off an asynchronous action. The response indicates whether the job is completed at response time, or whether it's still pending. As you can see from the above response, our job is still in a 'running' state. The next step is to poll the server for job completion.

    Poll for job completion

    Using the 'id' value from the job creation POST, the command to poll is:

    curl -X GET http://sasserver.demo.sas.com/reportImages/jobs/f7a12533-ac40-4acd-acda-e0c902c6c2c1\
      -H 'Authorization: Bearer ' \ 
      -H ‘Accept = application/vnd.sas.report.images.job+json’

    And the response:

    GET Poll Job Creation Response

    Once the job comes back with a 'completed' state, the response will contain the information we need to fetch the report image.

    Get the image

    I am now ready to get the image. Using the image file name (href field) from the response above, I run the following command:

    curl -X GET http://sasserver.demo.sas.com/reportImages/images/K1870020424B498241567.svg\
      -H 'Authorization: Bearer ' \ 
      -H ‘'Accept: image/svg+xml'

    Postman automatically interprets the response as as an image. If you use the curl command, you'll need to redirect the output to a file.

    SAS Visual Analytics Graph for Air Traffic

    What's Next?

    SAS Visual Analytics is usually considered an interactive, point-and-click application. With these REST APIs we can automate parts of SAS Visual Analytics from a web application, a service, or a script. This opens tremendous opportunities for us to extend SAS Visual Analytics report content outside the bounds of the SAS Visual Analytics app.

    I'll cover more in my next articles. In the meantime, check out the Visualization APIs documentation to see what's possible. Have questions? Post in the comments and I'll try to address in future posts.

    Using SAS Viya REST APIs to access images from SAS Visual Analytics was published on SAS Users.

    10月 312018
     

    An important step of every analytics project is exploring and preprocessing the data.  This transforms the raw data to make it useful and quality.  It might be necessary, for example, to reduce the size of the data or to eliminate some columns. All these actions accelerate the analytical project that comes right after.  But equally important is how you "productionize" your data science project.  In other words, how you deploy your model so that the business processes can make use of it.

    SAS Viya can help with that.  Several SAS Viya applications have been engineered to directly add models to a model repository including SAS® Visual Data Mining and Machine Learning, SAS® Visual Text Analytics, and SAS® Studio. While the recent post on publishing and running models in Hadoop on SAS Viya outlined how to build models, this post will focus on the process to deploy your models with SAS Model Manager to Hadoop.

    SAS Visual Data Mining and Machine Learning on SAS Viya contains a pipeline interface to assist data scientists in finding the most accurate model.  In that pipeline interface, you can do several tasks such as import score code, score your data, download score API code or download SAS/BASE scoring code.  Or you may decide – once you have a version ready - to store the model out of the development environment by registering your analytical model in a model repository.

    Registered models will show up in SAS Model Manager and are copied to the model repository.   That repository provides long-term storage and includes version control.  It's a powerful tool for managing and governing your analytical models.  A registered version of your model will never get lost, even it's deleted from your development environment.   SAS models are not the only kind of models that SAS Model Manager can handle:  Python, R, Matlab models can also be imported.

    SAS Model Manager can read, write, and manage the model repository and provide actions for model editing, comparing, testing, publishing, validating, monitoring, lineage, and history of the models.  It also allows you to easily demonstrate your compliance with regulations and policies. You can organize models into different projects.   Within a project it's feasible to test, deploy and monitor the performance of the registered models.

    Deploying your models

    Deploying, a key step for any data scientist and model manager, can assist in bringing the models into production processes. Kick off deployment by publishing your models.  SAS Model Manager can publish models to systems being used for batch processing or publish to applications where real-time execution of the models is required.   Let's have a look at how to publish the analytical model to a Hadoop cluster and run the model into the Hadoop cluster.  In doing so, you can score the data where it resides and avoid any data movement.

    1. Create the Hadoop public destination.

    The easiest way to do this is via the Visual Interface.  Go to SAS Environment Manager and click on the Publish destinations icon:

    Click on the new destination icon:

    Important:

    10月 292018
     

    CASL is a language specification that can be used by the SAS client to interact with and provide easy access to Cloud Analytic Services (CAS).  CASL is a statement-based scripting language with many uses and strengths including:

    • Specifying CAS actions to submit requests to the CAS server to perform work and return results.
    • Evaluating and manipulating the results returned by an action.
    • Creating user-defined actions and functions and creating the arguments to an action.
    • Developing analytic pipelines.

    CASL uses PROC CAS which enables you to program and execute CAS actions from the SAS client and use the results to prepare the parameters for a subsequent action.  A single PROC CAS statement can contain several CASL programs.  With the CAS procedure you can run any CAS action supported by the server, load new action sets into the server, use multiple sessions to perform asynchronous execution and operate on parameters and results as variables using the function expression parser.

    CASL, and the CAS actions, provide the most control, flexibility and options when interacting with CAS.  One can use DATA Step, CAS-enabled PROCS and CASL for optimal flexibility and control.  CASL works well with traditional SAS interfaces and the Base SAS language.

    Each CAS action belongs to an action set.  Each action set is further categorized by product (i.e. VA, VS, VDMML, etc.).  In addition to the many CAS actions supplied by SAS, as of SAS® Viya™ 3.4, you can create your own actions using CASL.  Developing and utilizing your own CAS actions allows you to further customize your code and increase your ability to work with CAS in a manner that best suits you and your organization.

    About user-defined action sets

    Developing a CASL program that is stored on the CAS server for processing is defined as a user-defined action set.  Since the action set is stored on the CAS server, the CASL statements can be written once and executed by many users. This can reduce the need to exchange files between users that store common code.  Note that you cannot add, remove, or modify a single user-defined action. You must redefine the entire action set.

    Before creating any user-defined actions, test your routines and functions first to ensure they execute successfully in CAS when submitted from the programming client.  To create user-defined actions, use the defineActionSet action in the builtins action set and add your code.  You also need to modify your code to use CASL functions such as SEND_RESPONSE, so the resulting objects on the server are returned to the client.

    Developing new actions by combining SAS-provided CAS actions

    One method for creating user-defined CAS actions is to combine one or more SAS provided CAS actions into a user-defined CAS action.  This allows you to execute just one PROC CAS statement and call all user-defined CAS actions.  This is beneficial if you repeatedly run many of the same actions against a CAS table.  An example of this is shown below. If you would like copy of the actual code, feel free to leave a reply below.

    In this example, four user-defined CAS actions named listTableInfo, simplefreq, detailfreq, and corr have been created by using the corresponding SAS-provided CAS actions tableInfo, freq, freqTab, and correlation.  These four actions return information about a CAS table, simple frequency information, detailed frequency and tabulate information, and Pearson correlation coefficients respectively.  These four actions are now part of the newly created user-defined action set myActionSet.  When this code is executed, the log will display a note that the new action set has been added.

    Once the new action set and actions have been created, you can call all four or any combination of them via a PROC CAS statement.  Specify the user-defined action set, user-defined action(s), and parameters for each.

    Developing new actions by writing your own code

    Another way to create user-defined CAS actions is to apply user-defined code, functions, and statements instead of SAS-provided CAS actions.

    In this example, two user-defined CAS actions have been created, bdayPct and sos.  These actions belong to the new user-defined action set myFunctionSet.

    To call one or both actions, specify the user-defined action set, user-defined action(s), and parameters for each.

    The results for each action are shown in the log.

    Save and load custom actions across CAS sessions

    User-defined action sets only exist in the current CAS session.  If the current CAS session is terminated, the program to create the user-defined action set must be executed again unless an in-memory table is created from the action set and the in-memory table is subsequently persisted to a SASHDAT file.  Note: SASHDAT files can only be saved to path-based caslibs such as Path, DNFS, HDFS, etc.  To create an in-memory table and persist it to a SASHDAT file, use the actionSetToTable and save CAS actions.

    To use the user-defined action set, it needs to be restored from the saved SASHDAT file.  This is done with the actionSetFromTable action.

    More about CASL programming and CAS actions

    Check out these resources for further information on programming in the CASL language and running actions with CASL.

    How to use CASL to develop and work with user-defined CAS actions was published on SAS Users.

    10月 222018
     

    This blog post was also written by Reece Clifford.

    Who’s responsible for x, y, z sales territory? What’s the most amount of people they engaged with in a month? What type of location leads to the best response from the meeting?

    To get the complete answer to these sales team-related questions, you need to trust your data. You need to be able to cut and slice high-quality data to prepare for analytics to drive innovation in your company. With SAS Data Preparation alongside SAS Decision Manager, you can do all this. Its many features allow you to perform out-of-the-box column and row transformations to increase your data quality and build the foundations for data-driven innovation.

    This blog will discuss how you can leverage SAS Decision Manager to enrich data when preparing it through SAS Data Preparation.

    The use case

    As posed above, we want to create a SAS Data Preparation plan to map a sales person to a postcode area. We use a SAS Decision Manager rule to find the sales person for a postcode area and map the person to the address. To trigger the rule, we are going to call it from SAS Data Preparation.

    In SAS Decision Manager we import a csv file to create a Lookup Table mapping a sales person to a postcode area. Lookup Tables are tables of key-value pairs and provide the ability to access reference data from business rules.

    Next, we create a rule to map a postcode and sales person. A rule specifies conditions to be evaluated and actions to be taken if those conditions are satisfied. Rules are grouped together into rule sets. Most rules correspond to the form:

    if condition_expressions then action_expressions

    For our rule, we are going to have an incoming postcode plus a record id. The postcode is assumed to be a UK postcode. We are extracting the first two characters of the postcode and lookup the sales person from the Lookup Table that we have just imported.

    The rule outputs the sales person (representative) and the record ID. When we have tested and published the rule, it's ready to be used in a SAS Data Preparation Plan.

    In SAS Data Preparation, we load a table with address data that we want to enrich by the appropriate sales person.

    1. We need to make sure the table column names and rule input parameter names match. Therefore, we are renaming the field ADDRESS_ID to ID, as ID is the rule input name. The second rule input parameter is Postcode which is the same as in the table, therefore no action is needed.

    1. We can then call the previously-created rule in SAS Decision Manager to map a sales person to an area. This will be done by adding some CASL code to the Code node in the SAS Data Preparation plan. This is featured below with a brief explanation of the functions.
      As the rule has two output parameters, we receive only two columns when executing the code step.

    CASL Code

    loadactionset "ds2";
    action runModel submit / 
    	modelTable={name="MONITORRULES", caslib="DCMRULES"}
    	modelName="Mon_Person"
    	table= {name= _dp_inputTable, caslib=_dp_inputCaslib}
    	casout= {name= _dp_outputTable, caslib=_dp_outputCaslib};

    Parameters settings for CASL call

    modelTable name Name of the table where the rule set was published to.
    modelTable caslib Name of the caslib where the rule set was published to.
    modelName Name of the decision flow to execute.
    table name Table name of the decision flow input data.

    (Set to _dp_inputTable)

    table caslib caslib name of the table input data.

    (Set to _dp_inputCaslib)

    casout name Table name of the decision flow output data.

    (Set to _dp_outputTable)

    casout caslib caslib name of the table output data.

    (Set to _dp_outputCaslib)

     

    Decision Manager Publishing Dialogue

     

    1. We then wanted to bring back the columns from the input table. We do this through joining the table in the SAS Data Preparation Plan to the original table (again) on the rule output field ID and the tables field ADDRESS_ID.

    Conclusion

    We have answered our initial question of which sales person is mapped to which region by enriching our data in a user-friendly, efficient process in SAS Data Preparation. We can now begin to gain further insight from our data to answer more of the questions posed at the beginning of the blog to help drive innovation. This can be done through additional insight using SAS Decision Manager or functions in SAS Data Preparation in the current plan or use the output table in another plan. Ultimately, this will facilitate data-driven Innovation via reporting or advanced analytics in your organisation.

    Using SAS Decision Manager to enrich the data prep process was published on SAS Users.

    10月 052018
     

    In my earlier blog, I described how to create maps in SAS Visual Analytics 8.2 if you have an ESRI shapefile with  granular geographies, such as counties, that you wish to combine into regions. Since posting this blog in January 2018, I received a lot of questions from users on a range of mapping topics, so I thought a more general post on using – and troubleshooting - custom polygons in SAS Visual Analytics on Viya was in order. Since version 8.3 is now generally available, this post is tailored to the 8.3 version of SAS Visual Analytics, but the custom polygon functionality hasn’t really changed between the 8.2 and 8.3 releases.

    What are custom polygons?

    Custom polygons are geographic boundaries that enable you to visualize data as shaded areas on the map. They are also sometimes referred to as a choropleth maps. For example, you work for a non-profit organization which is trying to decide where to put a new senior center. So you create a map that shows the population of people over 65 years of age by US census tract. The darker polygons suggest a larger number of seniors, and thus a potentially better location to build a senior center:

    SAS Visual Analytics 8.3 includes a few predefined polygonal shapes, including countries and states/provinces. But if you need something more granular, you can upload your own polygonal shapes.

    How do I create my own polygonal shapes?

    To create a polygonal map, you need two components:

    1. A dataset with a measure variable and a region ID variable. For example, you may have population as a measure, and census tract ID as a region ID. A simple frequency can be used as a measure, too.
    2. A “polygon provider” dataset, which contains the same region ID as above, plus geographic coordinates of each vertex in each polygon, a segment ID and a sequence number.

    So where do I get this mysterious polygon provider? Typically, you will need to search for a shapefile that contains the polygons you need, and do a little bit of data preparation. Shapefile is a geographic data format supported by ESRI. When you download a shapefile and look at it on the file system, you will see that it contains several files. For example, my 2010 Census Tract shapefile includes all these components:

    Sometimes you may see other components present as well. Make sure to keep all components together.

    To prepare this data for SAS Visual Analytics, you have two options.

    Preparing shapefile for SAS Visual Analytics: The long way

    One method to prepare the polygon provider is to run PROC MAPIMPORT to convert the shapefile into a SAS dataset, add a sequence ID field and then load into the Cloud Analytic Services (CAS) server in SAS Viya. The sequence ID is mandatory, as it helps SAS Visual Analytics to draw the lines connecting vertices in the correct order.

    A colleague recently reached out for help with a map of Census block groups for Chatham County in North Carolina. Let’s look at his example:

    The shapefile was downloaded from here. We then ran the following code on my desktop:

    libname geo 'C:\...\Data;
     
    proc mapimport datafile="C:\...\Data\Chatham_County__2010_Census_Block_Groups.shp"
    out=work.chatham_cbg;
    run;
     
    data geo.chatham_cbg;
    set  chatham_cbg;
    seqno=_n_;
    run;

    We then manually loaded the geo.chatham_cbg dataset in CAS using self-service import in SAS Visual Analytics. If you are not sure how to upload a dataset to CAS, please check the %SHIMPR. The macro will automatically run PROC MAPIMPORT, create a sequence ID variable and load the table into CAS. Here’s an example:

    %shpimprt(shapefilepath=/path/Chatham_County__2010_Census_Block_Groups.shp, id=GEOID, outtable=Chatham_CBG, cashost=my_viya_host.com,   casport=5570, caslib='Public');

    For this macro to work, the shapefile must be copied to a location that your SAS Viya server can access, and the code needs to be executed in an environment that has SAS Viya installed. So, it wouldn’t work if I tried to run it on my desktop, which only has SAS 9.4 installed. But it works beautifully if I run it in SAS Studio on my SAS Viya machine.

    Configuring the polygon provider

    The next step is to configure the polygon provider inside your report. I provided a detailed description of this in my earlier blog, so here I’ll just summarize the steps:

    • Add your data to the SAS Visual Analytics report, locate the region ID variable, right-click and select New Geography
    • Give it a name and select Custom Polygonal Shapes as geography type
    • Click on the Custom Polygon Provider box and select Define New Polygon Provider
    • Configure your polygon provider by selecting the library, table and ID column. The values in your ID column must match the values of the region ID variable in the dataset you are visualizing. The ID column, however, does not need to have the same name as in the visualization dataset.
    • If necessary, configure advanced options of the polygon provider (more on that in the troubleshooting section of this blog).

    If all goes well, you should see a preview of your polygons and a percentage of regions mapped. Click OK to save your geographic item, and feel free to use it in the Geo Map object.

    I followed your instructions, but the map is not working. What am I missing?

    I observed a few common troubleshooting issues with custom maps, and all are fairly easy to fix. The table below summarizes symptoms and solutions.
     

    Symptom Solution
    In the Geographic Item preview, 0% of the regions are mapped. For example:
    Check that the values in the region ID variable match between the main dataset and the polygon provider dataset.
    I successfully created the map, but the colors of the polygons all look the same. I know I have a range of values, but the map doesn’t convey the differences. In your main dataset, you probably have missing region ID values or region IDs that don’t exist in the polygon provider dataset. Add a filter to your Geo Map object to exclude region IDs that can’t be mapped.

    Only a subset of regions is rendered. You may have too many points (vertices) in your polygon provider dataset. SAS Visual Analytics can render up to 250,000 points. If you have a large number of polygons represented in a detailed way, you can easily exceed this limit. You have two options, which you can mix and match:

    (1)    Filter the map to show fewer polygons

    (2)    Reduce the level of detail in the polygon provider dataset using PROC GREDUCE. See example here. Also, if you imported data using the %shpimprt macro, it has an option to reduce the dataset. Here’s a handy link to In the Geographic Item preview, the note shows that 100% of the regions are mapped, but the regions don’t render, or the regions are rendered in the wrong location (e.g., in the middle of the ocean) and/or at an incorrect scale.

    This is probably the trickiest issue, and the most likely culprit is an incorrectly specified coordinate space code (EPSG code). The EPSG code corresponds to the type of projection applied to the latitude and longitude in the polygon provider dataset (and the originating shapefile). Projection is a method of displaying points from a sphere (the Earth) on a two-dimensional plane (flat surface). See this tutorial if you want to know more about projections.

    There are several projection types and numerous flavors of each type. The default EPSG code used in SAS Visual Analytics is EPSG:4326, which corresponds to the unprojected coordinate system.  If you open advanced properties of your polygon provider, you can see the current EPSG code:

    Finding the correct EPSG code can be tricky, as not all shapefiles have consistent and reliable metadata built in. Here are a couple of things you can try:

    (1)    Open your shapefile as a layer in a mapping application such as ArcMap (licensed by ESRI) or QGIS (open source) and view the properties of the layer. In many cases the EPSG code will appear in the properties.

    (2)    Go to the location of your shapefile and open the .prj file in Notepad. It will show the projection information for your shapefile, although it may look a bit cryptic. Take note of the unit of measure (e.g., feet), datum (e.g., NAD 83) and projection type (e.g., Lambert Conformal Conic). Then, go to https://epsg.io/ and search for your geography.  Going back to the example for Chatham county, I searched for North Carolina. If more than one code is listed, select a few codes that seem to match your .prj information the best, then go back to SAS Visual Analytics and change the polygon provider Coordinate Space property. You may have to try a few codes before you find the one that works best.

    I ruled out a projection issue, the note in Geographic Item preview shows that 100% of the regions are mapped, but the regions still don’t render. Take a look at your polygon provider preparation code and double-check that the order of observations didn’t accidentally get changed. The order of records may change, for example, if you use a PROC SQL join when you prepare the dataset. If you accidentally changed the order of the records prior to assigning the sequence ID, it can result in an illogical order of points which SAS Visual Analytics will have trouble rendering. Remember, sequence ID is needed so that SAS Visual Analytics can render the outlines of each polygon correctly.

    You can validate the order of records by mapping the polygon provider using PROC GMAP, for example:

    proc gmap map=geo.chatham_cbg data=geo.chatham_cbg;
       id geoid;
       choro geoid / nolegend levels=1;
    run;

    For example, in image #1 below, the records are ordered correctly. In image #2, the order or records is clearly wrong, hence the lines going crisscross.

     

    As you can see, custom regional maps in SAS Visual Analytics 8.3 are pretty straightforward to implement. The few "gotchas" I described will help you troubleshoot some of the common issues you may encounter.

    P.S. I would like to thank Falko Schulz for his help in reviewing this blog.

    Troubleshooting custom polygon maps in SAS Visual Analytics 8.3 was published on SAS Users.