Hi,
I have a piece of code which selects the date between two macro variables (Var1 and Var2) - however in proc fedsql these don't seem to resolve within the %TSLIT() function to a comparable date value. Is there a way to get them to work in my where clause?
Thanks in advance.
Is your FEDSQL sending the code off to POSTGRES? It looks like a Postgres error message. See e.g. this thread:
https://communities.sas.com/t5/SAS-Programming/Error-in-PROC-SQL-query/td-p/859565
Just to remove the macro factor, try writing your code with hard coded values, and I expect you'll get the same error message, e.g.:
WHERE '2024-01-01' <= date <= '2024-12-31';
Also, that syntax of chained operators, x<y<z is allowed in SAS, but is not common in other languages, so I would try:
WHERE ('2024-01-01' <= date) and (date <= '2024-12-31') ;
The error message says you're comparing a boolean value (true false) to a character value, which isn't allowed in Postgres. That would happen if postgres isn't evaluating x<y<z as a single boolean expression. Instead it's doing (x<y)<z.
Then once you get the code working without %TSLIT, go back to trying to use %TSLIT to add the single quotes.
Can you share the code that generates the two macro variables? And maybe provide some examples that "don't work". And the subsequent call to %TSLIT.
A very common problem with macro variables and "date" values is applying a format to the value and then attempting to use the result in a comparison or calculation. Generally the formatted values don't do well because the behave as character values and things like 11/05/2024 don't evaluate well when compared to 01/05/2025 (as in the first is "larger" than the second because of the rules used for comparison of strings). OR 11/05/2024 gets treated as multiple division operations as it would here and is why x = 0.0010869565 (more or less)
data junk; x = 11/05/2024; run;
The code that generates the macro variables is completely fine and not the cause of my issue - I can't share that part anyway I'm afraid. This is the where condition which I am certain is causing the problem.
WHERE %TSLIT(&VAR1.) <= date <= %TSLIT(&VAR2.);
This is the error message that I get in the log.
ERROR: Operator does not exist: bool <= CHAR
What are the values of &VAR1 and &VAR2? What are the typical values of variable DATE, un-formatted? What format is assigned to variable DATE? Is DATE numeric or character?
It seems to be after the %TSLIT macro function is called it converts the date from date9 to character.
Is your FEDSQL sending the code off to POSTGRES? It looks like a Postgres error message. See e.g. this thread:
https://communities.sas.com/t5/SAS-Programming/Error-in-PROC-SQL-query/td-p/859565
Just to remove the macro factor, try writing your code with hard coded values, and I expect you'll get the same error message, e.g.:
WHERE '2024-01-01' <= date <= '2024-12-31';
Also, that syntax of chained operators, x<y<z is allowed in SAS, but is not common in other languages, so I would try:
WHERE ('2024-01-01' <= date) and (date <= '2024-12-31') ;
The error message says you're comparing a boolean value (true false) to a character value, which isn't allowed in Postgres. That would happen if postgres isn't evaluating x<y<z as a single boolean expression. Instead it's doing (x<y)<z.
Then once you get the code working without %TSLIT, go back to trying to use %TSLIT to add the single quotes.
You were right it was the use of the chained operators that caused the problem. Thank you very much for your help.
Looks like it complaining about the operators, not the values.
Are you sure that PROC FEDSQL supports SAS's abbreviated comparison syntax that treats:
a op1 b op2
as meaning
(a op1 b) and (b op2 c)
??
What happens if you spell things out for FEDSQL?
WHERE (%TSLIT(&VAR1.) <= date) and (date <= %TSLIT(&VAR2.));
SHOW US the code.
Since you have macro variables, turn on the macro debugging options, run the code again and then also show us the log.
options mprint symbolgen;
Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.
Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.