简体   繁体   中英

SAS macro to Teradata syntax

I have a SAS macro that, when given different arguments, creates several tables. It looks something like this:

%macro create_tables(key, value);
data WORK.TABLE_&key.:
    set WORK.MAIN_TABLE;
        where col = &value.;
        col_&key. = 1;
        drop col;
%mend create_tables;

The key parameter/macro variable is injected into the table name. I call this macro several times with different keys and values.

I want to convert this piece of code into Teradata syntax. I can create multiple tables for every key and value, but I have 30+ keys and values. What would be the best way to achieve this in Teradata? Would creating multiple tables be more efficient? The number of rows for each table created will be between 1 million and 2 million, and the MAIN_TABLE has 30+ million rows.

I agree with Tom that it is not obvious why you create a lot of new tables that just contain a certain part of your original data. So, if you want to have these extra columns (one column per value of column col) then imho the following code seems to be the best solution in SAS. Here, a view is created by a SAS macro where you can specify one or more values via the macro parameter KEY. For each value there will be a new column with the values 1 and 0 in the resulting view. If you want only the rows with one certain value then you can do that based on this view.

%macro create_colkeys (KEY=);

%let NUMWORDS = %sysfunc(countw(&KEY.));
%do i=1 %to &NUMWORDS.;
  %let KEY_&i.=%scan(&KEY,&i.);
%end;

proc sql;
  create view col_flags as
  select 
     %do i=1 %to &NUMWORDS.;
        case when col="&&KEY_&i." then 1 
                              else 0 
        end as col_&&KEY_&i..,
     %end;
     *
  from main_table;
quit;
%mend;

%create_colkeys(KEY = abc def xyz);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM