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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

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.

The Boston Area SAS Users Group is hosting free webinars!
Next up: SAS Trivia Quiz hosted by SAS on Wednesday May 21.
Register now at https://www.basug.org/events.

View solution in original post

8 REPLIES 8
ballardw
Super User

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;

 

phopkinson
Obsidian | Level 7

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

PaigeMiller
Diamond | Level 26

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?

--
Paige Miller
phopkinson
Obsidian | Level 7

It seems to be after the %TSLIT macro function is called it converts the date from date9 to character.

Quentin
Super User

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.

The Boston Area SAS Users Group is hosting free webinars!
Next up: SAS Trivia Quiz hosted by SAS on Wednesday May 21.
Register now at https://www.basug.org/events.
phopkinson
Obsidian | Level 7

You were right it was the use of the chained operators that caused the problem. Thank you very much for your help.

Tom
Super User Tom
Super User

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.));
PaigeMiller
Diamond | Level 26

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;
--
Paige Miller

sas-innovate-white.png

Our biggest data and AI event of the year.

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.

 

Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1519 views
  • 0 likes
  • 5 in conversation