Desktop productivity for business analysts and programmers

Unable to get data with prompts

Accepted Solution Solved
Reply
Contributor
Posts: 37
Accepted Solution

Unable to get data with prompts

[ Edited ]

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

 


Accepted Solutions
Solution
‎03-18-2018 03:59 PM
Super User
Super User
Posts: 7,857

Re: Unable to get data with prompts

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


All Replies
Super User
Posts: 22,850

Re: Unable to get data with prompts

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

 


 

Solution
‎03-18-2018 03:59 PM
Super User
Super User
Posts: 7,857

Re: Unable to get data with prompts

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.

 

 

 

Contributor
Posts: 37

Re: Unable to get data with prompts

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

PROC Star
Posts: 1,263

Re: Unable to get data with prompts

[ Edited ]

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

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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