Question

I have the following authentication lines hard coded :

 1. libname xxx odbc user=userName password=passWord dsn=myDsn
    schema=mySchema;

 2. proc sql;  connect to xxx(dsn=myDsn user=userName password=passWord
    );

I want to keep the userName and passWord in an external .txt files and refer to them in the code . So that I do not keep them hard coded as it is right now . How can I do this ?

EDIT: My main objective is to be able to change/maintain my passwords more easily, NOT to improve security.

Was it helpful?

Solution 2

You can use a data-step to read in a file containing password information, see the following for an example. This code assumes the password file is formatted as username|password i.e. pipe delimited and only containing one line.

filename pw ""; /*<insert name of pw file here>*/

data _NULL_;
  infile pw dlm='|'; /* Change dlm if not pipe */
  input 
  uname : $50.
  pw : $50.;
  call symputx('uname',uname);
  call symputx('pw',pw);
run;

libname xxx odbc user=&uname. password=&pw. dsn=myDsn schema=mySchema;

proc sql;  
  connect to xxx(dsn=myDsn user=&uname. password=&pw.);

If you are working on a metadata server you can use authentication domains to hold password information instead.

In that case you would alter your password in the metadata and then refer to it in code using the authdomain= option, which would return the password to SAS without it being displayed.

e.g.

libname xxx odbc authdomain=MY_ODBC_AUTHDOMAIN dsn=myDsn schema=mySchema;

OTHER TIPS

If you're primarily concerned with ease of changing (or perhaps sharing the file among several users), you can use a %include file and/or a macro stored in your MAUTOCALL library to connect. IE

(text file)

%let con_str=dsn=myDsn user=userName password=passWord;

(SAS code)

proc sql;  
  connect to xxx(&con_str.);

If you put that in a macro, and compiled it without the SOURCE option, you could hide the values from whomever was running it.

You also can use PROC PWENCODE to encode the password, and then use the encoded password in your program. From the documentation:

filename pwfile "external-file";

proc pwencode in='mypass1' out=pwfile;
run;

then

filename pwfile 'external-filename';
options symbolgen;

data _null_;
   infile pwfile obs=1 length=l; 
   input @;
   input @1 line $varying1024. l; 
   call symput('dbpass',substr(line,1,l)); 
run;
libname x odbc dsn=SQLServer user=testuser password="&dbpass";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top