SAS Procedures

Help using Base SAS procedures
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
PeteSAS
Fluorite | Level 6

Is there a way to use functions within an IN clause (or am I totally wrecking the syntax)? Example:

 

proc sql;
create table myExample as
select datepart(myDate) as fmtDate format mmddyy8., sum(myStuff) as sum_stuff
from myTable
where datepart(myDate) in (mdy(11,15,2024), mdy(12,1,2024), mdy(1,1,2025))
group by myDate
;

myDate is a SQL Server DateTime field, hence the datepart addition there. SAS EG seems to want me to put a nested select in that "IN" clause, so it's choking on the MDY functions. Obviously there are a myriad other ways to do this, just wondering if it's a limitation of SAS that functions can't be used within IN clauses.

 

Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

SAS wants constants in the IN list.

 

Which is trivial to do with your example.  Just use date literals.

where datepart(myDate) in ('15NOV2024'd '01DEC2024'd '01JAN2025'd)

Or use macro function %SYSFUNC() to call the MDY() function so that SAS just sees the resulting number of days.

where datepart(myDate) in (%sysfunc(mdy(11,15,2024)) %sysfunc(mdy(12,1,2024)) %sysfunc(mdy(1,1,2025)))

Or like you said you can use the other syntax where it wants a query that generates to the list of values.

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

SAS wants constants in the IN list.

 

Which is trivial to do with your example.  Just use date literals.

where datepart(myDate) in ('15NOV2024'd '01DEC2024'd '01JAN2025'd)

Or use macro function %SYSFUNC() to call the MDY() function so that SAS just sees the resulting number of days.

where datepart(myDate) in (%sysfunc(mdy(11,15,2024)) %sysfunc(mdy(12,1,2024)) %sysfunc(mdy(1,1,2025)))

Or like you said you can use the other syntax where it wants a query that generates to the list of values.

Patrick
Opal | Level 21

The IN Operator only allows for a query expression or constants but not for functions.

You can pass SAS date constants in the form 'ddmonyyyy'd 

proc sql;
  create table myExample as 
    select datepart(myDate) as fmtDate format mmddyy8., 
    sum(myStuff) as sum_stuff 
  from myTable 
  where datepart(myDate) in ('15nov2024'd,'01dec2024'd,'01jan2025'd) 
  group by myDate;
quit;

 

 

myDate is a SQL Server DateTime field

Does that mean your source table myTable is a SQL Server table? If so then ensure that SAS can push the where clause to the database for processing so it doesn't first pull all the data from SQL Server before sub-setting. The datepart() function could cause a problem here. Passing SAS Functions to Microsoft SQL Server

 

Happy to provide additional advice if above is your situation. I would need to know if only the source or also the target table are in SQL Server and the exact where clause you want to use.

 

As a comment to below syntax provided by @Tom: This will only work for processing on the SAS side because the result of the %sysfunc() portion is a number that expresses a SAS date. SAS won't be able to convert this number to the matching SQL date for in-database processing. If you use SAS date literals like '01jan2024'd then SAS will be able to convert the date to the SQL Server equivalent for in-database processing.

where datepart(myDate) in (%sysfunc(mdy(11,15,2024)) %sysfunc(mdy(12,1,2024)) %sysfunc(mdy(1,1,2025)))

 

sas-innovate-white.png

Join us for our biggest event of the year!

Four days of inspiring keynotes, product reveals, hands-on learning opportunities, deep-dive demos, and peer-led breakouts. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 666 views
  • 2 likes
  • 3 in conversation