BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mrdlau
Obsidian | Level 7

I have a query that I'm trying to put in prompts, but it seems to not give me data or errors when the prompts are used

 

The original code is something like this:

proc sql;
select 
*
FROM table 1
WHERE State = 'TX'

This gives me the expected result.

 

However,  in SAS EG,  when I do this, it gives me 0 results

proc sql;
select 
*
FROM table 1
WHERE State = '&State'

I have created the 'State' prompt as text and tied it to the task.  The code runs fine and gives me the pop-up box, but does gives me 0 rows.

 

 

I already tried single quotes and double quotes, but not change.

 

Once this is fixed, I'm also trying to figure out how to put dates in.

Here is original vs what I want.

Original:

proc sql;
select 
*
FROM table 1
WHERE State = 'TX'
AND date1 between '2018-03-01' AND '2018-03-15'

What I want:

 

proc sql;
select 
*
FROM table 1
WHERE State = &State
AND date1 between '&begin' AND '&End'

I'm also running the date as text but I get the following error

 

Error: Teradata prepare: A character string failed conversion to a numeric value: SQL Statement was: SELECT

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

SAS does not care whether you use single or double quotes around literal values.  But Teradata does.  It only uses single quotes for literal values. When it sees double quotes then it expects the name of an object (variable name, table name , etc).

 

But the SAS macro process cares about the type of quote character used. Macro variable values are not resolved inside of single quotes.

 

So you need to work a little harder to get single quotes around the value of a macro variable.  The easiest way is to use a data step.  So if you have two macro variables named START and STOP you could run the data step to add single quotes.

data _null_;
  call symputx('start',quote(symget('start',"'"));
  call symputx('stop',quote(symget('stop',"'"));
run;

So your program might look like:

data _null_;
  call symputx('start',quote(symget('start',"'"));
  call symputx('stop',quote(symget('stop',"'"));
run;
proc sql noprint ;
connect to teradata (......);
create table want as select * from connection to teradata
(select * from mytable where date between &start and &stop)
;
quit;

 

 

If you need to use pure macro code (which you don't since you are already generating SAS code) then something like this can work.

%let start=%bquote('&start');

But then you send up with macro quoting around the string.  It probably wouldn't matter if you are pushing it into a pass through SQL query.  But you can remove the macro quoting with the %unquote() macro function.

 

 

 

View solution in original post

4 REPLIES 4
Reeza
Super User

Macro variables only resolve in double quotes.

 

WHERE State = "&State"

@mrdlau wrote:

I have a query that I'm trying to put in prompts, but it seems to not give me data or errors when the prompts are used

 

The original code is something like this:

proc sql;
select 
*
FROM table 1
WHERE State = 'TX'

This gives me the expected result.

 

However,  in SAS EG,  when I do this, it gives me 0 results

proc sql;
select 
*
FROM table 1
WHERE State = '&State'

I have created the 'State' prompt as text and tied it to the task.  The code runs fine and gives me the pop-up box, but does gives me 0 rows.

 

 

I already tried single quotes and double quotes, but not change.

 

Once this is fixed, I'm also trying to figure out how to put dates in.

Here is original vs what I want.

Original:

proc sql;
select 
*
FROM table 1
WHERE State = 'TX'
AND date1 between '2018-03-01' AND '2018-03-15'

What I want:

 

proc sql;
select 
*
FROM table 1
WHERE State = &State
AND date1 between '&begin' AND '&End'

I'm also running the date as text but I get the following error

 

Error: Teradata prepare: A character string failed conversion to a numeric value: SQL Statement was: SELECT

 


 

Tom
Super User Tom
Super User

SAS does not care whether you use single or double quotes around literal values.  But Teradata does.  It only uses single quotes for literal values. When it sees double quotes then it expects the name of an object (variable name, table name , etc).

 

But the SAS macro process cares about the type of quote character used. Macro variable values are not resolved inside of single quotes.

 

So you need to work a little harder to get single quotes around the value of a macro variable.  The easiest way is to use a data step.  So if you have two macro variables named START and STOP you could run the data step to add single quotes.

data _null_;
  call symputx('start',quote(symget('start',"'"));
  call symputx('stop',quote(symget('stop',"'"));
run;

So your program might look like:

data _null_;
  call symputx('start',quote(symget('start',"'"));
  call symputx('stop',quote(symget('stop',"'"));
run;
proc sql noprint ;
connect to teradata (......);
create table want as select * from connection to teradata
(select * from mytable where date between &start and &stop)
;
quit;

 

 

If you need to use pure macro code (which you don't since you are already generating SAS code) then something like this can work.

%let start=%bquote('&start');

But then you send up with macro quoting around the string.  It probably wouldn't matter if you are pushing it into a pass through SQL query.  But you can remove the macro quoting with the %unquote() macro function.

 

 

 

mrdlau
Obsidian | Level 7

thanks! I was able to get what I need with %bquote

TomKari
Onyx | Level 15

In SAS, dates are typically rendered as "18mar2018"d, with the d after the quotes signifying that it's a date, not a character variable. It looks like your dates are character variables.

 

This is something where you may need to experiment a bit to ensure that SAS is giving Teradata a date that matches what it's expecting.

 

Tom

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 2220 views
  • 0 likes
  • 4 in conversation