简体   繁体   中英

SAS connect to Teradata - Using 2 accounts (switch)

Can someone please help? I have not used SAS in a few years and need some assistance with connecting to Teradata.

I want to connect to Teradata using ACCT1 if the time of day is between 7pm-6:59am or with Acct2 if the time of day is between 7am-6:59pm.

%let
    acct1="mismktdev"
        acct2="mismktprod"


%include
%macro t_cnnct;  
    options nomprint;
    connect to teradata (tdpid="&tpidxyz" user="&misuid"
    password="&mispwd" account="&acct1"  mode=teradata);
    options mprint;

proc sql;
  connect to teradata (user="&terauser" password="&terapwd"  mode=teradata);
  execute (SET QUERY_BAND = 'Application=PrimeTime;Process=Daily;script=pt_add_history_v30.sas;' for session ) by teradata;
  %mend t_cnnct;

proc sql;
    Sel * from tblname;

You can use %let timenow=%sysfunc(time(), time.); to get the time at which the program is running and then in you macro do something like :

%macro test();
%let timenow=%sysfunc(time(), time.);
%put &timenow;
%if (&timenow > '19:00'T and &timenow < '06:59'T) %then %do;
    /* Your Code for 7pm - 6:59am Here*/
%end; 

%else %do;
    /*Code for 7am - 6:59pm here*/
%end;
%mend;

%test();

Your idea to use SAS macro variables is just right. Here is a macro to define all the global SAS macro variables you need (including changing the "account" string):

%macro t_cnnct;
%global tdserver terauser terapwd teraacct teradb;

%let tdserver=myTDPID;
%let terauser=myuserID;
%let terapwd=mYTDpassword;
%let teradb=myTDdatabase;

%let now=%sysfunc(time());
%if    &now >= %sysfunc(inputn(07:00,time5.)) 
   and &now <= %sysfunc(inputn(19:00,time5.))
  %then %let teraacct=mismktdev;
  %else %let teraacct=mismktprod;
%mend t_cnnct;

Note the SAS macro variable values are specified without double-quotes! Use double-quotes when they are referenced in your code.

Next, just run the macro before your PROC SQL code to set the variables and use those variables in your connect string:

%t_cnnct;

proc sql;
  connect to teradata (user="&terauser" password="&terapwd" account="&teraacct" 
                       server="&tdserver" mode=teradata);
  execute (
     SET QUERY_BAND = 'Application=PrimeTime;Process=Daily;script=pt_add_history_v30.sas;' for session
     ) by teradata;

  create table mySASds as
  select *
  from connection to teradata (
     select *
     from &teradb..tablename
     );
quit;

Note that the tdpid= option you were using is an alias for the server= option (which I prefer). Also, the query band you set will remain in effect for the entire PROC SQL run.

And here is an example of a SAS libref that uses the same macro variables:

libname myTD teradata user="&terauser" password="&terapwd" account="&teraacct" 
                      server="&tdserver" database="&teradb" mode=teradata);

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