I love my tea or coffee laced with sugar. I think you‘d all agree that it is always more pleasant to meet a nice customer service person rather than a crabby one. Likewise it’s music to a computer’s ears…and you’ll find your computer loves you more when you make the syntax easier for it. So why should SAS syntax be any different?
Here are my 6 SAS essentials to sweeten your SAS pot.
1. DATA Step essential - DATA Step debugger
You know the number of times you have no idea what went on behind the scenes - why is your data in error? A-throwing-up-of-hands in the air and screaming of HELP is entirely justified! Well, help is just around the corner. Use the powerful DATA Step debugger to go and see the values of each of your variables during execution. Unique to SAS, it lets you pause during execution to ostensibly check the values of your data (while you might actually surreptitiously run off to get a caffeine sugar boost). Here’s a neat usage example from the SAS support page: Debugging a Simple DATA Step.
Students often ask how they can compare historical data. How to determine if anything changed, variables/ observations? The answer to this frequently asked question lies in easy PROC COMPARE. See how sweetly SAS output complies with your request, showing variables with conflicting type.
proc compare base=sashelp.cars compare=work.compare; run;
3. Operator – SOUNDS LIKE =*
You're probably familiar with operators like addition, subtraction, AND, OR, NOT that Linda Jolley wrote about in her excellent post. While these Operators do make things easy for SAS, I have to admit I need my syntactic sugar of the SOUNDS LIKE Operator. It is so clever in grabbing misspelled data that I had to dedicate an entire blog post to its workings. Take a look and tell me if you are not impressed with this operator.
4. Functions allow you to manipulate data. Hands down my favorite function would be INTCK.
Many times customers ask how they can calculate the difference between two date values. For example, HR Manager wants to see the number of years since employees joined the company to calculate service awards. Without going through lines and lines of writing code, here’s a sweeter way. Use The INTCK function. See how much more it can do in these examples. Not only does it calculate number of years between two dates, it can calculate number of months, weekdays, etc.
data payroll; set orion.employee_payroll; years=intck('year',employee_hire_date, today()); run; proc print data=payroll; var employee_id years; where years >= 10; run;
5. Format – Let’s say your manager is interested in seeing a report with employee birth dates. This is what you‘re about to send and then realize that the dates look ambiguous, i.e. the date circled in red could be either November 12 or December 11. What to do? Instead of arguing with your Manager, just use the powerful date9. format that removes all ambiguity from your date display. Just sweetness and light! No more lengthy debates on whether it is a November or December date!
proc print data=payroll; var employee_id marital_status birth_date; format birth_date date9.; run;
6. Informat - What if you want to read non-standard data - a date that has a character value: '2002/APR/17' - You know SAS can read any data and you read that SAS Informats can do the trick. You search high and low for the right Informat that will do the trick. Informats are great sugar for reading data. Here you want an informat that will do 2 things: 1) read your character value and convert it to numeric, 2) Its no ordinary numeric, but a SAS date value (that's the number of days since January 1, 1960). Your search takes you nowhere. Don’t give up though. Did you know you can create your own? See below for a satisfying code snippet from a SAS Global Forum paper.
proc format ; picture temp low-high ='%Y/%b/%d'(datatype = date) ; run ; data infmt ; retain fmtname "yyyymd" type "I" ; do label = "01jan1999"d to "01jan2003"d ; start = put(label,temp11.) ; start = trim (left (start) ) ; output ; end; run; proc format cntlin = infmt ; run ; data _null_ ; _txtdate = "2002/APR/17" ; _sasdate = input (_txtdate,yyyymd.) ; put _sasdate = ; run ;
- The DATA Step is your builder. Use the debugger to see what you've built.
- The PROC Step is your reporter/analyzer. Use PROC COMPARE to report on data comparisons.
- OPERATORS are syntactic sugar. Use the SOUNDS LIKE Operator to track down misspelled data.
- FUNCTIONS manipulate data.
- INFORMATS have to match your data.
- FORMATS is where you have creative license.
While I did come up the first few lines above, I have to give credit for the last three lines to Barb Crowther, my colleague who uses these to great effect in her class.
Hopefully I was able to give you that much needed sugar boost during this week after Easter! I think my six SAS essentials are bound to melt the toughest and most gnarliest of a SAS session. Want to learn more ways to sweet talk your SAS session into submission? Consider the SAS Programming 1: Essentials and SAS Programming 2: Data Manipulation Techniques training courses.