BookmarkSubscribeRSS Feed
Universe
Calcite | Level 5

Hi,

I am trying sas code with unix commands to read all directories /sub directories(in unix - IPswitch) but few folders don't have permission to read so I am getting cannot access in the log those report folders need to be captured in a dataset along with folder level owner/group information. Could you please help with that to crack? 

Code used: 

 
Data all_files;
length  folder $600. file_name $256  owner $50 size 8 mod_date $12 mod_time $5  main_path$600. permissions $10 links 8  group $50 ;
main_path=compress(cats(folder||'/'||file_name));
if file_name ^=' ';
run;
%macro read_folders(base_dir);
 
filename dirlist pipe "find &base_dir -type d";
 
 
data directories;
infile dirlist truncover;
input  folder_path $600.;
if folder_path ne '';
run;
 
%if %sysfunc(exist(directories)) %then %do;
data _null_;
set directories nobs=num_dirs;
call symput('num_dirs', num_dirs);
run;
 
%if &num_dirs >0 %then %do;
 
 
  data _null_;
set directories ;
call symputx('current_folder'||left(_n_), strip(folder_path));
run;
 %do i=1 %to &num_dirs;
  filename filelist pipe "ls -lt --time-style=long-iso &&current_folder&i";
 
Data files_&i ;
infile filelist truncover;
length line $1024;
input line $1024.;
if substr(line,1,1)  in ('t','') then delete;
length permissions $10 links 8 owner $50 group $50 size 8 mod_date $12 mod_time $5 file_name $256 folder $600 datemodified 8;
permissions =scan(line,1,' ');
links=input(scan(line,2,' '),8.);
owner=scan(line,3,' ');
group=scan(line,4,' ');
size=input(scan(line,5,' '),8.);
mod_date=scan(line,6,' ');
mod_time=scan(line,7, ' ');
/*file_name=scan(line, -1, ' ');*/
length prefix $1024;
prefix=catx(' ',permissions,links,owner,group,size,scan(line,6, ' '),scan(line,7, ' '));
start_pos=lengthn(prefix) +2;
file_name=strip(substr(line,start_pos));
file_name=prxchange('s/^\d+\s*//', -1,file_name);
datemodified=input(catx(' ',mod_date,mod_time),anydtdtm20.);
format datemodified dateteime19.;
folder="&&current_folder&i";
main_path=cats(folder,'/',file_name);
if file_name^= ' ';
run;
 
proc append base=all_files data=files_&i force;
run;
 
%end;
 
Proc print data=all_files; title "All Files with details"; run;
%end;
%else %do;
 
%put ERROR: No directories found in &base_dir.;
%end;
 
%end;
%else %do;
%put ERROR: Unable to create directories dataset. Check the base directory path.;
%end;
 
%mend;
 
%read_folders(/home/apps/acctopts/PROCESSES);

 

3 REPLIES 3
JuanS_OCS
Azurite | Level 17

Hi @Universe ,

 

perhaps I am not understanding you, as you are requesting help with code, however it seems to me as your issue is with permissions. If the user that is owner of the SAS process does not have the right permissions, it won't be able to perform certain actions. You probably should align with our Unix admin.

 

This being said, I can see your code is quite elaborate for the required listing for which I commend you for.

However, since you are already using linux commands, perhaps you could use linux commands to do that job, much more simpler and with less lines of code. 


Some examples:

tree -nf  | awk '{printf $0} {system("stat -c \" [ %y ]\" " $NF )}'| sed -e 's#\([0-9][0-9]:[0-9][0-9]:[0-9][0-9]\).*#\1 ]#g'  2>/dev/null
ls -Rl --time-style=long-iso /path/to/directory

tree -D

 

ballardw
Super User

If you can't access the files from the LINUX/ UNIX/ any operating system command line why should the user be able to circumvent the system security features such as permissions to do things in SAS?!? 

 

I think the system administrator would be very cross with all involved.

 

If a user needs access then talk the admin about changing permissions.

Tom
Super User Tom
Super User

We cannot help you access files you are not allowed to access.

We could suggest better SAS code that what you showed.

If you just want to get a list of files you do not need to use operating system commands.

For example you could use this macro:

https://github.com/sasutils/macros/blob/master/dirtree.sas

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1485 views
  • 4 likes
  • 4 in conversation