while-loopsasteradatasas-macroteradatasql

Loop merge using Teradata or SAS


I have a Teradata data tables with more than 60MM IDs for a period of three years. Meaning, the same ID will be repeated in all three years. I am looking for a macro where I can use ID to loop through until the last record with start date and end date being the same.

I have a snippet of SAS code to define the lower and upper ID to use it as a filter

 %macro loopmerge(n);

 %do i=1 to &n.
 data _null_;
 call symputx('lower',%eval(&i-1)*25000000
 call symputx('upper',%eval(&i)*25000000

 Proc Sql;
   Connect to teradata (XXXXXXX);
   Create table DataTable as 
   select * from connection to teradata
   (Select ID,VAR1,VAR2
     From MyTable
     where Date between '2021-01-01' and '2023-12-31'
     and (&lower lt ID le &upper)
  );
Quit;

 %mend;

This is not working as I expected and throwing an error. Is there a way I can pull this information using Teradata Macros?


Solution

  • You appear to be using SAS syntax in your pass thru Teradata code. I doubt that Teradata will let you use SAS's shortcut syntax for multiple comparisons.
    So just expand it.

    Convert something like

    (A op1 B op2 C) 
    

    in to its components parts.

    (A op1 B) and (B op2 C)