Jedi SAS Tricks - DS2 gives you more to LIKE

2

I've been a busy Jedi the last few months and have spent the bulk of my non-teaching days learning new Jedi SAS tricks to share with you. The finishing touches are going on to our brand-new, one and a half day DS2 Programming: Essentials class. It's rolling out on December 16 at SAS world headquarters in Cary, NC - and I'm very much looking forward to teaching it!

DS2 is a new SAS programming language available in SAS9.4. It combines the best of the SAS DATA step and SQL:1999 programming languages. DS2 allows native manipulation of most ANSI data types (including the high-precision DECIMAL type - see "Jedi SAS Tricks: Finding Tattoine with DS2") and makes writing DATA steps for parallel processing so easy you'll be leveraging all your CPUs to get your job finished in no time. Besides all of the cool new technology, DS2 delivers some DATA step features that have been on my wish list for quite some time.  One of my favorites is how DS2 leverages the LIKE expression.

As you may remember, the LIKE expression allows searching for patterns in text.  You can use ''wildcards" to specify the pattern - a percent sign (%) for zero or more characters or an underscore (_) for exactly one character.   Up until now, LIKE has only worked in SQL WHERE clauses and SAS WHERE statements. Wouldn't it be awesome to be able to use LIKE expressions in other SAS statements? Well with DS2, you can!  For example:

libname test "!SASROOT\core\sashelp";
proc ds2;
data convertibles (overwrite=yes) S_type(overwrite=yes);
   Keep Make Model type;
   method run();
	   set test.cars;
      if Model like '%convertible%'
         then output convertibles;
      if Model like '% S'
         then output S_type;
   end;
enddata;
run;
quit;
 
proc FedSQL number;
title 'Convertible Cars';
select *
   from convertibles
	order by Make, Model, Type
;
title 'S Model cars';
select *
   from s_type
	order by Make, Model, Type
;
quit;

Partial output:

Convertible cars
Partial listing of convertibles


S-model cars
S-model cars

I really like LIKE - and DS2! How about you? If so, I hope I'll see you in a DS2 class sometime soon.

Until next time, may the SAS be with you!
Mark

Share

About Author

SAS Jedi

Principal Technical Training Consultant

Mark Jordan (a.k.a. SAS Jedi) grew up in northeast Brazil as the son of Baptist missionaries. After 20 years as a US Navy submariner pursuing his passion for programming as a hobby, in 1994 he retired, turned his hobby into a dream job, and has been a SAS programmer ever since. Mark writes and teaches a broad spectrum of SAS programming classes, and his book, "Mastering the SAS® DS2 Procedure: Advanced Data Wrangling Techniques" is in its second edition. When he isn’t writing, teaching, or posting “Jedi SAS Tricks”, Mark enjoys playing with his grand and great-grandchildren, hanging out at the beach, and reading science fiction novels. His secret obsession is flying toys – kites, rockets, drones – and though he usually tries to convince Lori that they are for the grandkids, she isn't buying it. Mark lives in historic Williamsburg, VA with his wife, Lori, and Stella, their cat. To connect with Mark, check out his SAS Press Author page, follow him on Twitter @SASJedi or connect on Facebook or LinkedIn.

Related Posts

2 Comments

  1. Hi, I'm a bit new to SAS and trying to learn DS2, and that's why I came across your blog. I believe that DS2 will make our code nicer and the processing time shorter for our data base. But I wanted to let you my code to do the same as you using SAS:
    libname test "!SASROOT\core\sashelp";
    data convertibles2 s_type2;
    keep make model type;
    set test.cars;
    if index(model, "convertible") ge 1 then output convertibles2;
    if scan(model, -1) = 'S' then output s_type2;
    run;

    • SAS Jedi

      You can accomplish this this task using functions in DS2 as well, for example:
      libname test "!SASROOT\core\sashelp";

      proc ds2;
      data convertibles (overwrite=yes) S_type(overwrite=yes);
         Keep Make Model type;
         method run();
            set test.cars;
            if index(model, "convertible") ge 1 then output convertibles2;
            if scan(model, -1) = 'S' then output s_type2;
         end;
      enddata;
      run;
      quit;

      However, you cannot use the LIKE operator in a traditional base SAS DATA step.

Back to Top