BookmarkSubscribeRSS Feed

Using Compute Tasks in SAS Viya: Concepts, API and Examples

Started yesterday by
Modified yesterday by
Views 91

For those of you coming from SAS 9, Stored Processes were the way to go for executing defined SAS code to bind a web application with a SAS environment. While it was possible to achieve the same using SAS Viya Jobs in SAS Viya or calling Compute Server REST APIs, the approach was not perfect due the asynchronous nature of SAS Viya. With 2025.09 stable release, SAS Viya introduces an alternative directly targeted to web developers or developers executing synchronously.

 

What Are Compute Tasks?

 

Compute Tasks in SAS Viya provide a fast, lightweight way to execute short SAS programs via a single HTTP request. Unlike the Job Execution Service (JES), which is asynchronous and resilient, Compute Tasks are synchronous and optimized for speed, making them ideal for quick, cancelable, and restartable jobs.

 

How Compute Tasks Work

 

  • Endpoint: POST /compute/tasks
  • Request: Contains a TaskRequest JSON object (or multipart form).
  • Process: The Compute service creates a session, submits the program, waits for completion, and returns the output—all in one HTTP request.
  • Response: Returns a Task JSON object (and optionally, multipart output files).

 

TaskRequest Structure

 

Field Type Description
name string Name of the task
description string Description of the task
code string SAS program to execute
arguments map[string]string Key-value pairs for variables and execution opts

 

Example:

{
    "name": "My Task",
    "description": "This is my task",
    "code": "data _null_; run;",
    "arguments": {
        "_contextName": "ReusableContext"
    }
}

 

Task Response Structure

 

Field Type Description
name string Name of the task
id string Task ID
description string Description of the task
state string State of the job (e.g., completed)
jobConditionCode int Exit code of the job
creationTimeStamp time.Time When the task was executed
elapsedTime int64 Time to execute (ms)
contextId string Context ID used
contextName string Context name used
sessionId string Session ID used
jobId string Job ID
error object Error details, if any
variables map[string]string Final values of requested variables

 

Restrictions

 

  • Timeout: Maximum 30 seconds (configurable, default is 5 seconds).
  • Short Duration: Designed for programs that finish quickly.
  • Cancelable/Restartable: Programs must be safe to terminate and rerun.

 

How to Run a Task

1. Minimal Example: Get Exit Code

 

SAS Program: 

data _null_;
call sleep(1, 10);

 

TaskRequest:

{
   "name": "Get Exit Code",
   "description": "A task that sleeps for 10 seconds.",
    "code": "data _null_; call sleep(1, 10); run;",
    "arguments": {
        "_contextName": "ReusableContext",
        "_omitJsonLog": "true",
        "_omitTextLog": "true",
        "_omitJsonListing": "true",
        "_omitTextListing": "true"
    }
  }

 

HTTP Request:

  • Method: POST
  • Endpoint: /compute/tasks
  • Content-Type: application/vnd.sas.compute.task.request+json
  • Accept: application/vnd.sas.compute.task+json

 

Response:

{ 
    "id": "...",
    "name": "Get Exit Code",
    "state": "completed",
    "jobConditionCode": 0,
    ...
}

 

2. Example: Generate HTML Output

 

SAS Program:

%global MYNAME;
data _null_;
    file _webout;
    put '<!DOCTYPE html>';
    put '<html lang="en">';
    put '<head><title>Hello World!</title></head>';
    put '<body role="main">';
    put "<h1>Hello %sysfunc(htmlencode(&MYNAME))!</h1>";
    put '</body>';
    put '</html>';
run;

 

TaskRequest:

{
    "name": "Hello World",
    "description": "A hello world program that writes an html file to _webout.",
    "code": "...", // (see above)
    "arguments": {
        "_contextName": "ReusableContext",
        "_addJesBeginEndMacros": "true",
        "_OUTPUT_TYPE": "html",
        "_omitTextLog": "true",
        "_omitSessionResults": "false",
        "_resultFilter": "eq(name,'_webout')",
        "MYNAME": "World"
    }
}

 

  • Accept: multipart/form-data

 

Response:

Multipart with:

 

  • Task JSON
  • JSON log
  • _webout.html file

 

3. Example: Provide an Input File (CSV)

 

SAS Program:

proc import datafile=&_WEBIN_FILEREF    
    dbms=csv
    out=work.mydata
    replace;
  getnames=yes;
run;

proc print data=work.mydata;
run;

 

Multipart Request:

  • Part 1: TaskRequest JSON
  • Part 2: File (e.g., cars.csv)

 

TaskRequest:

{    
    "name": "Print CSV File from Multipart Request",
    "description": "A task that prints the contents of a csv file.",
    "code": "...", // (see above)
    "arguments": {
        "_contextName": "ReusableContext",
        "_variableFilter": "contains(name,'_WEBIN_FILEREF')",
        "_omitJsonLog": "true",
        "_omitTextLog": "true"
    }
}

 

  • Accept: multipart/form-data

 

Response: Multipart with:

 

  • Task JSON (including variable values)
  • Listing in JSON and text formats

 

Advanced Options

 

  • Specify Session or Context: Use _sessionId, _contextName, or _contextId in arguments.
  • Control Output: Use _omitJsonLog, _omitTextLog, _omitJsonListing, _omitTextListing, _omitSessionResults, _resultFilter.
  • Return Variables: Use _variablesFilter (e.g., "eq(name,'myVar')" or "_ALL_").
  • Timeout: Set taskTimeout in the context definition.

 

Security and Best Practices

 

  • Input Parameters: All arguments become macro variables. Special characters are masked by default.
  • Short Tasks: For longer or resilient jobs, use JES instead.
  • Reusable Contexts: For best performance, use a context with reusable servers.

 

Conclusion

 

SAS Compute Tasks offer an alternative to SAS Viya Jobs or direct API calls to the Compute Server endpoints. It makes it really easy to execute SAS code in a single synchronous call. This is ideal for example when you want to build a web interface and populate prompts with data, or when retrieving data stored in SAS or in a database using SAS code. The real benefits comes with code that executes in a short amount of time. This is the reason why it fits nicely in the web development world. With the REST APIs for the Compute server and CAS, the SAS Viya Jobs and the Compute Tasks, you are armed to design responsive web applications in no time.

 

I will most probably write some sample web applications using the Compute Tasks. If you have ideas or topics that you would like to see covered, please don't hesitate to comment this article.

 

 

Find more articles from SAS Global Enablement and Learning here.

Contributors
Version history
Last update:
yesterday
Updated by:

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

SAS AI and Machine Learning Courses

The rapid growth of AI technologies is driving an AI skills gap and demand for AI talent. Ready to grow your AI literacy? SAS offers free ways to get started for beginners, business leaders, and analytics professionals of all skill levels. Your future self will thank you.

Get started