SAS Stored Processes and Developing Web Applications Phil Mason - - PowerPoint PPT Presentation

sas stored processes and
SMART_READER_LITE
LIVE PREVIEW

SAS Stored Processes and Developing Web Applications Phil Mason - - PowerPoint PPT Presentation

SAS Stored Processes and Developing Web Applications Phil Mason Independent SAS Consultant Contents Creating a Simple Web App using Enterprise Guide Make a report Turn it into a Stored Process Use Stored Process Web App Make


slide-1
SLIDE 1

SAS Stored Processes and Developing Web Applications

Phil Mason Independent SAS Consultant

slide-2
SLIDE 2

Contents

 Creating a Simple Web App using Enterprise Guide

Make a report Turn it into a Stored Process Use Stored Process Web App

 Make a better Web App

Add some HTML Add Java Script

 Make the best Web App

Use a Java Script framework

slide-3
SLIDE 3

Make Report

slide-4
SLIDE 4

Make a report

 Use Enterprise Guide  Build a query

slide-5
SLIDE 5

Query Builder

slide-6
SLIDE 6

Query builder

slide-7
SLIDE 7

Now you have a query

slide-8
SLIDE 8

Make Stored Process

slide-9
SLIDE 9

Make a Stored Process

slide-10
SLIDE 10

Name it

slide-11
SLIDE 11

View code

slide-12
SLIDE 12

Pick Repository

slide-13
SLIDE 13

Librefs

slide-14
SLIDE 14

Prompts

slide-15
SLIDE 15

Data Sources & Targets

slide-16
SLIDE 16

Review

slide-17
SLIDE 17

Edit Stored Process

slide-18
SLIDE 18

Stored Process

 Composed of:

A metadata component A code component

 Alternative way to create a Stored Process

Create the code with an editor In Management Console create the metadata/code

slide-19
SLIDE 19

Let’s take a quick look at Management Console

slide-20
SLIDE 20

Create using Management Console

slide-21
SLIDE 21

Name it

slide-22
SLIDE 22

Execution settings

 Select server to run it on  Specify we want to stream output and that we can produce a package  Keep code in metadata

slide-23
SLIDE 23

Edit Source Code

slide-24
SLIDE 24

Define parameters

slide-25
SLIDE 25

Run it via Enterprise Guide

slide-26
SLIDE 26

Edit a Stored Process with Management Console

 Find the one you want and double click it

slide-27
SLIDE 27

Management Console

Name & Location

slide-28
SLIDE 28

Management Console

slide-29
SLIDE 29

Management Console

slide-30
SLIDE 30

Management Console

slide-31
SLIDE 31

Management Console

slide-32
SLIDE 32

Management Console

slide-33
SLIDE 33

Management Console

slide-34
SLIDE 34

Stored Process Web Application

 Part of Integration Technologies  Allows running a stored process

From a web browser From an application such as EXCEL, MS Word

 Parameters passed on URL appear as macro variables to Stored Process

slide-35
SLIDE 35

Stored process web application

slide-36
SLIDE 36

Get a menu down left

slide-37
SLIDE 37

Run stored process from web app

slide-38
SLIDE 38

Run stored process from browser

slide-39
SLIDE 39

Run stored process from excel

slide-40
SLIDE 40

Open one to run it

slide-41
SLIDE 41

Results of run appear in EXCEL

slide-42
SLIDE 42

Add a Graph to Stored Process

slide-43
SLIDE 43

Result in web browser

slide-44
SLIDE 44

Add parameter

slide-45
SLIDE 45

Add prompt for macro parameter added

slide-46
SLIDE 46

Stored Process using parameter via prompt

slide-47
SLIDE 47

URL showing parameter passed

 http://my.server/SASStoredProcess/do? _program=%2FShared+Data%2FSASTesting %2FTest3&product_line=Children

slide-48
SLIDE 48

Enhance with HTML & JavaScript

slide-49
SLIDE 49

Make a better Web App

 Use some HTML

Gives us some more control

 Use some JavaScript

Great for adding more interactivity Allows programming to control User Interface

slide-50
SLIDE 50

Simple HTML

<html> <body> <h1>Pick a report to run</h1> <a href="http://my.server/SASStoredProcess/do? _program=%2FShared+Data%2FSASTesting%2FTest3 &product_line=Children"> Children</a><p> <a href="http://my.server/SASStoredProcess/do? _program=%2FShared+Data%2FSASTesting%2FTest3 &product_line=Clothes+%26+Shoes">Clothes & Shoes</a><p> <a href="http://my.server/SASStoredProcess/do? _program=%2FShared+Data%2FSASTesting%2FTest3 &product_line=Outdoors">Outdoors</a><p> <a href="http://my.server/SASStoredProcess/do?_program=%2FShared+Data%2FSASTesti ng%2FTest3 &product_line=Sports">Sports</a><p> </body> </html>

slide-51
SLIDE 51

HTML Menu

slide-52
SLIDE 52

Report shown in browser

slide-53
SLIDE 53

Using a form to run a Stored Process

 HTML form is great for making User Interfaces  Form tag

Make the first part of URL

 Input tag, using different type settings

Text – fields for text Password – passwords where text is hidden Radio – radio buttons Checkbox – check boxes Submit – submit buttons to execute URL using parts of form

slide-54
SLIDE 54

HTML using Form

slide-55
SLIDE 55

What’s displayed

slide-56
SLIDE 56

Writing directly from Stored Process to browser

 _webout

Reserved fileref for writing from Stored Process to browser %stpbegin macro sends output from procs to _webout %stpend stops output going to _webout Can write from a datastep to _webout

 Best to turn automatic Stored Process macros off

slide-57
SLIDE 57

New stored process to write HTML

slide-58
SLIDE 58

Stored process produces this

slide-59
SLIDE 59

Add iFrame to keep output on same page

slide-60
SLIDE 60

Now menu & output on same page

slide-61
SLIDE 61

Useful optional parameters for Stored Processes

 _odsdest – lets us choose an ODS destination for type of

  • utput produced

Switch between HTML, RTF, PDF, EXCEL, etc.

 _odsstyle – choose an ODS style  _debug – can produce the SAS log, execution times, etc.

_debug=log,time _debug=2179 … this is my favourite

slide-62
SLIDE 62

Define a dropdown menu for type of output

slide-63
SLIDE 63

Output type is added to menu

slide-64
SLIDE 64

HTML Headers

 Set headers in HTML from stored process  Indicates what browser should do  RTF  EXCEL

rc = stpsrv_header("Content-type", "application/msword") ; rc = stpsrv_header('Content-disposition','attachment; filename=test.rtf'); rc = stpsrv_header("Content-type", "application/pdf") ; rc = stpsrv_header('Content-disposition','attachment; filename=test.pdf');

slide-65
SLIDE 65

RTF output

slide-66
SLIDE 66

Define dropdown for ODS style

slide-67
SLIDE 67

Here is what 2 ODS style selections produce

slide-68
SLIDE 68

Add some checkboxes

slide-69
SLIDE 69

After report we get the log

slide-70
SLIDE 70

After log we get time

slide-71
SLIDE 71

Parts of URL for stored process call

 http://khv-sas- iis.cfsi.local/SASStoredProcess/do? _program=%2FShared+Data%2FSA STesting%2FTest3&product_line=Chi ldren &_debug=log&_debug=time

slide-72
SLIDE 72

Useful automatic macro variables

slide-73
SLIDE 73

Use automatic macro variables

 Construct a URL using:

&_srvname &_srvport &_url

slide-74
SLIDE 74

What’s good about this technique

 Easily make SAS code run from a web browser  Generate HTML, Excel, PDF or RTF easily.  Easily add parameters making it much more flexible  Build up a multiple page Web Application

slide-75
SLIDE 75

What’s not so good

 Tables and graphs don’t look as good as they could

Can improve their look with some more work within SAS

 Tables lack some functionality

Cant sort columns Slow using lots of data Table headers are not fixed, so they will scroll away if tables are long

 Only some basic widgets available for building a UI  Only basic use of mouse for selecting

slide-76
SLIDE 76

Adding some JavaScript for interactivity

 onLoad – execute when page finishes loading  onUnload – execute when page closes  onKeyPress – execute when a key is pressed  onKeyUp – execute when a key is released  onMouseOver – execute when mouse is over something  onMouseOut - … when mouse moves away  onMouseDown … when left button is pressed  onMouseUp … when left button is released  onClick … when left button is clicked  onDblClick … when left button is double clicked

slide-77
SLIDE 77

JavaScript Demo

 Lets see some of those things…  Note: not all browsers support all JavaScript (in the same way)

Try things out in all the browsers you might use

slide-78
SLIDE 78

<html> <head> <script type='text/javascript'> var popwin = null; function open_popup() { if (popwin == null) popwin = window.open( 'http://www.google.com', '', 'top=150, left=350, width=250, height=50, status=0, toolbar=0, location=0, menubar=0, directories=0, resizable=0, scrollbars=0' ); } function close_popup() { if (popwin != null) { popwin.close(); popwin = null; } } function checkKey() { var key = String.fromCharCode( window.event.keyCode); if (key == 'X') { alert( "You pressed the X key" ); } } </script>

Some added JavaScript for extra functionality

slide-79
SLIDE 79

JavaScript providing more interactivity

</head> <body onload='alert("finished loading");' onunload='alert("finished unloading");'

  • nkeypress='window.status="key pressed is: " +

String.fromCharCode(window.event.keyCode) ;'

  • nkeyup='window.status="key up"; checkKey() ;'>

Pop-up a window with information by moving over <a href='#'

  • nmouseover='open_popup(); window.status="Hovering over the link" ; return true ;'
  • nmouseout='close_popup(); window.status=" " ; return true ;'>here</a>.

<p>Pop-up a window with information by holding mouse button down <a href='#'

  • nmousedown='open_popup();' onmouseup='close_popup();'>here</a>.

<p><a href='#' ondblclick='open_popup();'>Double click to open</a>, <a href='#' onclick='close_popup();'>single click to close here</a>. <p><a href='#' style='font-size="x-large"' onmousemove='open_popup();'>Move mouse over this to open</a>, <a style='font-size="x-large"' href='#' onmousemove='close_popup();'>move

  • ver this to close</a>.

<p>Press <b>X</b> to make an alert window pop up. <p>Hold down a key to see what it is in the status bar. </body> </Html>

slide-80
SLIDE 80

Stored Process generates HTML

Some excerpts…

<form method="get" action="http://khv-sas-iis2.cfsi.local/SASStoredProcess/do?" target="content"> <select name="_program"> <option value="/Shared Data/SASTesting/cabotCollectionsDashboard">Month to Date</option> <option value="/Shared Data/SASTesting/ccDashDaily">Daily</option> <option value="/Shared Data/SASTesting/ccDashYTD">Year to Date</option> </select> <input type="checkbox" name="_debug" value="log">Show log<nbsp><nbsp><nbsp> <input type="checkbox" name="_debug" value="time">Show time taken<nbsp> <input type="checkbox" name="inline" value="1">Inline?<nbsp> <input type="submit" value="Run"><br>

slide-81
SLIDE 81

More excerpts of HTML

  • rganisationName <select name="organisationName" onchange="this.form.submit();">

<option value="Cabot Debt Purchase " selected>Cabot Debt Purchase </option> <option value="Cabot Stratford Serviced ">Cabot Stratford Serviced </option> </select> </form> <iframe name="content" height="85%" width="100%"> </iframe> </body> </html>

slide-82
SLIDE 82

Use JavaScript Libraries

slide-83
SLIDE 83

A few good JavaScript libraries

 jQuery

Write less, do more

 HandsOnTable or EasyUI

 Excel-like spreadsheet for Apps

 HighCharts

Create interactive web charts easily

 D3

Data Driven Documents

slide-84
SLIDE 84

Manipulating your web page

 jQuery makes it easy to select parts of your page and attach functionality

$(document).ready(function(){ $("button").click(function(){ $("p").hide(); }); }); $(document).ready(function(){ $("button").click(function(){ $("#div1").fadeIn(); $("#div2").fadeIn("slow"); $("#div3").fadeIn(3000); }); }); <script> $(document).ready(function(){ $("button").click(function(){ $("#div1").load("demo_test.txt"); }); }); </script> <div id=“div1"> <h2>Let jQuery AJAX Change This Text</h2> </div> <button>Get External Content</button>

slide-85
SLIDE 85

Making a better table in web browsers

slide-86
SLIDE 86

Making interactive charts

slide-87
SLIDE 87

Creating Data Driven Documents

slide-88
SLIDE 88

Hosted Libraries

 You can download JavaScript libraries and keep on your site  Or you can use a Hosted Library, such as Google’s  read about it here https://developers.google.com/speed/libraries/

<script src=“https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/ jquery.min.js”></script>

 Some host their own library

<script src=“http://d3js.org/d3.v3.min.js"> </script> <script src="http://code.highcharts.com/highcharts.js"> </script>

slide-89
SLIDE 89

How to use SAS with JavaScript objects

 HTML 5 structures your page  CSS 3 controls the look and feel  JavaScript handles the interactivity  Data typically either hard-coded or provided in JSON, CSV, TSV, …  SAS Stored Processes

Provide the data in CSV or JSON format using data steps or PROC JSON Create and customise the HTML, CSS & JavaScript Using data steps or PROC STREAM

slide-90
SLIDE 90

Use JSON with Stored Processes

slide-91
SLIDE 91

What is JSON and why use it?

 Key-Value pairs  Man and Machine readable  Nested if necessary  Used by many JavaScript objects  Is a subset of JavaScript Object Notation, which is how

  • bjects in JavaScript are built

 Sometimes XML or CSV can be used  Very flexible  can represent complex nested hierarchies of data

slide-92
SLIDE 92
slide-93
SLIDE 93

Example

{ "name": "Phil Mason", "home": { "town": "Wallingford", "phone": 1491824891, "current home": true, "ages": [ 51, 46, 18, 16, 13 ] } }

slide-94
SLIDE 94

Data Step to produce JSON

 Can run from a stored process  Write to _webout to stream to browser  Can define macros, to make it flexible and easy to repeat  Can handle very complex & nested JSON

slide-95
SLIDE 95

Example data step making JSON

data _null_ ; set sashelp.class end=_end ; if _n_=1 then put '[' ; put '{' ; put '"Name":"' name +(-1) '",' ; put '"Sex":"' sex +(-1) '",' ; put '"Age":"' age +(-1) '",' ; put '"Height":"' height +(-1) '",' ; put '"Weight":"' weight +(-1) '"' ; put '}' ; if not _end then put ',' ; else put ']' ; run ;

slide-96
SLIDE 96

[ { "Name":"Alfred", "Sex":"M", "Age":"14", "Height":"69", "Weight":"112.5" } , { "Name":"Alice", "Sex":"F", "Age":"13", "Height":"56.5",

slide-97
SLIDE 97

PROC JSON

 SAS 9.4  Easily produces JSON from SAS datasets  Can produce more complex JSON as well as simple JSON  Can use macros to write PROC JSON, making it very flexible

slide-98
SLIDE 98

PROC JSON

 Minimum code needed  Produces default output

{ "SASJSONExport": "1.0", "SASTableData+CLASS": [ { "Name": "Alfred", "Sex": "M", "Age": 14, "Height": 69, "Weight": 112.5 }, { "Name": "Alice", "Sex": " F", "Age": 13, "Height": 56.5, "Weight": 84 },

proc json out=temp ; export sashelp.class ; run ;

slide-99
SLIDE 99

Better PROC JSON code

 Pretty - lays out the JSON clearly  noSAStags - leaves out SAS metadata from the JSON

proc json out='/tmp/class.json' pretty nosastags ; export sashelp.class ; run ; [ { "Name": "Alfred", "Sex": "M", "Age": 14, "Height": 69, "Weight": 112.5 }, Produces this output

slide-100
SLIDE 100

JavaScript libraries with SAS

slide-101
SLIDE 101

PROC STREAM

 Processes text and expands macros within it  Text is sent to an external file  Can execute SAS code while processing the input stream  using %sysfunc(dosub(fileref))  Can include text into stream for processing  using %include filleref  Files can be read into stream, but macros not processed  using &streamdelim readfile filref  Newlines can be forced in output  using &streamdelim newline

slide-102
SLIDE 102

Simple example

%macro cols(n) ; %do i=1 %to &n ; <td>&i</td> %end ; %mend cols ; filename out '/folders/myfolders/test.html' ; proc stream outfile=out ; BEGIN <table> <tr>%cols(4)</tr> </table> ;;;; <table><tr><td>1</td> <td>2</td> <td>3</td> <td>4</td></tr></table>

slide-103
SLIDE 103

General Approach

slide-104
SLIDE 104

Find an example

 Pick a visualisation  Close to something you want  Could be changed to meet needs  Could be combined with other things to meet needs  Check it for suitability  Some things are easier than others  Is it free?  Handle libraries  Make sure you are pointing to a local copy or CDN  Copy & Test locally

slide-105
SLIDE 105

Number Display Example

<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/crossfilter/1.3.11/crossfilter.min.js"></script> <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/dc/1.7.3/dc.min.js"></script> <script type="text/javascript" src="../js/d3.js"></script> <script type="text/javascript" src="../js/crossfilter.js"></script> <script type="text/javascript" src="../js/dc.js"></script> http://dc-js.github.io/dc.js/examples/cust.html

slide-106
SLIDE 106
slide-107
SLIDE 107

Wrap in a Stored Process

 Write code to _WEBOUT using  Data step  Proc Stream  Mixture of both  Leave out the %stpbegin & %stpend macros  Call using Stored Process Web Application from browser

http://server57562.uk2net.com/SASStoredProcess/do?_program=%2FUse

slide-108
SLIDE 108
slide-109
SLIDE 109

Use Proc Stream with PRESCOL

slide-110
SLIDE 110

What should we change?

 Parameters  So we can control what it does  Data  So it works using our data  Libraries  Often they point to resources on another web site  Can copy to our web site  Can use Content Delivery Network (CDN)

slide-111
SLIDE 111

Parameters

 Identify anything we want to alter  If it is a one time change, then make the change  If we want to change it from run to run, then it is a candidate for macros  Identify anything we want to add, e.g. a title

slide-112
SLIDE 112

Data

 Identify where the data is coming from  What format is it in?  We will need to make our data in same format  Consider options for reproducing data

slide-113
SLIDE 113

How to change things?

 Macro variables  Replace shorter things we want to control  Macro programs  Replace longer things we want to control  Make use of looping, conditions, dataset I/O, file I/O, etc.

slide-114
SLIDE 114
slide-115
SLIDE 115

Change width and height to a macro variable

slide-116
SLIDE 116

Add corresponding parameters to Stored Process

slide-117
SLIDE 117

Use parms in call to Stored Process

http://server57562.uk2net.com/SASStoredProcess/do?_program=%2FUser+Folde

slide-118
SLIDE 118

 Can just make a flat file  Can make a stored process to deliver data  Data Step  Proc Stream

Make data into a CSV “file”

slide-119
SLIDE 119

Make a Stored Process to deliver data

slide-120
SLIDE 120

Modify Stored Process to use data from Stored Process

 Use the d3.csv function to read in CSV data into a JavaScript array.  Remove the hard-coded data  Add a call to load data  All code needs to be inside function  Finish off function call at the end with })

slide-121
SLIDE 121

Stored Process using macro parms and another Stored Process for data

slide-122
SLIDE 122

Where have we go to?

 Stored Process for Program  Creates HTML, JavaScript, CSS  Stored Process for Data  Produces a stream of CSV data  Possibly copies of libraries on our Infrastructure  CSS  JavaScript  We have a working little JavaScript/SAS Stored Process Visualisation Web Application!

slide-123
SLIDE 123

Next Steps

 Add more graphs to these 3 based on the same data  Parameterise the data used  Change variables too  Make them all Stored Process parameters  Therefore they are also macro variables  Change Stored Process making data to be general purpose by feeding a dataset name to it.  Create several Stored Processes like this and combine them to make a dashboard.

slide-124
SLIDE 124

Repeat object(s) to make a Dashboard

 We don't have time for this, but basic steps are …  Create a bunch of empty div sections in HTML 5  perhaps just with a H1 so you can see them  Make some CSS to lay them out how you like  easy to do accordions, tabs, sliding layouts, etc.  Use some jQuery to fill the div sections  use Ajax calls to do it  Possibly add some drop down selections lists, etc.  Use jQuery to add info from there to your Ajax calls  That’s about it!

slide-125
SLIDE 125

Conclusion

slide-126
SLIDE 126

Conclusions

 It’s easy to find great JavaScript visualisation software  It’s easy to wrap it up and customise it with a Stored Process  It’s easy to feed it with data from another Stored Process  Combine some of these into a dashboard or application  Put it all together and you have yourself a powerful cutting edge visualisation system.

slide-127
SLIDE 127

Extras

slide-128
SLIDE 128

Some great links to useful resources

slide-129
SLIDE 129

http://www.robslink.com/SAS/Home.htm

slide-130
SLIDE 130

http://www.visualisingdata.com/index.php/re sources/

slide-131
SLIDE 131

http://www.jsgraphs.com/

slide-132
SLIDE 132

http://www.graphviz.org/Gallery.php

slide-133
SLIDE 133

http://www.highcharts.com/demo

slide-134
SLIDE 134

http://www.amcharts.com/demos/

slide-135
SLIDE 135

https://github.com/mbostock/d3/wiki/Gallery

slide-136
SLIDE 136

http://dc-js.github.io/dc.js/

slide-137
SLIDE 137

Some great diagrams

slide-138
SLIDE 138