Monday, 25 April 2011

NOTE: Writing Your Own Functions

Did you ever hear of PROC JLAUNCH? No, neither did I until I tried the FCmp Function Editor. And, if you didn't know, the FCmp Function Editor is an interactive tool for exploring and maintaining functions created with PROC FCMP. There are a lot of good reasons for creating your own function. Read on...

I confess I dismissed the SAS Function Compiler (PROC FCMP) as nothing more than an alternative to my old, familiar macro language. A colleague of mine recently pointed out that although FCMP may be similar to macro in the functionality that it provides, it provides the functionality in a far more elegant and packaged manner. FCMP avoids the complexities of SAS macro language such as percentage signs, quoting, and the fact that all values and variables are character. FCMP offers the novice-intermediate SAS programmer an easy entry into structured programming.

You can create functions, CALL routines, and subroutines in FCMP; and those items can then be used in DATA steps, WHERE clauses, ODS statements, and selected PROCs. The details of using FCMP are in the Base SAS Procedures Guide, but I'll step you through some basics here.

Firstly, you should understand that your functions will be stored in a data set. When you define a function you specify a library, data set, and a package in which to store the function. Data sets can store more than one package, and packages can store more than one function. The following log illustrates some further basics (with explanation following the log).
15 options cmplib=work.funclib;
16
17 proc fcmp OUTLIB=work.funclib.NOTEPKG;
18   function HERBERT();
19     x=41;
20     y=42;
21     z=44;
22     return (43);
23   endsub;
24 run;
NOTE: Function HERBERT saved to work.funclib.NOTEPKG.
25
26 quit;
27
28 data lotus;
29   y=40;
30   x=herbert();
31   put _all_;
32 run;
y=40 x=43 _ERROR_=0 _N_=1
The simple example above shows a) use of the CMPLIB option to specify a search path for bespoke functions, b) variables used within the function definition are local to the definition and do not interfere with variables used in the code that called the function, and c) return values are specified with the RETURN statement.

The DATA step I/O statements (for example, INPUT, FILE, SET, and MERGE) are not available in PROC FCMP. However, some functionality of the PUT statement is supported.

I recommend that you adopt a naming standard for your bespoke functions in order to distinguish them from in-built SAS functions. This will avert much gnashing of teeth by hapless maintenance programmers later in the life of your code.

And finally, back to JLAUNCH. The FCmp Function Editor is a handy tool for helping you to create and maintain your functions. You can access it from the good, old-fashioned Display Manager interface, i.e. (on Windows) Start --> All Programs --> SAS --> SAS 9.2 (English). It'll probably prompt you to login to your metadata server, then you'll get a nice, windowed Java application to play with (hence the use of JLAUNCH).

Once inside the FCmp Function Editor, you can right-click a library, choose New Function, and then use a wizard-like interface to specify the attributes of your function, and its code. FCmp Function Editor will generate the SAS code for you (except the CMPLIB options statement) and it will check the syntax for you (you need to click View --> Show Log to see your errors). Upon clicking OK, it'll create and store the function for you. You can update the function at any time, or retrieve the full PROC FCMP code by double-clicking the function from within FCmp Function Editor.

All-in-all I'm seeing FCMP in a new light and I'm sure it'll feature in my future work. However, I will confess to being too sweeping in my earlier comparison of SAS macro language with FCMP. If you want to do stuff at the data set or PROC level you'll still need to learn macro!

Wednesday, 20 April 2011

300 Subscribers. Thank you!

Google Feedburner tells me that the NOTE: blog had 300 subscribers yesterday. This feels like a significant milestone to me. Thank you for your continued interest in my musings and observations.

At the risk of skewing the figures and compounding the apparent popularity of these posts, some of the most popular posts over the last 18 months are listed below. You'll notice how I prefix the title of SAS-related posts with "NOTE:" but don't do so for more generic topics.
The blog got started for real in September 2009. We quickly hit 100 subscribers in October 2009, and we achieved 200 subscribers in November 2010.

For those interested in statistics and figures, we have made a total of 205 posts (206 if you include this one); Sitemeter tells us that we've had a total of 9,673 visits to the home page (262 of which were last week alone). The 300 subscribers comprise 156 email subscribers plus 144 RSS subscribers.

Please keep visiting and keep commenting. And don't forget to encourage your colleagues to take a look and subscribe too: either by getting automatic emails, using RSS, or following our Twitterstream (@aratcliffeuk).

Monday, 18 April 2011

NOTE: Undocumented Features - Use Them at Your Peril!

Back in October 2009 I mentioned the undocumented feature in Enterprise Guide whereby data sets whose names begin "_TO" are hidden by EG's Server List pane. I also cautioned against using undocumented features.

SAS's Chris Hemedinger recently highlighted that this behavior has been "fixed" in SAS 9.3. You were warned!

NOTE: Are You Ready to be Taught? Test Your SAS Skills

SAS Education in the US have launched a series of online assessments to allow prospective training customers to see if they're at the right level of knowledge to join a course. Since SAS share course materials across many regions, the value of these assessments is not limited to the US.

Each assessment contains 10 to 12 multiple choice questions designed to help you determine the most appropriate training for you based on your prior knowledge and experience. Once an assessment is completed, you are given a score (including the number and percentage of questions answered correctly), and a course recommendation is made.

It's a neat way to test your SAS skills, regardless of whether you intend going on the courses. I tried "SAS Macro Language 2: Developing Macro Applications" and was relieved to get 100% of the ten questions correct! The questions are multiple choice, but they include code snippets and they make you think. Recommended.

Wednesday, 13 April 2011

NOTE: SAS Global Forum 2011 - Back-Ups, and The Last Word

My trip to SAS Global Forum 2011 in Las Vegas was valuable to me for many reasons. I learned a lot of information about a) SAS in general, and b) SAS 9.3 (to be released later this year). I echo many of the sentiments of SAS's Mike Kalt in this week's blog entry.

My hotel internet bill issue is now resolved, but my small poll of friends suggests that a significant number of those who booked accommodation through  the conference web site (and hence gained entitlement to free internet access in their hotel) were refused free use of the internet and/or had to pay. Only those who persisted with their claim through several layers of hotel management were successful, the others just didn't use the internet or they paid (twice).

Anyway, on a more positive note, I enjoyed SAS's Bubba Talley's BI 9.2 Troubleshoting paper. In addition to some valuable hints and tips about symptoms, investigations, resolutions and prevention, Bubba also spoke about SAS 9.3. According to Bubba, back-ups in 9.3 will be manageable from SAS Management Console; will be executed in a separate thread; the server will remain up and operational; configured during install; replaces %omabakup; features roll-forward recovery; and allows for ad-hoc back-ups. Nice!

And finally, all SAS Gloabl Forum 2011 papers are now available online within the SAS Support web site. In addition to Bubba's paper, you'll find mine(!) and many, many others.

NOTE: Inspiring Analytics

I was very pleased to meet SAS's Chris Hemedinger at this year's SAS Global Forum. Needless to say, he was espousing the virtues of Enterprise Guide when I found him!

If you're a regular NOTE: reader you'll know that I'm a fan of his blog. I was jolly interested to read his most recent post "Hammer time with high school students". It was a very eloquent description of how he opened the eyes of some local high school students to the meaning and power of analytics (and SAS). Analytics is a widely growing area of business interest and any school or college-leaver would do well to understand what it means (better still, to have skills in analytics). I wonder if Chris's students realise the value of the insight he's given them.

For my part, I shall be repeating the exercise with my own kids this weekend, and hammering home the message that gathering and understanding the data & information that surrounds you is a job worth doing (and worth doing well).

[It was a pleasure to meet you Chris. Keep up the good work!]