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

Hello friends,

 

i have below programs that i created to delete few selected entire directories from specific dir (on linux system via EG).

 

this program running fine however, my goal is delete all directories that i have in test2 dataset  (have like 15 dir under test) with "rm -rf" command but its just deleting one dir when i run it,

 

how can i tell SAS that perform all command (rm -rf) for all directory?

 

 

data test (keep=files);

length access $ 20 files $ 200;

infile "cd /test; ls -lt;" pipe firstobs=2;

input access $ nx $ user $ group $ size $ month $ day $ time $ files;

run;

 

/*creating rm -rf command*/

data test2;

set test;

rmcmd=catx(' ','rm -rf'

              ,cats('/test/',files);

run;

 

 

/*rmcmd variable looks like this*/

 

rmcmd 

rm -rf sdfkjqfjnsvv

rm -rf skdfjspiu

rm -rf soivjasivuhqsvjnsvkjnsv

rm -rf sdlvjasvhqqdsdkvjbqsdvhb

 

 

/*trying to execute rm -rf commanf for all*/

data _null_;

set test2;

infile cmd pipe filevar=rmcmd;

input@;

run;

 

 

Thanks a lot...!!!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The easiest change is to use the FILEVAR option on the INFILE statement to execute the commands you have generated.

 

data test2;
  set test;
  rmcmd=catx(' ','rm -rf',cats('/test/',files));
run;

data _null_;
  set test2 ;
  infile cmd pipe filevar=rmcmd end=eof;
  do while (not eof);
    input;
    put _infile_;
  end;
run;

View solution in original post

7 REPLIES 7
ballardw
Super User

You might try the FDELETE function. You would not send the unix command just the name of the file and/or directory:

 

data _null_;
   set test2;
   rc=fdelete(fname);
   put rc=;
   msg=sysmsg();
   put msg=;
run;

where Fname above is just the name of the directory. The PUT statements show the results.

 

woo
Barite | Level 11 woo
Barite | Level 11
dir name I have to remove is bigger than 8 char and that's why may be I am receiving this error,


NOTE: In a call to the FDELETE routine, the fileref rm -rf
/test/sdfkjqfjnsvv exceeds 8 characters, and will be truncated.
rc=20004
msg=ERROR: No logical assign for filename RM.
ballardw
Super User

the length for any character type variable needs to be set long enough to hand the explected values. For the FDELETE you do not want the "rm -rf" as part of the name string as that is not the actual file or directory name.

 

Another approach, would be the X or call system statements, IF you have permissions to run X. Since I'm not running on unix I can't really test what you may be attempting to provide more direct examples.

Something like this perhaps:

data _null_;
   set test2;
   call system(rmcmd);
run;

 

 

Your pipe failing because INFILE is not really an executeable, as in it isnot called for each record in test2.

Tom
Super User Tom
Super User

The easiest change is to use the FILEVAR option on the INFILE statement to execute the commands you have generated.

 

data test2;
  set test;
  rmcmd=catx(' ','rm -rf',cats('/test/',files));
run;

data _null_;
  set test2 ;
  infile cmd pipe filevar=rmcmd end=eof;
  do while (not eof);
    input;
    put _infile_;
  end;
run;
woo
Barite | Level 11 woo
Barite | Level 11
tried same Tom 🙂 - worked fine - thanks a lot...

needed that end=eof + do while statement

Kurt_Bremser
Super User

Why not simply use call system to execute the rm commands?

data _null_;
set test2;
call system(rmcmd);
run;

I'd suggest to use absolute path names for the files to be removed.

Ksharp
Super User

I would like to make a shell file to contain all these command, and execute once for all.

data _null_;
file '/home/temp.sh';
put "rm -rf sdfkjqfjnsvv";
put "rm -rf skdfjspiu";
put "rm -rf soivjasivuhqsvjnsvkjnsv";
put "rm -rf sdlvjasvhqqdsdkvjbqsdvhb";
run;

x "./home/temp.sh";


sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 4658 views
  • 2 likes
  • 5 in conversation