SAS Conditional Libname Assignments and Error Log - sas

I am using SAS Enterprise Guide 6.1 and am utilizing the "Submit SAS Code when server is connected" capability under Tools, SAS Programs to submit all of my personal credentials, libnames. Yes, I know there is probably an easier way to do this with SAS Management Console, but that is not an option right now.
I have several Teradata libraries I need to assign within each project, but the problem is that sometimes I (or more commonly somebody else on my team) will change my password and forget to change it in the start-up code. This results in several incorrect attempts and locks me out immediately. I wish to do a few things:
Create a conditional libname assignment that will execute all the libnames if the credentials are correct.
If the credentials are incorrect, the libnames are not executed (so that I don't lock myself out).
Since the SAS code in the "Submit SAS Code when server is connected" section doesn't seem to generate a log, I wish to send myself an email with the SAS Log attached (only if the credentials fail).
Kill the Server connection (if credentials error out) to avoid further attempts to assign libraries.
Here is my attempt, I'm having trouble attaching the log and setting up the email statement.
*Define personal credentials;
%let myemail=gollum#middleearth.com;
filename temp email "&myemail";
*Define Teradata credentials;
%let tera_user=Gollum; /*Teradata Username*/
%let tera_pwd=#filthy_hobbitses; /*Teradata Password*/
*Conditionally assign libraries;
%macro libsetup();
libname library1 teradata user=&tera_user password="&tera_pwd" tdpid=terap schema=library1 fastload=yes bulkload=yes fastexport=yes;
%if &syslibrc=0 %then
%do;
libname library2 teradata user=&tera_user password="&tera_pwd" tdpid=terap schema=library2 fastload=yes bulkload=yes fastexport=yes;
libname library3 teradata user=&tera_user password="&tera_pwd" tdpid=terap schema=library3 fastload=yes bulkload=yes fastexport=yes;
*more library statements here;
%end;
%else
%do;
data _null_;
file temp
subject="TERADATA CREDENTIALS ERROR"
attach=("put SAS LOG filename here");
put 'Teradata Login Failed. SAS LOG Attached.';
%abort abend;
%end;
%mend libsetup;
%libsetup;
Thanks.

Can't tell if your looking for suggestions on alternative approaches, or for help with the emailing.
For emailing, something like below (untested) should work:
filename __mymail email
to="gollum#middleearth.com"
from="gollum#middleearth.com"
subject="credential error"
attach="/home/mylog.log"
;
data _null_;
file __mymail;
put "Hi!";
run;
Note the server where SAS is executing has to have access to a mail server, and actual code needed may vary with mail protocol, etc.
For attaching the log, will probably need to use PROC PRINTTO to write your log to a file, and then use PROC PRINTTO again to let go of the file before you email it.
HTH

Related

SAS Rename file on SFTP

my task in SAS is to upload a file via SFTP with extension tmp and after the upload is finished to rename it to csv. As I am no adminstrator of the server my debugging output is limited and I am struggeling with the correct implementation. The following code generates no error in the SAS Log but does not rename the file:
%let host=...;
%let sftpUser = ...;
%let filename_tmp=20160301-test01-sas.tmp;
%let filename=20160301-test01-sas.csv;
%let sftpPath=...;
FILENAME test SFTP "&sftpPath.&filename_tmp."
HOST="&host."
USER="&sftpUser."
DEBUG;
proc export data=.... outfile=test dbms=csv replace;
run;
data _null_;
rc=rename("test&sftpPath.&filename_tmp.", "test&sftpPath.&filename.", 'file');
run;
I already read the docs for filename and rename but I could not get a clue on how to combine both statements - any help or hints or alternatives are greatly appreciated.
Thanks
Stephan
There is a script running on the destination server scanning for csv files and move them every minute so there is the danger that a file is moved when the upload is not completed.
I'm pretty sure that there's no danger of the file being moved, as it should be locked until the upload completes. Just try it.

can PC SAS be used to bypass SAS EG connection to Unix session?

We've migrated our data warehouse and we are being asked to use SAS EG to connect to a metadata server where all our libraries are.
Is there a way to still use PC SAS to connect to those libraries ? If so, any hints on how to do this ?
I prefer using Base SAS for various reasons, one of them being that with SAS EG, if our metadata server is down (which happens a lot), then I can't authenticate my session, thus I'm screwed.
Also, most of my SAS coding doesn't require me to access that specific data warehouse so I'd rather not be tied to SAS EG.
Yes of course you can connect to a metadata defined library from PC SAS - just two statements required!
options metaserver="YourMetaserver.domain"
metaport=8561 /* as appropriate */
metauser="YourMetadataUserID"
metapass="YourMetadataP*ssw*rd";
libname YourLibref meta library="The library name given in metadata";
Personally I prefer to work with librefs than remember the library names so I wrote the below to assign as appropriate (you need to be connected to the metadata server first, via the options statement above)
%macro assign_lib(
libref= /* libref that needs to be assigned */
);
%if %sysfunc(libref(&libref)) %then %do;
data _null_;
length lib_uri LibName $200;
call missing(of _all_);
nobj=metadata_getnobj("omsobj:SASLibrary?#Libref='&libref'",1,lib_uri);
if nobj=1 then do;
rc=metadata_getattr(lib_uri,"Name",LibName);
call symputx('LIB',libname,'L');
end;
else if nobj>1 then do;
putlog "ERROR: More than one library registered with libref &libref";
end;
else do;
putlog "ERROR: Library &libref not found in metadata";
end;
run;
libname &libref meta library="&lib";
%if %sysfunc(libref(&libref)) %then %do;
%put WARNING: Library &libref not assigned!;
%end;
%end;
%else %put NOTE: Library &libref is already assigned;
%mend;
use as follows:
%assign_lib(libref=SVRLIBRF)
Mostly, this is a question for your site's Data Warehouse Administrators (the people who control access to your data warehouse). It may, or may not, be possible to connect directly to the data warehouse, depending on how it is set up, and how permissioning is done. Oftentimes the answer is no, and it's set up specifically to avoid allowing this (as that's one easy way to handle authentication).
That aside, your second point does not mean you can't use Enterprise Guide and avoid the metadata server. In Enterprise Guide, you can select which server to run code on, which should include the option for the "local server", which is your local Base SAS installation. (In fact, that's how I use Enterprise Guide 100%.) So, if the metadata server is down, or you for other reasons want to avoid connecting to it, you can simply open EG with a Local Connection profile and run code locally.
There are a couple of big IF's.
If you have a license to run SAS on your desktop.
If you have a license for the proper ACCESS engine on your desktop SAS.
The data warehouse/data base has permissions for you to log in remotely through the ACCESS engine.
If all of those are true, then yes. If not, no.

exporting sas stored process output

I created a stored process but I want to export the output to Excel. My usual export statement doesn't work in the stored process.
%let _ODSDEST=none;
%STPBEGIN();
data x;
set sashelp.class;
run;
proc export data=x outfile = "//my documents/sp_test.xlsx" dbms=xlsx replace;
sheet="table1";
run;
* Begin EG generated code (do not edit this line);
;*';*";*/;quit;
%STPEND;
Is there a way to get this to work in the stored process?
One way to have a stored process return an excel file (actually in this case it's an xml file that excel will happily open) is to use ODS to output tagsets.excelxp (xml).
When you do this, you can use stpsrv_header to modify the HTML header. The first statement tells the browser to open the file with excel, the second tells it the file name. I believe for this header modification to work the stored process needs to deliver streaming results, not package results. But I could be wrong.
When I run below, I get a file download dialog box from the browser, allowing me to open or save the file. I'm running from Stored Process Web App, but should work fine when called from Information Delivery Portal.
%let _odsdest=tagsets.excelxp;
%let rc=%sysfunc(stpsrv_header(Content-type,application/vnd.ms-excel));
%let rc=%sysfunc(stpsrv_header(Content-disposition,attachment%str(;) filename=MyExcelFile.xls));
%stpbegin()
proc print data=sashelp.shoes (obs=&obs);
run;
%stpend()
did u check with your spelling proc exportd and outfile='mypath/my documents/myoutpt.xlsx' dbms=xlsx or outfile='mypath/my documents/myoutpt.xls' dbms=xls?? U can try with ODS also.
You can also try setting your STP up as a streaming web service, removing the %STPBEGIN and %STPEND macros, and sending to _webout using this macro: https://core.sasjs.io/mp__streamfile_8sas.html
The benefit of this, is that your code will subsequently work on Viya as well.

How to suppress error messages from SAS proc sql execute statement

I use SAS proc sql execute statement to run sql and access tables on Oracle server. In order to let the script run automatically, I normally drop tables or indice before creating them. Sometimes however, when the drop sql code runs, the table or index doesn't exist. An error message will be logged in SAS about dropping something that doesn't exist. But since the SAS code will continue after the drops, this kind of error message doesn't matter at all. I want to suppress these error messages, stop SAS from logging them because I don't care about them. How can I do this in proc sql?
Thanks for any help.
I'm not aware of an option to supress this single type of error. There are a couple of options though.
The best bet is to clean up your code so you check to see if the data set exists before trying to act on it:
%if %sysfunc(exist(&name_of_data_set)) %then %do;
You could also redirect all logging during these steps where you get messages you don't want with proc printto, but you would potentially lose valuable information.

Determine the folder of a SAS source file

When I open a SAS file in enterprise guide and run it, it is executed on the server. The source file itself is located either on the production site or the development site. In both cases, it is executed the same server however. I want to be able to tell my script to store results in a relative folder. But if I write something like
libname lib_out xport "..\tmp\foobar.xpt";
I get an error, because the working folder of the SAS Enterprise Guide process is not the location of my source file, but a folder on the server. And the folder ..\tmp does not exist there. Even if it would, the server process does not have write permission in that folder.
I would like to determine from which folder the .sas file was loaded and set the working folder accordingly. In one case it's S:\Development\myproject\sas\foobar.sas and in the other case it's S:\Production\myproject\sas\foobar.sas
It this possible at all? Or how would you do this?
Depending on the way EG is configured, you may be able to use something like the syshostname global macro variable to determine where to save your results:
%macro sasdir;
%global sasdir;
%if "&syshostname" eq "mydevelopmenthost" %then %do;
%let sasdir = S:\Development;
%end;
%else %if "&syshostname" eq "myproductionhost" %then %do;
%let sasdir = S:\Production;
%end;
%mend;
%sasdir;
libname lib_out xport "&sasdir\myproject\sas\tmp\foobar.xpt";
If not, try looking at what other global or automatic macro variables may be able to help you by doing a:
%put _all_;
Hope this helps
Cheers
Rob
OK, this isn't going to answer your question exactly, but I have this macro easily available so I thought I would share it. From here you would just need to do a little string processing.
%macro progName;
%* Returns the name of current program;
%let progPath = %sysfunc(GetOption(SysIn));
%* if running in interactive mode, the above line will not work, and the next line should;
%if %length(&progPath) = 0 %then %let progPath = %sysget(SAS_ExecFilePath);
%str(&progPath)
%mend progName;