DATA Step, Macro, Functions and more

filename function with user, password on linux

Reply
Regular Contributor
Regular Contributor
Posts: 156

filename function with user, password on linux

[ Edited ]

Dear all,

 

My SAS program runs on a linux server. I need to access files in a directory that my user (abc) cannot acces due to user rights. So I use another user (xyz) that has read rights on the directory:

 

filename fref FTP "/project/anotherprpject/src/file_I_want_to_access.txt" 
USER = "xyz" PASS = "{SAS002}3427979DHKS79237SDHH323";

This works.

[Note: if I use DISK instead of FTP as device the statement does not work, because DISK does not allow the option USER]

 

Now. I'd like to do the same with the filename function. Can anybody give my the right syntax? Seems like I'm getting the '  , " etc. wrong.

 

data _null_;   
     length fref $8 filename $80;     
     rc = filename("fref", "/project/anotherproject/src/file_I_want_to_access.txt", "ftp",
     'USER = "xyz", PASS = "{SAS002}3427979DHKS79237SDHH323"');    
     if not fexist(fref) then do;         
          call symput ("l_sErrMsg", "file does not exist.");             
          call symput ("l_nErrRC", "-1");         
          return;        
     end;
run;

 

This doesn't work. I get the error:

 

Message:ERROR: Invalid option name ,.

 

Best wishes

Eva

Respected Advisor
Posts: 4,173

Re: filename function with user, password on linux

[ Edited ]

According to documentation https://support.sas.com/documentation/cdl/en/hostunx/61879/HTML/default/viewer.htm#a000195127.htm 

the valid syntax for the filename function is:

FILENAME(fileref, filename <,device-type<,"host-options"<,dir-ref>>>)

 

According to this syntax (and also the example given in the docu link) you need to single quote the credential bits.

rc = filename(fref, DISK, "/project/anotherproject/src/file_I_want_to_access.txt", 
     'USER="xyz"', 'PASS="{SAS002}3427979DHKS79237SDHH323"');   

What I'm not so sure about: Can you use a SAS encoded password this way? You need to try. I have my doubts that it will work but curious what you will tell us.

Regular Contributor
Regular Contributor
Posts: 156

Re: filename function with user, password on linux

[ Edited ]

Dear Patrick,

 

meanwhile I found out, that DISK doesn't allow user and password (I edited this in my orginal post). So I use ftp as described in the documentation for UNIX and filename function.

 

So I tried this with your example:

 

rc = filename (fref, FTP, "/project/anotherproject/src/file_I_want_to_access.txt, 
'user="xyz"',
'pass="{SAS002}3427979DHKS79237SDHH323"');

 

Then I tried this after I read the documentation and followed the adivice there (changed filename and device in order; set fref and ftp in quotation marks):

 

rc = filename ("fref", "/project/anotherproject/src/file_I_want_to_access.txt, 
"ftp",
'user="xyz"',
'pass="{SAS002}3427979DHKS79237SDHH323"');

 

In both cases the Error message is:

 

Message:ERROR: Invalid logical name.

 

Respected Advisor
Posts: 4,173

Re: filename function with user, password on linux

Is there a double quote missing after the path/filename?

 

 

Regular Contributor
Regular Contributor
Posts: 156

Re: filename function with user, password on linux

Yes, you're right.

 

I tried:

 

rc = filename ("fref", "/project/anotherproject/src/file_I_want_to_access.txt", 
"ftp",
'user="xyz"',
'pass="{SAS002}3427979DHKS79237SDHH323"');

and:

 

rc = filename ("fref", "/project/anotherproject/src/file_I_want_to_access.txt", 
"ftp",
'user="xyz",
pass="{SAS002}3427979DHKS79237SDHH323"');

and got the message:

 

Message:ERROR: Invalid logical name.

Then I added the host name:

 

rc = filename ("fref", "/project/anotherproject/src/file_I_want_to_access.txt", 
"ftp",
'user="xyz",
host="my.host.name"
pass="{SAS002}3427979DHKS79237SDHH323"');

Added the port number (7551) with a blank and in the second example with a colon:

 

rc = filename ("fref", "/project/anotherproject/src/file_I_want_to_access.txt", 
"ftp",
'user="xyz",
host="my.host.name 7551"
pass="{SAS002}3427979DHKS79237SDHH323"');
rc = filename ("fref", "/project/anotherproject/src/file_I_want_to_access.txt", 
"ftp",
'user="xyz",
host="my.host.name:7551"
pass="{SAS002}3427979DHKS79237SDHH323"');

 

and in all three cases got the message:

 

ERROR: Invalid option name                        XÌ6Ÿ+  p¥žŸ+  ðEÀŸ+          €    @fÐ6Ÿ+         ¦Réåž+  ðEÀŸ+          
                     UÀŸ+  r1Ÿ+              Ÿ+          0fÐ6Ÿ

I have no more ideas... do you?

 

Respected Advisor
Posts: 4,173

Re: filename function with user, password on linux

[ Edited ]

I believe you can't use the SAS encoded password for your FTP connection. Try without encoding (just as a test, not as the final solution).

 

Let's go back to your initial problem:

"My SAS program runs on a linux server. I need to access files in a directory that my user (abc) cannot acces due to user rights. So I use another user (xyz) that has read rights on the directory:"

 

The way I normally get my UNIX/Linux commands right: I'm using Putty and try them first via command prompt.

 

In your case: Can you FTP from the same machine? Try first in Putty and only if that works try out of SAS.

 

I believe another option is to execute a command with the credentials of another user (elevated priviledges).

https://linuxacademy.com/blog/linux/linux-commands-for-beginners-sudo/  

 

I haven't done the following myself so just throwing ideas:

- Login via Putty with your normal SAS user

- Issue SUDO to change to the user with access priviledges to your file

- Test file access (eg. issuing a CAT command for your file)

- If all of the above works:

  - Use a PIPE: 
    https://support.sas.com/documentation/cdl/en/hostunx/67929/HTML/default/viewer.htm#n1ceb0xedanuj3n19...

    Write either a Shell script or combine all commands directly in the command bit of the Filename Pipe syntax.
    (I believe you need just a SUDO and a CAT).
  - Use the fileref in your data step reading the file into SAS

 

Much easier would be...

Get read access for your normal SAS user to this directory and file

 

    

 

 

Ask a Question
Discussion stats
  • 5 replies
  • 392 views
  • 0 likes
  • 2 in conversation