put the shortest sas® command to work for you -...

25
Put the Shortest SAS® Command to Work for You: How to Use X for Fun and Profit Mark Menzie, Assurant Health, Milwaukee, WI WI-IL SAS UG June 24, 2015 WI-IL SAS UG June 24, 2015 Milwaukee, WI 1

Upload: truongkhanh

Post on 23-Apr-2018

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Put the Shortest SAS® Command to Work for You - …wiilsu.org/LHIDsghss734gfs2HR/SUSJun2015/Proceedings/Slides/Menzie...Put the Shortest SAS® Command to Work for You: How to Use

Put the Shortest SAS® Command to Work for You:

How to Use X for Fun and ProfitMark Menzie, Assurant Health, Milwaukee, WI

WI-IL SAS UG June 24, 2015

WI-IL SAS UG June 24, 2015 Milwaukee, WI

1

Page 2: Put the Shortest SAS® Command to Work for You - …wiilsu.org/LHIDsghss734gfs2HR/SUSJun2015/Proceedings/Slides/Menzie...Put the Shortest SAS® Command to Work for You: How to Use

What is X?

A. The first letter in Xylophone?

B. A club drug?

C. A way to extend SAS capabilities using the host operating system?

D. I don’t know?

WI-IL SAS UG June 24, 2015 Milwaukee, WI 2

Page 3: Put the Shortest SAS® Command to Work for You - …wiilsu.org/LHIDsghss734gfs2HR/SUSJun2015/Proceedings/Slides/Menzie...Put the Shortest SAS® Command to Work for You: How to Use

What is X?

A. The first letter in Xylophone?

B. A club drug?

C. A way to extend SAS capabilities using the host operating system?

D. I don’t know?

WI-IL SAS UG June 24, 2015 Milwaukee, WI 3

Page 4: Put the Shortest SAS® Command to Work for You - …wiilsu.org/LHIDsghss734gfs2HR/SUSJun2015/Proceedings/Slides/Menzie...Put the Shortest SAS® Command to Work for You: How to Use

What is X?

X is a shell instruction that invokes the host operating system from within SAS. The X statement and X command are available in Windows, UNIX and Z/OS operating systems. The basic X format is:

X <'command '>;A statement is an instruction from within your program while a command is issued from the command line of an interactive session. X is valid in either context. The X instruction is executed immediately when encountered in the program flow. X opens a command window for the host operating system.

WI-IL SAS UG June 24, 2015 Milwaukee, WI 4

Page 5: Put the Shortest SAS® Command to Work for You - …wiilsu.org/LHIDsghss734gfs2HR/SUSJun2015/Proceedings/Slides/Menzie...Put the Shortest SAS® Command to Work for You: How to Use

Why use X?

The user may want to add functionality that is not supported in their current SAS installation. The X command presents one avenue through which the user can add functionality to SAS. X very conveniently simplifies and automates file management tasks.

WI-IL SAS UG June 24, 2015 Milwaukee, WI 5

Page 6: Put the Shortest SAS® Command to Work for You - …wiilsu.org/LHIDsghss734gfs2HR/SUSJun2015/Proceedings/Slides/Menzie...Put the Shortest SAS® Command to Work for You: How to Use

Alternatives to X

1. SYSTASK

2. CALL SYSTEM

3. %SYSEXEC

4. FILENAME

WI-IL SAS UG June 24, 2015 Milwaukee, WI 6

Page 7: Put the Shortest SAS® Command to Work for You - …wiilsu.org/LHIDsghss734gfs2HR/SUSJun2015/Proceedings/Slides/Menzie...Put the Shortest SAS® Command to Work for You: How to Use

X Option Settings

XCMD / NOXCMD – NOXCMD is the default setting and does not permit any use of X commands. With a setting of XCMD X commands can be used. This setting may be controlled by your system administrator.

XSYNC / NOXSYNC – XSYNC specifies that SAS operates synchronously with your operating system. Control is not returned to SAS until operating system tasks are complete. NOXSYNC allows SAS execution to continue after the X command is issued.

XWAIT / NOXWAIT – XWAIT specifies that SAS will wait for an Exit command from the operating system to continue execution. NOXWAIT allows SAS to continue after the X command is issued.

WI-IL SAS UG June 24, 2015 Milwaukee, WI 7

Page 8: Put the Shortest SAS® Command to Work for You - …wiilsu.org/LHIDsghss734gfs2HR/SUSJun2015/Proceedings/Slides/Menzie...Put the Shortest SAS® Command to Work for You: How to Use

RECUPERATION

The X command is not recuperated. This means that it does not directly return a result to SAS. As a result subsequent steps are needed if SAS is to capture the results of the X command. For example if a file is created then it can be subsequently imported.

WI-IL SAS UG June 24, 2015 Milwaukee, WI 8

Page 9: Put the Shortest SAS® Command to Work for You - …wiilsu.org/LHIDsghss734gfs2HR/SUSJun2015/Proceedings/Slides/Menzie...Put the Shortest SAS® Command to Work for You: How to Use

Using X in a Client-Server Network

WI-IL SAS UG June 24, 2015 Milwaukee, WI 9

Page 10: Put the Shortest SAS® Command to Work for You - …wiilsu.org/LHIDsghss734gfs2HR/SUSJun2015/Proceedings/Slides/Menzie...Put the Shortest SAS® Command to Work for You: How to Use

Effect of Client-Server Architecture

Because the command window opened by X is sent to the Server the client machine is disconnected from the requested process. This undermines attempts at interactivity.

WI-IL SAS UG June 24, 2015 Milwaukee, WI 10

Page 11: Put the Shortest SAS® Command to Work for You - …wiilsu.org/LHIDsghss734gfs2HR/SUSJun2015/Proceedings/Slides/Menzie...Put the Shortest SAS® Command to Work for You: How to Use

Using X to copy files

Copy all files from directory A to B

x ‘copy "C:\A\" "C:\B\"’;

WI-IL SAS UG June 24, 2015 Milwaukee, WI 11WI-IL SAS UG June 24, 2015 Milwaukee, WI

Page 12: Put the Shortest SAS® Command to Work for You - …wiilsu.org/LHIDsghss734gfs2HR/SUSJun2015/Proceedings/Slides/Menzie...Put the Shortest SAS® Command to Work for You: How to Use

Using X to move files

Move files with extension TXT from A to B:

X ‘move "C:\A\*.TXT" "C:\B\"’;

WI-IL SAS UG June 24, 2015 Milwaukee, WI 12WI-IL SAS UG June 24, 2015 Milwaukee, WI

Page 13: Put the Shortest SAS® Command to Work for You - …wiilsu.org/LHIDsghss734gfs2HR/SUSJun2015/Proceedings/Slides/Menzie...Put the Shortest SAS® Command to Work for You: How to Use

Zipping files

Package collection of files A as zip archive B:

X ‘zip "C:\A\*" "C:\B\"’;

WI-IL SAS UG June 24, 2015 Milwaukee, WI 13WI-IL SAS UG June 24, 2015 Milwaukee, WI

Page 14: Put the Shortest SAS® Command to Work for You - …wiilsu.org/LHIDsghss734gfs2HR/SUSJun2015/Proceedings/Slides/Menzie...Put the Shortest SAS® Command to Work for You: How to Use

Using X to copy files

Delete all files in directory A:

X ‘del "C:\A\*"’;

WI-IL SAS UG June 24, 2015 Milwaukee, WI 14WI-IL SAS UG June 24, 2015 Milwaukee, WI

Page 15: Put the Shortest SAS® Command to Work for You - …wiilsu.org/LHIDsghss734gfs2HR/SUSJun2015/Proceedings/Slides/Menzie...Put the Shortest SAS® Command to Work for You: How to Use

Using the %STR function

x "%str(copy %”\\Path1\A B%”

%”\\Path2\C D%”)";

%STR masks quotation marks following a % sign.

WI-IL SAS UG June 24, 2015 Milwaukee, WI 15WI-IL SAS UG June 24, 2015 Milwaukee, WI

Page 16: Put the Shortest SAS® Command to Work for You - …wiilsu.org/LHIDsghss734gfs2HR/SUSJun2015/Proceedings/Slides/Menzie...Put the Shortest SAS® Command to Work for You: How to Use

Example FILE MANAGEMENT PROGRAM-

We’ve devised a program that detects when a new period begins by the calendar, archives the contents of a working directory in a zip file and

deletes text files from the working directory.

Full program is in the paper included with conference proceedings.

WI-IL SAS UG June 24, 2015 Milwaukee, WI 16WI-IL SAS UG June 24, 2015 Milwaukee, WI

Page 17: Put the Shortest SAS® Command to Work for You - …wiilsu.org/LHIDsghss734gfs2HR/SUSJun2015/Proceedings/Slides/Menzie...Put the Shortest SAS® Command to Work for You: How to Use

Using X with other programs:

With the appropriate installation and settings it’s possible to run any program hosted by your operating system with possibilities including encryption, calculation routines in other languages, custom I/O, user redirection of the program and more.

WI-IL SAS UG June 24, 2015 Milwaukee, WI 17WI-IL SAS UG June 24, 2015 Milwaukee, WI

Page 18: Put the Shortest SAS® Command to Work for You - …wiilsu.org/LHIDsghss734gfs2HR/SUSJun2015/Proceedings/Slides/Menzie...Put the Shortest SAS® Command to Work for You: How to Use

USING X AND POWERSHELL

Powershell is the new batch file and it allows the user to initiate a sequence of Windows commands. It’s commonly used for more complex file management tasks.

WI-IL SAS UG June 24, 2015 Milwaukee, WI

18WI-IL SAS UG June 24, 2015 Milwaukee, WI

Page 19: Put the Shortest SAS® Command to Work for You - …wiilsu.org/LHIDsghss734gfs2HR/SUSJun2015/Proceedings/Slides/Menzie...Put the Shortest SAS® Command to Work for You: How to Use

ExecutionPolicy Settings:

Restricted- No scripting is allowed.

AllSigned- Any script signed by a trusted Certificate Authority will run.

RemoteSigned- Any local script and remote scripts signed by trusted Certificate Authorities can be run.

Unrestricted- Allows any script to be run.

WI-IL SAS UG June 24, 2015 Milwaukee, WI 19WI-IL SAS UG June 24, 2015 Milwaukee, WI

Page 20: Put the Shortest SAS® Command to Work for You - …wiilsu.org/LHIDsghss734gfs2HR/SUSJun2015/Proceedings/Slides/Menzie...Put the Shortest SAS® Command to Work for You: How to Use

Setting ExecutionPolicy

• Microsoft recommends that the executionpolicy setting be changed to RemoteSigned selectively only on machines that are intended to run scripts. You can change the execution policy to RemoteSigned by

• Start a session of Powershell as administrator

• Issue the command • PS C:\>Set-ExecutionPolicy Remotesigned.

• Confirm your choice

• The command Get-ExecutionPolicy returns your current execution policy setting.

WI-IL SAS UG June 24, 2015 Milwaukee, WI 20WI-IL SAS UG June 24, 2015 Milwaukee, WI

Page 21: Put the Shortest SAS® Command to Work for You - …wiilsu.org/LHIDsghss734gfs2HR/SUSJun2015/Proceedings/Slides/Menzie...Put the Shortest SAS® Command to Work for You: How to Use

Benefits of using Powershell with SAS

• Powershell allows for a sequence of operating system commands

• supports COM objects

• contains a wide variety of pre-built commands

• interactive capability

• Supports variables

• Alias for DOS and UNIX style commands like ‘DIR’, ‘MD’, ‘CD’ and ‘ls’.

WI-IL SAS UG June 24, 2015 Milwaukee, WI

21WI-IL SAS UG June 24, 2015 Milwaukee, WI

Page 22: Put the Shortest SAS® Command to Work for You - …wiilsu.org/LHIDsghss734gfs2HR/SUSJun2015/Proceedings/Slides/Menzie...Put the Shortest SAS® Command to Work for You: How to Use

Syntax for calling Powershell with X

X ‘PSpath\powershell.exe <Powershell argument>’;

22WI-IL SAS UG June 24, 2015 Milwaukee, WI

Page 23: Put the Shortest SAS® Command to Work for You - …wiilsu.org/LHIDsghss734gfs2HR/SUSJun2015/Proceedings/Slides/Menzie...Put the Shortest SAS® Command to Work for You: How to Use

AWKNOWLEGEMENTS

• Much of the creative inspiration for this paper came from my friend and colleague Irv Snider.

WI-IL SAS UG June 24, 2015 Milwaukee, WI

23WI-IL SAS UG June 24, 2015 Milwaukee, WI

Page 24: Put the Shortest SAS® Command to Work for You - …wiilsu.org/LHIDsghss734gfs2HR/SUSJun2015/Proceedings/Slides/Menzie...Put the Shortest SAS® Command to Work for You: How to Use

REFERENCES

Using Windows PowerShell to report on your SAS Enterprise Guide project files from The SAS Dummy by Chris Hemedinger

Better Ways to Speak to Your System Using SAS:

Automate Routine Tasks by using X, SYSTASK & FILENAME

By Ranganath Bandi and Harini Kunduru

Windows Powershell Programming for the Absolute Beginner by Jerry Lee Ford, Jr.

WI-IL SAS UG June 24, 2015 Milwaukee, WI

24WI-IL SAS UG June 24, 2015 Milwaukee, WI

Page 25: Put the Shortest SAS® Command to Work for You - …wiilsu.org/LHIDsghss734gfs2HR/SUSJun2015/Proceedings/Slides/Menzie...Put the Shortest SAS® Command to Work for You: How to Use

LEGAL NOTICE

SAS is a registered trademark of SAS Institute Inc. in the USA and other countries. Other trademarks are property of their respective owners.

WI-IL SAS UG June 24, 2015 Milwaukee, WI 25WI-IL SAS UG June 24, 2015 Milwaukee, WI