How to move a table from SAS to Teradata SQL Assistant? - sas

I would like to ask you how to move a table from SAS to TeradataSQL Assistant.
What I have done in SAS is to define a libname, then create the table that I want to move in Teradata.
libname NAME "/path"
proc sql;
create table WORK.EXAMPLE as(
select *
from DATASET
);
quit;
However, I do not know if I need to connect SAS to Teradata in this way:
libname NAME teradata USER=tduser PASSWORD=tdpasswd SERVER=TDServ ;
proc sql;
connect to teradata (
tdpid=“” user=“” password=“”);
create table WORK.EXAMPLE as
select * from connection to teradata
(select * from DATASET
); quit;
My questions are:
in the second libname, where should I consider the path?
which one of the code above should I consider and why?
how can I 'call' this table in Teradata? I tried with SELECT * FROM WORK.EXAMPLE, but it does not exist.
Thank you for your help.

You can either connect to Terdata using the LIBNAME statement
libname mylib TERADATA .... ;
or the CONNECT statement in PROC SQL.
proc sql;
connect to teradata .... ;
In fact you can even use the libref created in a previously created LIBNAME statement in your connect statement.
libname mylib TERADATA .... ;
proc sql;
connect using mylib ;
Generally I find it easiest to use PROC APPEND to copy data into Teradata.
libname td TERADATA ... ;
libname mysas 'path to where my SAS datasets live';
proc append base=td.target_table data=mysas.source_table force;
run;
If the target table doesn't exist then SAS will create it. (In which case take care as you might not want the default variable types or the primary index that will get created that way.).

Related

SAS - Can I create a table of all table names within a library and then all variables within those tables?

I would like to create a new table with all tables contained within a library and the variables within each of those tables. I know I can use something like the below to get the table name but I cant find much on getting each variable. I have multiple libraries and each has potentially hundreds of tables. Any help really appreciated.
proc sql ;
create table mytables as
select *
from dictionary.tables
where libname IN ('WORK','SPDSWORK',etc)
order by memname ;
quit ;
Just use PROC CONTENTS with the special _ALL_ member name. Use the NOPRINT option to suppress the output and the OUT= option to name the dataset with the contents information.
proc contents data=mylib._all_ noprint out=contents;
run;
Use distionary.columns instead.
proc sql ;
create table mytables as
select *
from dictionary.columns
where libname IN ('SASHELP')
order by memname ;
quit ;

How execute a Teradata Store Procedure in SAS program?

I have a store procedure in Teradata and I trying execute with "PROC SQL" in SAS but I don't have results.
libname S_SQUEMA teradata server="100.00.00.100" user=##### pwd=#### schema=S_SQUEMA fastload=YES;
PROC SQL;
CALL S_SQUEMA.SP_TEST(year);
QUIT;
Thanks.
To run something in Teradata you need to connect to Teradata using a CONNECT statement and then push the Teradata code to the database using the EXECUTE statement. So your code will look something like this:
proc sql ;
connect to teradata ..... ;
execute ( call S_SQUEMA.SP_TEST(year) ) by teradata;
quit;
If you already ran your LIBNAME statement to define your S_SQUEMA libref then you can just use that to make the connection.
proc sql ;
connect using S_SQUEMA ;
execute ( call S_SQUEMA.SP_TEST(year) ) by S_SQUEMA;
quit;

Can I use wildcards in dataset names for PROC CONTENTS?

On the SAS server we have a library that contains thousands of datasets. I want to catalog the contents of a subset of these, all of which have names that begin with "prov". Can I use a wildcard to specify this?
I tried:
PROC CONTENTS DATA=library.prov*;
RUN;
But that just produces a log with this error message:
ERROR: File LIBRARY.PROV.DATA does not exist.
I also tried library.prov%, and that gave the same error.
There are over 100 datasets that start with "prov" so I really don't want to have to do them one at a time. Any ideas?
Depending on what information you want that the CONTENTS procedure produces you could just use the DICTIONARY metadata views.
proc sql ;
create table want as
select *
from dictionary.columns
where libname = 'LIBREF'
and memname like 'PROV%'
;
quit;
Use a WHERE data set option.
proc contents data=sashelp._all_ noprint out=class(where=(memname like 'CLASS%'));
run;
When you specify the keyword _ALL_ in the PROC CONTENTS statement, the step displays a list of all the SAS files that are in the specified SAS library.
Example :
PROC CONTENTS DATA=libref._ALL_ NODS;
RUN;
But to open only the datasets that begin with prov you can use the SQL and add CONTAINS to WHERE e.g:
proc sql ;
create table mytables as
select *
from dictionary.tables
where libname = 'WORK'
order by memname ;
quit ;
Now just run:
PROC CONTENTS DATA mytables;
RUN;
I may be using a different version of SAS check if you have the library SASHELP if so try this based on my note in your comment on the previous response you may see that this works out for you:
proc sql outobs=100;
create table see as
select distinct libname,memname,crdate,modate from sashelp.vtable
where libname='LIBRARY' and memname like 'PROV%'
order by memname;
quit;

proc sql outobs= triggers SAS warning

We currently use the %runquit macro function as detailed here (http://analytics.ncsu.edu/sesug/2010/CC07.Blanchette.pdf). The %runquit macro is shown below. It basically stops running any more SAS code when an error is encounterd, and can be used as a replacement for both the run and quit statements:
%macro runquit;
; run; quit;
%if &syserr %then %abort cancel;
%mend;
Because using the outobs statement in proc sql triggers a system error (even when the nowarn option is specified) it means we are unable to use the %runquit macro when we need to use the outobs= option.
The below example will generate the following warning message:
proc sql noprint outobs=3 /*nowarn*/;
create table tmp as
select age, count(*) as freq
from sashelp.class
group by 1
order by 2 desc
;
%runquit;
WARNING: Statement terminated early due to OUTOBS=3 option.
Thank you SAS for the completely unnecessary warning. The behaviour is obviously expected because I explicitly wrote code to ask for it. I don't see warnings given when we specify inobs= and outobs= on a set statement. Why does proc sql get the special treatment?
Is there any way to disable the warning issues by the outobs= option in proc sql? Alternatively, is there another way to limit the output rows from proc sql that will not generate an error?
Assuming you are okay with the full SQL statement executing, you can get around this with a data step view that contains the obs limitation.
proc sql noprint ;
create table tmp as
select age, count(*) as freq
from sashelp.class
group by 1
order by 2 desc
;
%runquit;
data tmp_fin/view=tmp_fin;
set tmp(obs=3);
%runquit;
Or make the SQL statement a view and use the data step to make the data set.
proc sql noprint ;
create view tmp_view as
select age
, count(*) as freq
from sashelp.class
group by 1
order by 2 desc
;
quit;
data tmp;
set tmp_view(obs=3) ;
run;
This might be one of your options considering I/O is not a huge constraint, here the reset outobs= option with nowarn does the trick but at IOs cost.
proc sql;
create table test as
select * from sashelp.class;
reset outobs=10 nowarn;
create table test1 as
select * from sashelp.class;
quit;

Load SAS dataset into Teradata table using Fast LOAD

I am trying to load SAS dataset into a teradata table using FASTLOAD utility. This works fine in some cases, but I want to separate the error tables and create them in my own/other database in teradata environment.
Could some one provide me the syntax (I do know it but it's not working) for how to make it possible?
Any method is fine either using proc sql command or proc append command. Thanks in advance.
You can use the LOGDB libname option to tell SAS into which database the log files should be created. By default they are created in the same database as the table being created (named as the target table named plus the three character suffixes you've discovered). Using the info provided in your comments, try this:
/* Delete any exisiting log files for table TPT_LD_TEST */
libname TPTLOAD TERADATA
SERVER=TDServ DATABASE=TPTLOAD
USER=tduser PASSWORD=tdpasswd1
;
proc delete data=TPTLOAD.TPT_LD_TEST_ET;
run;
proc delete data=TPTLOAD.TPT_LD_TEST_UV;
run;
proc delete data=TPTLOAD.TPT_LD_TEST_RS;
run;
libname TPTLOAD clear;
/* Define connection to target database */
LIBNAME TDSERV TERADATA
SERVER=TDServ
USER=tduser PASSWORD=tdpasswd1
LOGDB=TPTLOAD;
/* Truncate target table if necessary */
proc sql noprint;
delete from TDSERV.TPT_LD_TEST;
quit;
proc append base=TDSERV.TPT_LD_TEST(fastload=yes tpt=yes)
data=work.FastLoad;
run;
I added some code to delete any existing rows in the target table (a requirement for FASTLOAD).
If you have DROP TABLE and CREATE TABLE rights on your target database, it might be safer to drop and re-create the table so you can guarantee the structure and explicitly name the table index.
/* Delete target table if it exists */
proc delete data=TDSERV.TPT_LD_TEST;
run;
data TDSERV.TPT_LD_TEST
(fastload=yes tpt=yes
dbcreate_table_opts='primary index(index_column_name)'
)
set work.FastLoad;
run;
And in either case, be sure to remove any duplicate records from your source dataset; those will be written to your error files (as well as any records that fail other constraints).
PROC DELETE is a handy device because it will not create an error if the target table does not exist.