Search code examples
sqlsassql-like

SAS SQL with the "Like" and '%___%'


I have a dataset where I read in multiple sheets of an excel spreadsheet. One of the variables (OrderDate) is in the "04-Aug-95" format, although when I print, it comes out "04AUG1995."

I'm trying to write code that will return the number (count) of orders in the first quarter of 1995. However, the following code returns a list of every OrderDate in the data set and a Count of 830 (which is the total number of rows). Any help is greatly appreciated:

proc sql;
select OrderDate, count(*) as Count
from Orders
where OrderDate LIKE '%JAN1995' OR '%FEB1995' OR '%MAR1995';
quit;

Solution

  • Try either (in the where-clause):

    Put(OrderDate, Date9.) like '%JAN1995' OR Put(OrderDate, Date9.) like '%FEB1995' OR Put(OrderDate, Date9.) like '%MAR1995'
    

    Or:

    Where Case When Put(OrderDate, Date9.) like '%JAN1995' Then 1
               When Put(OrderDate, Date9.) like '%FEB1995' Then 1
               When Put(OrderDate, Date9.) like '%MAR1995' Then 1
               Else 0
          End