BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
aj34321
Quartz | Level 8

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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;

View solution in original post

5 REPLIES 5
Shmuel
Garnet | Level 18

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;

aj34321
Quartz | Level 8
Hi Shmuel,

How to handle Sticky Bits, UID & GID's with this code? Thanks your guidance.
Kurt_Bremser
Super User

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.

aj34321
Quartz | Level 8
Thank you so much. This small code really awesome.
How to Concatenate Values

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 1914 views
  • 1 like
  • 3 in conversation