Take control of ODS results in SAS Enterprise Guide

When you run a program or task in SAS Enterprise Guide, the application wraps your job in an "ODS sandwich", the colloquial term we use for the ODS statements necessary to create output that can be viewed in your project.

That's convenient for exploring and refining your program, but at some point you might want to use your own ODS statements and options to control the format and appearance of the output. You might find that the default ODS sandwich that SAS Enterprise Guide generates can get in your way.

For example, suppose that you want to create a PDF output with landscape orientation and Legal paper size. The default SAS Enterprise Guide options might be in conflict with some of the options you need. The best approach? Squelch the "automatic" options, and code up your own exactly how you need them. Here's how.

First, change the properties of your SAS program.. Right-click on the program node in your flow and select Properties, or click the Properties icon at the top of the editor window:

codebar

In the Properties window, select the Results tab. Then select the Customize results formats [...] option, and uncheck all of the built-in formats.

resultsopts
Next, add the ODS statements and options necessary to produce the results that you want. For example, if you want a PDF file that has a particular format, use something like this:

options orientation=landscape papersize=legal;
ods noproctitle;
 
/* using ID= to control each ODS dest */
ods pdf (id=custom) 
  /* this file path is on the SAS workspace */
  file="C:\Projects\report.pdf" 
  columns=2 style=journal;
  title "Types by Cylinders and MSRP stats";
 
proc freq data=sashelp.cars;
  table Type * Cylinders;
run;
 
proc means data=sashelp.cars;
  class Origin;
  var msrp;
run;
 
/* print-friendly image options */
ods pdf (id=custom) dpi=300 startpage=no;
ods graphics / 
  noborder scale=on
  height=2in width=2.5in;
title "Distribution";
 
proc sgplot data=sashelp.cars;
	histogram msrp;
	density msrp;
run;
 
ods pdf (id=custom) close;

Sample result:
Link to PDF
Some notes about this program:

  • The FILE= option on the ODS PDF statement refers to the file that you want to create in the file system of your SAS workspace, not your local machine. SAS Enterprise Guide will offer to download this file for you to view, but if you want complete control over where it lands on your local PC, use the Copy Files task to download it.
  • If you run this program in SAS Enterprise Guide without turning off the other Results formats, the final PDF output won't have all of the attributes you expect. For example, the graph might look slightly different. So if this PDF is your ultimate objective, be sure to suppress the automatic results from SAS Enterprise Guide.
  • Use this same technique to create custom HTML output, or ODS POWERPOINT (new in 9.4), ODS EPUB (new in 9.4) or even ODS HTML5 (hey! new in 9.4).

For a minimalist, fast-running SAS program, you can use this technique to suppress the fancy (bulkier) ODS results and rely on the old-school LISTING format. In the Results properties for your program, uncheck all output destinations except for Text. If your program generates only text-based output that you want to view quickly, this will turn SAS Enterprise Guide into a speedy-quick results-generating machine...just like you might be accustomed to from your old PC SAS days.

See also

Destination Known: Programmatically Controlling Your Output in SAS Enterprise Guide, by Aaron Hill
[VIDEO] New Destinations in ODS: PowerPoint, HTML5, EPUB, EXCEL
Tip Sheet for ODS PDF

Post a Comment

Frankenstein hackey-sacking with a missile

I'm in the grocery store with a "mental" list of 3 items to pick up: bread, eggs, and something unusual like cupcake liners. My phone rings. It's my wife (or sometimes a daughter) asking me to pick up one or two more things, maybe orange juice and bananas. Suddenly, the entire list vacates my mind, as if a gate was left open. I will leave the store having forgotten something.

That happens to me all of the time. I now ask my family to simply text the grocery list to my phone so that I have it for reference (as long as I remember to look at my phone). But according to Nelson Dellis, memory expert and 2014 USA Memory Champion, that's not the way to improve my memory.

chris (and sara) and nelsonInstead, I should be training my brain to remember more things, perhaps using interesting and vivid images to help create a "memory palace" that I can easily traverse and retrieve memories as I need them. That's how Nelson developed his extraordinary memory performance, which allowed him to achieve feats such as memorizing pi to 1000 digits. ("Frankenstein hackey-sacking a missile" was one of his example memory anchors.)

Nelson proved his memory prowess in front of thousands of SAS users at SAS Global Forum, and I helped him to do it. My part was to point at a digit in the middle of the 1000-digit sequence, then read the 10-digit sequence from that point. Nelson would then recite the 10 digits immediately following my sequence, and then the 10 digits preceding.

On his first attempt, Nelson came up empty. It might have had to do with the way I read the digits (hey, it's harder than it looks!) or perhaps it was just dumb luck. But we tried again, and success! Then another audience member read a sequence, and Nelson rattled off the 20 surrounding digits. We were all amazed.

nelsonandmeNelson was an excellent sport throughout the rest of the day. Our SAS internal communications folks had him memorizing employee names, reciting song lyrics after having just one minute to review them, and playing a kid's matching card game. He performed admirably.

We caught up later in the Demo and Support Center, and he assured me that he held no hard feelings for my part in his missed pi performance (as he redeemed himself anyway, to much applause from the crowd). As you can see from this photo of us, he's a tall dude. (For comparison, I'm 6'1" and change.) I was pleased to see Nelson get some very positive press recently on Mashable, where you can read more about his background, his personal story, and his memory techniques.

See also:

Karin Reed interviews Nelson Dellis on SAS Global Forum Live Report
Lisa Arney interviews Nelson for Inside SAS Global Forum
Mashable: Man With World's Strongest Memory Crusades Against Alzheimer's

Post a Comment

Confessions of a SAS Dummy

SAS Global Forum 2014 was a ton of fun, and extremely busy for yours truly. If you wonder how I spent my time at the conference, you need only to visit the on-demand video archive and see how many of the various sessions feature my shiny head.

In most of these videos I'm a bit player, acting only as a host while some smarter people share the interesting demos and insights. But I do have one video that's all my own. And it features content that I "test marketed" on this very blog. The session is called "Confessions of a SAS Dummy".

Description from the conference schedule:

People from all over the world are using SAS analytics to achieve great things, such as to develop life-saving medicines, detect and prevent financial fraud, and ensure the survival of endangered species. Chris Hemedinger is not one of those people. Instead, Chris has used SAS to optimize his baby name selections, evaluate his movie rental behavior, and analyze his Facebook friends. Join Chris as he reviews some of his personal triumphs over the little problems in life, and learn how these exercises can help to hone your skills for when it really matters.

This presentation is a reprise of talk that I've given at several user groups, including as a keynote feature in two regional SAS user group conferences last fall. Now, since it's recorded and viewable by anyone at any time, I can probably never give it again. I guess I'll have to find some other topics to milk for my in-person appearances.

I've had friends who watched the video (and I didn't even have to pay them!) and said (with a hint of pity), "I enjoyed the talk, but the audience didn't seem to be all that entertained." Well, I was there and I can tell you that my audience was very good, and I was pleased with their feedback. Unfortunately, unlike at the taping of a sitcom episode, the audience is not miked so you can't hear their reaction as I speak. You can hear their questions at the end only because the questioners use a microphone for that part of the session.

Perhaps I'll petition the video editing team add a laugh track. If you're feeling charitable, feel free to leave some chuckles in the blog comments.

Archived recording: [VIDEO] Confessions of a SAS Dummy

Post a Comment

Talking Tech at SAS Global Forum 2014

Talking tech with Nancy R. at SASGF13

For the third year now, I'll be hosting the SAS Tech Talk shows at SAS Global Forum. (Since I've been invited back I can assume that I'm more of an Ellen DeGeneres than a Seth MacFarlane.)

These shows feature SAS technical experts (mostly from SAS R&D) who are prepared to discuss and demonstrate the technologies that they work on. These folks have job titles such as "Chief Data Scientist", "Principal Research Statistician", and "Senior Analytical Consultant". They work in areas such as "SAS Output Delivery and Reporting", "BI Data Visualization", and "Enterprise Management Integration".

These guests represent the SAS R&D talent that bring the SAS products to life. Some will be names that you know from books and blogs, such as Rick Wicklin. Others are return guests to SAS Tech Talk, such as Nascif and Himesh. And still others might be faces that are new to you, but they are the movers and shakers when it comes to SAS technology.

This year we have three SAS Tech Talk shows:

  • Monday, 24March at 2pm EDT
  • Tuesday, 25March at 2pm EDT
  • Wednesday, 26March at 9am EDT

Each show is different, and we're covering 12 great topics in all. I'm very excited about this year's lineup! I'd list out the topics right here, but some are the subject of exciting product announcements and I don't want to steal anyone's thunder!

There is no doubt that you get the best conference experience when you can attend in person. But even if you cannot attend this year, you don't have to miss out. Much of the content -- the opening sessions, SAS Tech Talk, and many breakout sessions -- will be streamed on live video channels, all hosted at www.sasglobalforum.com. Any content that is streamed will also be archived so that you can watch it later (can you say "movie night"?).

See also
Previous SAS Tech Talk shows on YouTube

Post a Comment

Adding some culture to your SAS Enterprise Guide prompts

My teenage daughter is a self-appointed anglophile. She's a big fan of British movies and TV shows such as Doctor Who and Sherlock, and although she has not yet visited the UK (an injustice for which she blames her father), she considers the place to be her homeland.

In an effort to live out her adopted British lifestyle, she changes her iPhone and laptop settings to "English - British". It's an innocuous preference, usually. However, it does occasionally create a problem, such as when she accidentally turns in schoolwork with British spellings for words such as "honour" or "colour". (As the famous quote from George Bernard Shaw points out, we are two nations divided by a common language.) Fortunately, her teachers sympathize (or "sympathise") and don't usually deduct points for these "misspellings".

SAS Enterprise Guide (and other SAS applications) are translated into many languages, but for these apps "English is English", and there is no special affordance for those users in the UK or Australia. For these users, running SAS Enterprise Guide in English means running it as "English - United States". Even though the language is the same, there are a few local customs, such as the format of date values, that can confuse end users.

These differences are evident in a few places, including in the prompt dialog for date values. When running in "English - United States" mode, a "Date"-type prompt looks like this:

The display value is USA-style, but more confusing is what happens when you try to enter a date value. Even though the prompt window displays the current value with the full month name, the user can enter date values by using a variety of different value styles. In the USA, we usually enter dates as "MM/DD/YYYY", where "02/10/2014" is interpreted at February 10, 2014. But a British or Australian user, accustomed to "DD-MM-YYYY" formatting, might expect that value to come out as October 2, 2014.

However, you can convince SAS Enterprise Guide to observe the local customs, and here's how. Change your SAS Enterprise Guide shortcut to add a "/culture" argument, as in:

"c:\Program Files\SASHome\x86\SASEnterpriseGuide\6.1\SEGuide.exe" /culture:en-AU

The above example is for Australia. For British, try adding "/culture:en-GB". Here's the same date prompt with the new setting applied:

Any date value that you enter will be interpreted in the "Australian way" instead of the American way. This technique works for any version of SAS Enterprise Guide, 4.2 and later.

If you don't want to rely on a modified shortcut, you can set your "language" preference in Tools->Options. But first, you need to "fool" SAS Enterprise Guide into thinking that you have installed these additional English variations. Here's how.

  1. Using Windows Explorer, navigate to the installation directory for SAS Enterprise Guide.
  2. In the application folder, create a new empty folder with a name that matches the culture you want. For example, "en-GB" for British or "en-AU" for Australia.

    You can find the complete set of "culture names" on the Microsoft developer site.

When you restart the application, you will find new "language" options available on the list:

Select the one you like, restart the application, and immerse yourself in a new culture.

Post a Comment

SAS Talks: Favorite SAS Enterprise Guide Tricks

Last week's SAS Talks session, My Favorite SAS Enterprise Guide 5.1 Tricks, was remarkable in several ways.

First, the featured presenter was Marje Fecht, who also serves as the conference chair for SAS Global Forum 2014. She's an avid SAS professional who loves to educate her fellow SAS users on the best productivity tips and practices, including the use of SAS Enterprise Guide. As Marje points out, SAS Enterprise Guide is a primary topic of at least 20 presentations at the upcoming conference. You can see this for yourself in the easy-to-use conference scheduler, which allows you to slice-and-dice the agenda in many ways, including by SAS product.

The second remarkable thing about this SAS Talks session was the attendance. The number of connected participants was through the roof. It's a testament to Marje's star power and to the popularity of the topic. Did you miss it? The archive recording is available here and is free to view.

There was one more remarkable aspect to the session that I hope most attendees did not notice. On the day of the SAS Talks event, SAS headquarters was closed due to inclement weather in North Carolina. Aside from Marje (who was in sunny Florida), the session hosts and panelists were all snowbound in our respective homes. With an ice storm in the forecast, we hoped that our power and Internet connections would sustain us. And fortunately, they did.

I-Kong Fu, the product manager for SAS Enterprise Guide, followed Marje's presentation with a short demo of SAS Enterprise Guide 6.1 and a glimpse into some future developments for the next release of the product. While Marje and I-Kong were "on stage", my colleague Mark Allemang and I addressed many of the technical questions that participants submitted using the Q&A feature. In the remainder of this post, I've grouped the questions by topic and summarized the answers.

About the Data Explorer

Marje presented a thorough review of the interactive Data Explorer. According to Marje, the Data Explorer replaces a number of manual coding steps that analysts typically perform as they get acquainted with their data.

Q: Can I use the Data Explorer with WORK data sets?
A: The Data Explorer uses a special pool of utility SAS sessions (Workspaces) to perform its operations. Since these sessions can't share the WORK folder from your primary SAS session, the Explore option doesn't appear for WORK data. However, here's a trick... Submit this code within a SAS program node:

libname explorer &sasworklocation;

This will assign a new library (named "EXPLORER" in this case) to the same physical path as WORK, and you should be able to use the Data Explorer from that library.

Q: I don't see the Explore option on the menu, even for non-WORK data. Why not? Am I cursed?
A: Don't panic. Check whether you have the option "Automatically open data when added to project" disabled (in Tools->Options->Data General). If you disable that option, the Explore data feature becomes...hidden. See Problem Note 48487 for details.

Q: Can I use Data Explorer with DBMS tables in SAS/ACCESS libraries? Very large tables?
A: Yes, you can use this feature with any table in a library that you access with SAS. Behind the scenes, the Data Explorer runs PROC SQL and other descriptive-oriented SAS procedures to summarize data and produce "Quick Stats". Larger tables might take longer to generate results, but there isn't a hard limit on the size. You can configure the behavior of automatic Quick Stats to a maximum number of columns (default: 20) or data set rows (default: 10,000). See the SAS Enterprise Guide online help for more details.

When using a DBMS, your database administrator might have some opinions about how/when you should use this interactive exploration tool. A SAS administrator can limit this feature (using metadata roles/capabilities) if desired.

About the SAS Enterprise Guide projects and process flows

Marje showed how she was able to be more productive by organizing her work into projects, using process flows, built-in tasks, and notes. And I-Kong showed the new "sticky-note" style of process flow notes in the 6.1 version.

Q: When you create a Note in the process flow and then export a SAS program that you link to it, does the content appear as comments in the exported program?
A: No. The Note object isn't exported as part of the code, as it really annotates the process flow and not just the program. Using the Note object doesn't "excuse" you from adding useful comments in your code!

Q: In the SAS Enterprise Guide workspace, can you divide up your windows by monitor (if using multiple displays)?
A: No, not currently. Most of the SAS Enterprise Guide windows are constrained to the main application window, and cannot "float" to other monitors. This is something that the R&D team is exploring for a future release.

Q: When you select "Send to Excel" for a data set, is there a way to access the SAS program for that operation? Does it use SAS/ACCESS to PC Files?
A: The "Send To" mechanism does not actually use SAS code to do the work -- which is why you don't need the SAS/ACCESS product for this. Instead, SAS Enterprise Guide automates your local instance of Microsoft Excel and populates a new spreadsheet with the selected data set. This is just one of many ways to get your SAS content to Excel.

Q: Why doesn't the Query Builder task discover the table relationships from the database schema when adding two tables from the same database?
A: The relationships aren't always apparent from a SAS/ACCESS library. Right now, efficient queries do require some education on the part of the end user to know what keys/indexes to leverage. A database administrator can help users by defining SQL views that comprise the most commonly requested combinations.

About SAS programming


The program editor in SAS Enterprise Guide is syntax-aware, and provides keyword suggestions as well as automatic display of SAS library names, data set names, and even variable names as you type. It also provides a very handy code formatter that makes your programs (or those you "inherit" from colleagues) much more readable.

Q: Does the "autofill" work with the WHERE statement?
A: Yes (see picture). You can also call up a list of variables in the active data set with the Ctrl+Shift+V key combination. A full list of keyboard shortcuts is in the SAS Enterprise Guide online help (search the index for "keyboard shortcuts").

Q: Can I run just a selection of SAS code?
A: Yes. In the program editor, you can select the lines that you want to run, right-click and select Run Selection on (server) (or press F3 as a shortcut). Also, in a process flow you can select multiple program nodes (Ctrl+click), then right-click and select Run to run just those nodes. And don't forget about the File->New->Ordered List feature to create a list of program nodes to run in a prescribed sequence.

Q: Can I rely on all code being saved within a project?
A: You can decide: save the code in the project file (embedded) or store it on a file system as .SAS files. In the latter case, the SAS Enterprise Guide project holds a shortcut reference to the program file(s). And, you can mix and match -- with some embedded, some external files.

About SAS program versioning

I-Kong demonstrated some new features that are currently under development. One feature that generated much discussion was built-in "SAS program versioning", which keeps track of your SAS program revisions over time. I-Kong showed how you will be able to access the program history and compare differences over time. If this sounds interesting to you, take the time to watch the demonstration in the recorded SAS Talks session.

Q: Do I have to license WinMerge to see the SAS program differences?
A: No. WinMerge is free and that's what I-Kong showed, but you can use any file comparison/merge tool (for example, Beyond Compare or WinDiff).

Q: Is the program history linear or tree-structured?
A: In the history window you see it as a linear progression. However, you can go back to any "snapshot" in time and bring it back as the "current version" if you want to recover to a particular point. Under the covers, SAS Enterprise Guide is using a Git repository -- but no additional setup is needed on your part.

You'll hear more about this and other future developments at SAS Global Forum 2014, which now brings me to the final action item...

Don't stop learning: attend #SASGF14

Have you made your plans for SAS Global Forum? If not, it's the perfect time to take advantage of the Presidents Day Special. Through February 21, pay for one registration and you'll receive a discount code for 50 percent off of a second registration (both at the lower early-bird rates).

Post a Comment

Using Microsoft Visual Studio 2013 Express to debug custom tasks

It's true: you can use the Microsoft Visual Studio Express tools to build custom tasks for SAS Enterprise Guide and the SAS Add-In for Microsoft Office. And I said as much in my book, Custom Tasks for SAS Enterprise Guide Using Microsoft .NET. I even provided step-by-step instructions for how to get started with your first custom task.

The advantage of the Express editions is that they are free. You can download them from Microsoft's developer site and use them free-and-clear for 30 days. After 30 days, you must "sign in" to the tool with a Microsoft account, but the account is free too.

In my book, I also provided a stern caution about using the Express editions. While you could use the Express tools to build a .NET code library (DLL file) -- necessary for a custom task -- you had very few options for debugging it. The integrated debugger in the Express edition did not allow you to debug into an external application -- anything that you didn't build in the tool. Nor did the debugger offer the option to Attach To Process, which would have allowed you to start SAS Enterprise Guide, get your task loaded in, and then attach a debugging session to hit your breakpoints. And believe me, you will need the debugger.

With Microsoft Visual Studio Express 2013 the debugger integration has improved. You still can't "launch" an external application, but you can attach the debugger to an external process. And that's good enough to get the job done for custom task developers. (And remember -- even though my book didn't mention it -- you can use Visual Studio 2013 to build custom tasks for SAS Enterprise Guide 4.3 and later.)

To use Microsoft Visual Studio Express 2013 to debug your custom task:

Before you begin, make sure the PDB file (debug symbols file) is copied to the same directory as your custom task DLL. This will allow the debugger to sync up and find breakpoints.

With your task DLL in place (in one of the designated Custom folders where SAS Enterprise Guide searches), start SAS Enterprise Guide.

Then, with your task project open in Visual Studio, select Debug->Attach to Process.

In the Attach to Process window, find and select SEGuide.exe. (If you're working with the SAS Add-In for Microsoft Office, then the process you want will be Excel.exe, Winword.exe, or Powerpnt.exe.)

If you have breakpoints set in the debugger, you should find that they are hit (assuming that your task is working properly at that point). You should be able to view the contents of local and class variables in a Watch window. You can set debug options to "break on exception" to find tricky issues. In my book, I dedicate an entire chapter to debugging techniques that work especially well with custom tasks.

Obtaining the Express edition of Visual Studio

Here are some general steps to download the Express edition. I didn't provide exact links as I find those are subject to change, but this general navigation should get you there.

  • Visit VisualStudio.com, and select Downloads.
  • Look for the "Express" section; that's where you'll find the Express downloads. (Microsoft also offers trial periods for the professional editions -- if you want to give those a go.)
  • There are a few different flavors: one for Web, one for Windows 8, and one for Desktop.
    The specific version you want is Microsoft Visual Studio Express 2013 for Windows Desktop.

I still recommend the professional edition of Microsoft Visual Studio, especially if you're planning to build multiple custom tasks and maintain them over the long haul. Use the Express edition to experiment and dabble, and to explore whether custom tasks are a viable solution for your business problems. But if you continue with custom tasks, invest in the professional version. The professional editions provide even better debugging features, plus many productivity features that you'll appreciate, such as refactoring techniques, integrated profiling, unit testing, and more.

See also

SAS custom tasks: using Microsoft Visual Studio 2013 and other new toys
11 super-useful custom tasks for SAS Enterprise Guide
Custom tasks for SAS Enterprise Guide: Q&A

Post a Comment

Adding "sticky notes" in your SAS Enterprise Guide projects

This isn't the first blog post to laud the "Note" feature of SAS Enterprise Guide. You know, the feature that allows you to add documentation to describe what the heck your project or process flow is actually doing.

For example, Tricia described 3 useful ways to use the note feature. AnnMaria says it's a "godsend" for her, given her continuously interrupted life. Even I've mentioned it before within my advice about organizing your projects.

Your colleagues (or more likely, your future self) will thank you for using notes.

SAS Enterprise Guide 6.1 takes "Notes" to the next level. In a direct response to customer requests, the notes can now be arranged in your process flow just like the sticky notes on your desk. You can position them where you want, pin them, and resize them so you can see more content. They're beautiful.

Here's an example of the new look:

When you open an older project file in SAS Enterprise Guide 6.1, you might not see the new note style right away. That's because the conversion process doesn't want to disrupt your project's appearance without your permission. To expand an "old-style" tiny note into the more useful sticky note, right-click on the note and select Expand:

Would you like to learn more about new SAS Enterprise Guide features, plus glean real-world SAS Enterprise Guide tips from Marje Fecht (yes, that Marje -- SAS Global Forum 2014 chair)? Tune in next week for My Favorite SAS Enterprise Guide 5.1 Tricks, a free webinar in the SAS Talks series. Marje is the titled "My" person sharing the tips, and then I-Kong Fu will join her to showcase the 6.1 release.

Post a Comment

Reading and updating ZIP files with FILENAME ZIP

In a previous post, I shared an example of using ODS PACKAGE to create ZIP files. But what if you need to read a ZIP file within your SAS program? In SAS 9.4, you can use the FILENAME ZIP access method to do the job.

In this example, let's pretend that I need to analyze data that a government agency published (maybe by using SAS!) into a ZIP file. I've selected an exciting data source (found via data.gov) about Large Truck Crash Causation.

First, I need to download the latest version of the data file. I'll use PROC HTTP to do that job:

/* detect proper delim for UNIX vs. Windows */
%let delim=%sysfunc(ifc(%eval(&sysscp. = WIN),\,/));
 
/* create a name for our downloaded ZIP */
%let ziploc = %sysfunc(getoption(work))&delim.datafile.zip;
filename download "&ziploc";
 
/* Download the ZIP file from the Internet*/
proc http
 method='GET'
 url="http://ai.fmcsa.dot.gov/ltccs/Data/TEXT/Public/LTCCS_db_txt_public_01.zip"
 out=download;
run;

Next, I need to discover what files are within the ZIP file. I'll assign a fileref using the new FILENAME ZIP method. FILENAME ZIP is a directory-based access method, similar to the CATALOG access method or to using FILENAME to map to a folder. You can use functions such as DOPEN and DREAD to treat the ZIP file as if it's a file directory (since that's what it is, in concept).

/* Assign a fileref wth the ZIP method */
filename inzip zip "&ziploc";
 
/* Read the "members" (files) from the ZIP file */
data contents(keep=memname);
 length memname $200;
 fid=dopen("inzip");
 if fid=0 then
  stop;
 memcount=dnum(fid);
 do i=1 to memcount;
  memname=dread(fid,i);
  output;
 end;
 rc=dclose(fid);
run;
 
/* create a report of the ZIP contents */
title "Files in the ZIP file";
proc print data=contents noobs N;
run;

Here's the report of files within the ZIP archive:


I've identified the HAZMAT.TXT file as the one that I want to analyze. I peeked at the first couple of records and was able to scratch out a simple DATA step to read the data. Notice how I don't need to explicitly extract the HAZMAT.TXT file -- I can simply reference it as a "member" of the INZIP fileref. The ZIP access method does the rest.

/* Import a text file directly from the ZIP */
data hazmat;
 infile inzip(hazmat.txt) 
   firstobs=2 dsd dlm='09'x;
 input 
  CaseID $10.
  VehicleNumber 
  Material 
  Reportable 
  Waiver 	
  PSU	 
  PSUStrata	
  RATWeight;
run;
 
title "Box plot of Vehicles # per incident";
ods graphics / height=200 width=450;
proc sgplot data=hazmat;
	hbox vehiclenumber;
	label VehicleNumber="# of vehicles";
	xaxis labelattrs=(size=12) valueattrs=(size=12);
run;

SAS reads my data file successfully, and yields this interesting box plot from the SGPLOT step:


(It looks like most "hazardous materials" accidents involved just 2 or 3 vehicles, except for one messy outlier that had nearly 30. Imagine the cleanup effort on that one!)

As an alternative, if I know exactly which file I need, I can assign a direct fileref by using the MEMBER= syntax:

filename inzip zip "&ziploc" member="hazmat.txt";
 
/* then my INFILE references the file directly, no parenthesized-member */
data hazmat;
 infile inzip
   firstobs=2 dsd dlm='09'x;
/* ...  */

The ZIP access method isn't just for reading. I can also use it to create and update ZIP files. For creating ZIP files, I prefer to use ODS PACKAGE. But it's very handy to be able to update ZIP files from a SAS program without using an external tool. For example, here's a program that deletes an extraneous file from an existing ZIP file:

/* Remove the PackageMetadata piece that ODS PACKAGE creates */
filename pkg ZIP "c:\projects\filenamezip\new.zip" member="PackageMetaData";
data _null_;
 if (fexist('pkg')) then 
  rc = fdelete('pkg');
run;

Note: Like ODS PACKAGE, the FILENAME ZIP method does not support encrypted (password-protected) ZIP archives.

Download the complete SAS 9.4 program: filenameZipHttpExample.sas

Thanks to the growing size of data files, ZIP files are created and consumed by SAS users everywhere. Between ODS PACKAGE and FILENAME ZIP, you can teach your SAS programs to build and read the files without having to rely on external tools. The more you that you can use native SAS methods for this work, the more portable your SAS programs will be.

Post a Comment