This Sql work fine:
SELECT
TOP 10 *
FROM SomeCalendarData
WHERE SomeDate = '2016/01/01';
So using a 'string macro variable' like so should work as well:
%let StartDate = 2016/01/01; proc sql; connect to teradata (BULKLOAD=YES MODE=TERADATA user=&user. Password=&passwd.); CREATE TABLE WorkTest.test AS select * from connection to teradata ( SELECT TOP 10 * FROM SomeCalendarData WHERE SomeDate = %bquote('&StartDate.'); ); quit;
Any idea why this is not the case? Thanks!
Thanks for all your help, This:
%let StartDate = 2016/01/01; proc sql; connect to teradata (BULKLOAD=YES MODE=TERADATA user=&user. Password=&passwd.); CREATE TABLE WorkTest.test AS select * from connection to teradata ( SELECT TOP 10 * FROM SomeCalendarData WHERE SomeDate = CAST(%bquote('&StartDate.') AS DATE FORMAT 'YYYY-MM-DD'); ); quit;
Does the trick. Although I am still unsure why an explicit CAST like this should be necessary?! Bizare ...
This line:
WHERE SomeDate = %bquote('&StartDate.');
Your macro variable will not resolve unless its in doubel quotes:
WHERE SomeDate = %bquote("&StartDate.");
If Teradata doesn't like double quotes try this:
WHERE SomeDate = %str(%')&StartDate.%str(%');
I've never used Teradata but a quick Google search for Teradata date formats suggests '2016-01-01' is more commonly used. Have you tried that?
Thanks for all your help, This:
%let StartDate = 2016/01/01; proc sql; connect to teradata (BULKLOAD=YES MODE=TERADATA user=&user. Password=&passwd.); CREATE TABLE WorkTest.test AS select * from connection to teradata ( SELECT TOP 10 * FROM SomeCalendarData WHERE SomeDate = CAST(%bquote('&StartDate.') AS DATE FORMAT 'YYYY-MM-DD'); ); quit;
Does the trick. Although I am still unsure why an explicit CAST like this should be necessary?! Bizare ...
Although I am still unsure why an explicit CAST like this should be necessary?
According to docu that's the syntax:
CAST ( expression AS data_type [ ( length ) ] )
https://msdn.microsoft.com/en-AU/library/ms187928.aspx#BKMK_examples
If you just use the date string without any cast, then are you sure that this string gets converted to a SQL Server representation of DATE and not of DATETIME2 due to Data Type Precedence?
https://msdn.microsoft.com/en-us/library/ms190309.aspx
I'd assume that if it becomes DATETIME2 then your code executes without any error but the where clause doesn't behave as you'd expected it.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.