Syntactic sugar - 6 SAS essentials

7

The 25 boys I teach on Sundays loved these cupcakes I baked for them for Easter… the sugared peeps had them completely satisfied.

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.

2. PROC Step essential– PROC COMPARE

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 ;

To sum up:

  • 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.

Share

About Author

Charu Shankar

Technical Training Specialist

Charu Shankar has been a Technical Training Specialist with SAS since 2007. She started as a programmer, and has taught computer languages, business and English Language skills. At SAS, Charu teaches the SAS language, SQL, SAS Enterprise guide and Business Intelligence. She interviews clients to recommend the right SAS training to help them meet their needs. She is helping build a center for special needs kids in this project. http://www.handicareintl.org/pankaja/pankaja.swf

Related Posts

7 Comments

  1. Hi
    I am not quite sure INTCK example is correct in the above example (4). "The INTCK function counts intervals by using a fixed starting point for the interval as opposed to counting in multiples of the interval unit. Partial intervals are not counted."

    suppose today function return below dates,

    data payroll;
    employee_hire_date='31DEC2012'd; today='01JAN2013'd ; output;
    employee_hire_date='01JAN2012'd; today='31DEC2012'd ; output;
    run;

    data payroll1;
    set payroll;
    years=intck('year',employee_hire_date, today);
    put years;
    run;

    which return 1 for the first record and 0 for second record.

  2. Charu Shankar
    Charu Shankar on

    Hi Divyesh, if your primary tool is SAS Enterprise guide, here's your answer--The DATA step debugger, being a full-screen application based in SAS, cannot run within SAS Enterprise Guide. You can still use the time-tested and proven techniques of OBS=0 (to prevent data processing) and strategically placed PUT statements (to see the values of key variables within the SAS log). For more read this note
    http://support.sas.com/documentation/onlinedoc/guide/sasegforprogrammers.htm

  3. Charu Shankar
    Charu Shankar on

    Good to know you can use these tips in your scripts Soraya. How're things with your big data pulls at work. I'd love to hear how that's going..

  4. Charu Shankar
    Charu Shankar on

    Thanks Ferdinand, glad you found this useful. Hope all's well with you.. l'd love to hear your news & ways in which you're using SAS at the bank.

  5. With regards to your first tip on debugging, "/debug" won't work when we
    develop Base SAS programs using SAS Enterprise Guide. I wonder if
    there is an equivalent for such an option in Enterprise Guide.

  6. Soraya Walker on

    Hi Charu
    Cupcakes look yummy and I will definitely try some of the 6 SAS essentials in my future scripts. Soraya

Leave A Reply

Back to Top