Thursday, February 18, 2016

Hack 2.2 Creating a Subset of a SAS Data Set for Testing


SAS Programming Professionals,

Did you know that you can effortlessly create a small subset from a large SAS data set for testing purposes?

The FIRSTOBS and OBS SAS data set options can be used to slice-n-dice a group of observations from within a SAS data set so they are the only ones written to an output data set.  The FIRSTOBS option specifies the first observation that SAS is to process.  The default is 1.  The OBS option specifies the last observation that SAS is to process.  The default is MAX, which means all observations in a SAS data set are to be processed.

When you specify OBS, SAS performs the following algorithm to return observations:  (OBS – FIRSTOBS) + 1 = the observations returned.

Let’s look at an example:

data cars;
set  sashelp.cars(obs=200 firstobs=151);
run;

In the example, we need an extract of the CARS data set, which contains 428 observations,  to test our new program.  So, we create the CARS data set in the SAS WORK library with 50 observations in it.  We specified that all of the observations in SASHELP.CARS between 151 and 200, inclusive, be written to our new CARS data set.  The log looks like this:

1    data cars;
2    set  sashelp.cars(obs=200 firstobs=151);
3    run;

NOTE: There were 50 observations read from the data set SASHELP.CARS.
NOTE: The data set WORK.CARS has 50 observations and 15 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds

So we now have a happy, healthy extract data set that we can use to develop our new program!

Best of luck in all of your SAS endeavors!

----MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)

Excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers

I plan to post each and every one of the hacks in the book to social media on a weekly basis.  Please pass them along to colleagues who you know would benefit.

Monday, February 15, 2016

Hack 2.1 Calculating a Person’s Age in Years

SAS Programming Professionals,

Did you know that you can easily calculate a person’s age in years?

The optional AGE argument for the YRDIF function allows you to calculate a person’s age in years.  The AGE argument takes all of the leap years into account as it determines the number of years between a birthdate and another date.  Here is an example:

data mikeage;

bdate = "27APR1975"D;

CurrentAge = int(yrdif(bdate, today(), 'AGE'));

put "Michael's current aqe is:  " CurrentAge;

run;

In the example, we obtain the subject’s birthdate in the BDATE variable.  Then, we use the YRDIF function with the AGE argument to calculate how old the subject is as of today.  The log looks like this:

1    data mikeage;
2
3    bdate = "27APR1975"D;
4
5    CurrentAge = int(yrdif(bdate, today(), 'AGE'));
6
7    put "Michael's current aqe is:  " CurrentAge;
8
9    run;

Michael's current age is:  40

Note that we used the INT function to return an integer representation of the subject’s age.  Not doing so would have resulted in an age of 40.78630137 which, I am sure you will agree, is not quite so meaningful.

Best of luck in all your SAS endeavors!

----MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)

Excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers

I plan to post each and every one of the hacks in the book to social media on a weekly basis.  Please pass them along to colleagues who you know would benefit.

Sunday, October 4, 2015

Phil Holland's New SAS Book is a Programming Tour de Force

SAS Programming Professionals,

The latest book in Phil Holland’s Power User’s Guide series, SAS Programming and Data Visualization Techniques, is an unmitigated tour de force.  It contains fourteen fact-filled chapters covering three very relevant main topic areas:  Programming and Efficiency Techniques, External Interfaces, and Data Visualization.  As with the other books in the Power User’s Guide series, this book is written in Phil’s authoritative, but conversation style and has lots and lots of code examples that illustrate real-world SAS programming solutions.

There are so many usable programming gems in this book—even for very experienced SAS programming professionals—that it is hard to call them all out in a brief review.  However, three specifically come to mind because they can save any organization much more than the price of the book. 

·        Part I: Programming Efficiency Techniques, Chapter 1: The Basics of Efficient SAS Coding.  In this chapter, Phil discusses speed versus low maintenance.  It is a very nuanced discussion that should make seasoned SAS programmers reconsider what they might have thought was the obvious choice.
 
·        Part II: External Interfaces, Chapter 4: SAS to R to SAS.  Because of its powerful statistical and graphic capabilities, its low price (it is actually free), and its popularity in academic institutions, R has gained entry into the programming infrastructure of many organizations—including my own.  So, sooner or later you are going to need to interface with R; and when you do, you will reach for the examples in this chapter.

·        Part II: External Interfaces; Chapter 8: Everyday Uses for SAS Output Delivery System (ODS).  The section titled Disguising a Web Page is absolutely brilliant and worth the price of the book.  I have bookmarked that page and already begun using this simple, but deeply clever technique in my own SAS programs.

My prediction is that when you purchase SAS Programming and Data Visualization Techniques:  A Power User’s Guide, it will not sit in your programming bookshelf between other SAS programming books.  Instead, it will be beside your keyboard on your desk with pages dog-eared and a dozen yellow sticky-notes protruding from various pages.  Mark my word!

Best of luck in all your SAS endeavors!

----MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)

Saturday, April 18, 2015

Doctor, I Have This Friend Who Has A Pain Right About Here

SAS Programming Professionals,

We have all heard the old joke about the person who sidles up to a doctor at a party and describes in great detail a medical problem a "friend" is having in order to get free medical advice.  It could just as easily be a person buttonholing an accountant for free tax advice, a lawyer for free legal advice, a personal trainer for free fitness advice, a CEO for free business advice, a chef for free cooking advice, a financial planner for free investment advice, a dietitian for free dietary advice, a college professor for free educational advice, and so on.  But, what about free SAS programming advice?

The good news is that if you have a question about SAS programming, you don't have to sidle up to a SAS expert and ask it in a roundabout way. There are a number of great venues where you can ask SAS technical questions directly and have them answered by SAS experts from the US and from around the world.  And, the only cost to you is the several hundred keystrokes you expend writing a concise description of your problem with supporting information such as:

  • The portion of your SAS program in question
  • A snippet of the log showing an error, if applicable
  • The version of SAS you are using
  • The operating system you are running SAS on
Here are some of the facilities available to help SAS professionals with their programming questions:
  • Support Communities on support.sas.com -- There are very active discussion groups with titles such as Base SAS Procedures, SAS Macro Language and Data Step, ODS and Base Reporting, and many more.  Users post their programming questions and then SAS experts from around the world and from SAS post possible solutions.  Questioners usually get many and varied answers to their questions.  This web-based community even has its own document titled "How to ask a Question in SAS Support Communities?" to help lead you through the process if you are new.  You can access the SAS Support Communities via this link:  https://communities.sas.com/community/support-communities 
  • SAS-L Listserv.  This oldest medium for SAS Q&A is realized as an email listserv.  You subscribe to the SAS-L listserv, and then when people post questions and answers, all subscribers get the messages.  SAS-L has a very loyal base of SAS experts who usually jump on questions immediately with answers from many different angles.  The sasCommunity.org web site has a good write-up on how to subscribe to SAS-L:  http://www.sascommunity.org/wiki/SAS-L
  • LinkedIn SAS Groups.  There are over a dozen SAS discussion groups on LinkedIn.  Some notable ones for Q&A are:  SAS Professional Forum, SAS Programming Tips, and SAS Author: Phillip R Holland.  These discussion groups are conducted much the same way as the previously mentioned Q&A sites, but also include articles of general interest to SAS programmers.  The LinkedIn home page is:  https://www.linkedin.com/
  • Code Clinics.  Most of the SAS Local Users Groups (LUGs) and SAS Regional Users Groups (RUGs) feature a "code clinic" at their meetings.  The code clinics are staffed with SAS programming experts who can help you with programming problems one-on-one.  They are expecting drop-ins, so bring your SAS questions and sidle-away.
There are doubtlessly other web sites and other facilities out there in the big wide world where SAS users will be glad to pitch in and help answer your questions. But, the list above is a good starting point.

Hey, as long as I have you here; I have this friend who is interested in competing in his first triathlon; you wouldn't happen to know anything about training for one would you?

Best of luck in all your SAS endeavors!

----MMMMIIIIKKKKEEEE
aka Michael A Raithel
Amazon Authors page:

Monday, April 13, 2015

Keep Your Hands Off of My SAS Data Sets!

SAS Programming Professionals,

I am all about sharing.  Knock on my door and I will gladly lend you a stick of margarine, a cup of sugar, an egg or two, some flour, a corkscrew, or a beer.  Not a problem to borrow a tie, one of my extra belts, a white shirt (if it fits), a scarf, or a pair of gloves.  Sure, I will gladly lend you one the books in my home library, a couple of my music Cd's, or one of my movie DVDs.  I am reasonably sure that most of these items will either be returned to me in due time, or reciprocated, or paid forward.  But, keep your hands off of my SAS data sets!

I would bet that final sentiment regarding SAS data sets is pretty prevalent in organizations where SAS programmers work with shared storage resources.  Whether you are storing your SAS data sets in server directories or on network directories, you do not want to have them deleted, resorted, updated, or otherwise overwritten by other programmers on your team.  At least, not without your permission.

Fortunately, most organizations create security permissions that largely safeguard against unauthorized access of data.  They implement security packages--either native to the OS or purchased from vendors--to ensure that data is accessed only by those with the need to know.  Groups of programmers are given access to specific directories and other staff, with the exception of systems administrators, cannot get into them or see them at all.  Despite all of this protection, data integrity issues can come from programmers within your own group; programmers who have access to the same directories that you do; programmers who have the same access rights that you do.

Unfortunately, there is no foolproof way for you to keep your teammates from crunching your SAS data sets.  But, here are a few protection measures that you can put into place to help safeguard your SAS data sets:

  • SAS Data Set Passwords.  SAS allows you to specify ALTER, READ, and WRITE passwords.  Users must know the password in order to process password-protected data sets with SAS.  So, you can specify passwords for your do-not-disturb SAS data sets, wait for your co-workers to complain, and determine if they really do need access to those data sets.
  • ACCESS=READONLY Libname Option.  This option specifies that no data sets in the library can be updated and no new files can be written to the library.  This option can be effectively deployed in a shared group AUTOEXEC.sas file.  If your colleagues grumble, tell them to copy the data sets in question to one of their libraries and process them there.  Caution them to be mindful of version control issues.
  • SAS Views.  Create views of your SAS data sets via the DATA step or PROC SQL and allow your colleagues to use the views instead of the actual data sets.  The beauty of this approach is that you can have your valued data sets in one directory and the views in another.  Point your coworkers at the views directory and do not tell them the whereabouts of the permanent data sets.
  • Generation Groups.  You can specify for SAS to keep several generations of your SAS data sets available.  Consequently, when one of them is modified, you will still have the older version available.
  • SAS Audit Logs.  SAS Audit Logs can be used to determine who updated SAS data sets after the fact. They don't stop your colleagues from actually updating SAS data sets, but you can use them to determine the who, what, and when so that you can storm into the right office for an explanation.
  • LOCK Statement.  You could use the LOCK statement to lock your SAS data sets so that no other SAS program can read or write to the file.  This is a bit extreme and would require that your SAS program with the LOCK statements be running for the duration of when you wanted to safeguard your SAS data sets.  This option can be effectively deployed in a shared group AUTOEXEC.sas file.
  • Zip Files.  You could simply zip your SAS data sets up into zip files, delete the permanent SAS data sets, and restore the data sets from the zip files when you need them.  This is another extreme measure, but if your data sets get clobbered on a regular basis, you may find it more appealing than having them constantly restored.
Obviously, the best solution for keeping your important SAS data sets from being updated by your teammates is good communications between all involved, and a shared set of best practices for accessing and modifying SAS data sets.  But, when that is not available, you may have to reach for some of the ideas I have posted here.

Oh, and about that book that you borrowed from my SAS bookshelf library a couple of months ago... can I get it back?

Best of luck in all your SAS endeavors!

----MMMMIIIIKKKKEEEE
aka Michael A. Raithel

Amazon Author's Page:  
http://www.amazon.com/Michael-A.-Raithel/e/B001K8GG90/ref=ntt_dp_epwbk_0 



Sunday, April 5, 2015

We Don't Need No Education!

SAS Programming Professionals,

So, how did you first learn SAS programming?

Originally, I was self taught.  Many years ago, I learned SAS on the job when a systems programmer quit and I took over supporting a mainframe performance software package that was written in SAS. I got a copy of the Base SAS users guide and the SAS Procedures guide and learned how to write my own SAS programs.  It was new and fun; and SAS was more powerful than any of the other programming languages I had been using.

After I had been programming with SAS for about three years, my then employer finally paid to send me to a SAS class.  I entered that class pretty confident of my SAS programming abilities, but was humbled within the first hour.  I had been running my SAS programs in batch on mainframe computers.  All of the students in the class ran their SAS programs interactively with SAS Display Manager, which I had never seen before.  So, I had to catch up to their level of expertise just to do the class exercises.

It was a great class!  I learned to use the SAS Display Manager; the proper ways to perform match merges; how the Program Data Vector works in the DATA step; the intricacies of PROC MEANS and its first cousin PROC SUMMARY; and a host of other very useful programming techniques. I learned the fundamentals of SAS that I had missed by simply jumping into using it and getting all of my information from the manuals.  That class made me a much stronger SAS programmer.

Today's programming professionals who are interested in taking formal SAS classes to increase their SAS programming acumen have a lot of choices.  SAS currently has the following training formats:
  • Classroom.  You can take an instructor-led SAS class in one of SAS's state-of-the-art training facilities located throughout the US and in many other countries.
  • Live Web Classroom.  Let an instructor-led SAS class come to you via the Internet.  This format saves on travel costs and time out of the office.
  • E-Learning.  This option allows you to take SAS classes at your own pace via the Internet 24/7.  This is another way to save travel costs and time out of the office.
  • On-Site Training.  Have qualified SAS instructors come on-site to teach you and your colleagues various SAS courses.
  • Mentoring Services.  This option provides you with a SAS instructor who becomes your coach to help you learn how to write SAS programs that address your organization's unique data processing needs.
You can find out much more about available SAS classes being offered, their content, when they are scheduled, and the training formats in the Training section of the support.sas.com web site:


Check it out because there is likely a SAS class that would make you a stronger programmer in a format that fits with your own work schedule.

"We don't need no education"?  Nope, that couldn't be further from the truth!

Best of luck in all your SAS endeavors!

----MMMMIIIIKKKKEEEE
aka Michael A Raithel





Wednesday, April 1, 2015

Don't Call Me an April Fool!

SAS Programming Professionals,

With over 50,000 certification credentials awarded to people around the world, the SAS Global Certification program is obviously meeting the needs of a goodly number of SAS programming professionals.  Interested SAS software users can demonstrate an in-depth understanding of SAS by earning credentials in SAS Programming, SAS Analytics, SAS Administration, SAS Data Management, and SAS Business Intelligence.  They study the material, master it, take the test, and earn the credential.

You can learn more about the SAS Global Certification program by visiting this link: //support.sas.com/certify/

As it stands now, the two SAS Base Programmer certification tests require a person to answer multiple choice and short-answer questions.  Personally, I am locked-and-loaded for the day that one of those exams requires an essay.  I know that the secret to getting a good grade on an essay question is not to simply answer the prompt.  The secret is to add additional information about the topic which shows that you have a mastery of the overall subject matter.  Since I have been programming with SAS for a very long time, I have a lot of very sage and insightful comments that I would put into my essay.  So, I have no doubt that I would crush it!

Here are some of the clever things I have learned along the way that I would weave into my essay:


  • The “DO WHILE” statement is known as the “Stalactite DO” since it is evaluated at the top.  The “DO UNTIL” statement is known as the “Stalagmite DO” since it is evaluated at the bottom.


  • When I need to fix a SAS program with a hard-to-find error, I just put a %MEND at the bottom and rerun it.


  • An example of a SAS regular expression is:  “Don’t forget the semicolon”!


  • The SAS Log is basically a waste of time and effort.  I never use it because it only prints bad news!

  • I sometimes code: PROC OPTIONS; ...without the “RUN;” statement, just to keep my OPTIONS open!


  • A known SAS programming trick is: If you code a DROP statement and a KEEP statement for the same variable in a DATA step, you will see a bright flash on your monitor as they cancel each other out.


  • It is difficult to determine if PROC MIXED has executed successfully because it prints MIXED messages.


  • The SAS Display Manager is a long-time SAS Institute employee who is responsible for supervising the setup of SAS Institute booths at conferences and trade shows.


  • Despite its strong odor, the RANK procedure is very popular among SAS programmers serving in the US armed forces.


  • Due to heightened security concerns, SAS programs containing PROC EXPLODE have been banned from all government buildings, airports and rail stations.


  • A recent survey of American SAS programmers found that most use PROC IMPORT far more than they use PROC EXPORT.  However, the same survey determined that this did not significantly add to the United States trade deficit.
 
Okay, so do I really have to say:  April fools!  Nah, didn't think so.

Best of luck in all your SAS endeavors!

----MMMMIIIIKKKKEEEE
aka Michael A. Raithel
Check out my SAS books:  http://www.amazon.com/Michael-A.-Raithel/e/B001K8GG90/ref=ntt_dp_epwbk_0