Looking for a macro that converts unix file permissions string to a number
Like -
-rwxr-xr-x should return 755
-rwxrwxr-x should retrun 775
-rwx------ should return 700
Has anyone something like this readily available?
Rgds, Anil
data perm;
input perm $10.;
array vn v1-v9;
do i=1 to 9;
j=i+1;
x=substr(perm,j,1);
if x='-' then vn(i) =0; else
if x='r' then vn(i) =4; else
if x='w' then vn(i) =2; else
if x='x' then vn(i) =1;
end;
code=(v1+v2+v3)*100+(v4+v5+v6)*10+v7+v8+v9;
keep perm code;
cards;
-rwxr-xr-x
-rwxrwxr-x
-rwx------
; run;
data perm;
input perm $10.;
array vn v1-v9;
do i=1 to 9;
j=i+1;
x=substr(perm,j,1);
if x='-' then vn(i) =0; else
if x='r' then vn(i) =4; else
if x='w' then vn(i) =2; else
if x='x' then vn(i) =1;
end;
code=(v1+v2+v3)*100+(v4+v5+v6)*10+v7+v8+v9;
keep perm code;
cards;
-rwxr-xr-x
-rwxrwxr-x
-rwx------
; run;
The sticky bits occupy a different position in the octal value:
data perm (keep=perm perms_oct);
input perm $10.;
perms_oct = 0;
do i = 0 to 2;
read = (substr(perm,i*3+2,1)='r');
write = (substr(perm,i*3+3,1)='w');
exec = (substr(perm,i*3+4,1) in ('x','s'));
setb = (substr(perm,i*3+4,1) in ('S','s'));
i1 = abs(i - 2);
oct = read * 4 + write * 2 + exec * 1;
if oct ne 0 then perms_oct = perms_oct + oct * 8 ** i1;
if setb ne 0 then perms_oct = perms_oct + (((setb*2) ** i) * 512);
end;
format perms_oct octal4.;
cards;
-rwxr-xr-x
-rwxrwxr-x
-rwx------
-rwsr-sr-x
---------S
;
run;
proc print data=perm noobs;
run;
result looks like this:
perms_ perm oct -rwxr-xr-x 0755 -rwxrwxr-x 0775 -rwx------ 0700 -rwsr-sr-x 3755 ---------S 4000
Edit: slightly changed my code to accomodate the fact that the SAS ** operator doesn't like 0 ** 0.
Use the stat command for files:
stat -c "%a %n" filename
on Linux. It delivers the permissions in numerical form, followed by the filename.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.