5

I have a SQL statement that I wish to automate using SAS EG (9.4). The following statement has been tested in Teradata SQL Assistant and works.

select * from TD.DATA where date='2015-06-01'

Now I wish to push this through a proc SQL pass through, and feed the date to the SQL program, like so....

proc sql;
connect to teradata as tera(user=&tera_user password="&tera_pwd" tdpid=terap);
create table MYDATA as 
select * from connection to tera
(
select * from TD.DATA where date='2015-06-01'
);
disconnect from tera;
quit;

The above code has been tested and produces the exact same output as the previous SQL statement. However, what I really want is to do something like this:

%let input_date='2015-06-01';
proc sql;
connect to teradata as tera(user=&tera_user password="&tera_pwd" tdpid=terap);
create table MYDATA as 
select * from connection to tera
(
select * from TD.DATA where date=&input_date.
);
disconnect from tera;
quit;

I have tried various combinations of quotations and different date formats....what am I missing here? Thanks.

2
  • Try using %STR(') instead of just a single-quote Commented Jul 30, 2015 at 4:49
  • Strange - that code you provide looks fine to me (with the single quotes being assigned inside the macro variable). I tested it against a mySQL database and it worked fine. Can you try it one more time just to humor me because I find it difficult to believe that it isn't working. Maybe do it in a new SAS session. Commented Apr 4, 2016 at 16:04

3 Answers 3

7

You can use the %BQUOTE() macro function to resolve macro variables within single quotes.

%let input_date = 2015-06-01;
proc sql;
  connect to teradata as tera(user=&tera_user password="&tera_pwd" tdpid=terap);
  create table MYDATA as 
  select * from connection to tera
  (
   select * from TD.DATA where date = %BQUOTE('&INPUT_DATE')
  );
  disconnect from tera;
quit;
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, Chris! This solution is perfect, and I learned a new macro function.
I am getting "Teradata prepare: A character string failed conversion to a numeric value". Any ideas?
@csetzkorn Does the string represent a valid date?
1

Try this:

%let input_date=2015-06-01;
proc sql;
connect to teradata as tera(user=&tera_user password="&tera_pwd" tdpid=terap);
create table MYDATA as 
select * from connection to tera
(
select * from TD.DATA where date=%str(%'&input_date%')
);
disconnect from tera;
quit;

2 Comments

Thanks, Stig. This works perfectly, though I prefer Chris J's solution. It looks a little cleaner.
Hi, yeah the BQUOTE looked better, Im going to use that in the future too ;)
0

as with such format of date 201501 to make a working macrovariable

%let input_date = 201506;
%let input_date2=input(put(intnx('month',%sysfunc(inputn(&input_date,yymmn6.)),0,'b'),10.),yymmddd10.)
proc sql;
  connect to teradata as tera(user=&tera_user password="&tera_pwd" tdpid=terap);
  create table MYDATA as 
  select * from connection to tera
  (
   select * from TD.DATA where date = %BQUOTE('&INPUT_DATE2')
  );
  disconnect from tera;
quit;

2 Comments

Can you explain? What is the supposed effect of %BQUOTE('&INPUT_DATE2')?
just as input_date=2015-06-01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.