DATA Step, Macro, Functions and more

delete files with sas Data step

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

delete files with sas Data step

[ Edited ]

Hello guys,

 

I'm trying to delete multiple files that are contained in a dataset, the dataset  contains data like this:

 

Path

-----------------

/test/file1.txt

/test/file2.txt

/test/file3.txt

/test/file4.txt

/test/file5.txt

.....

 

so I want to do something like this

 

data _null_;

set path_table;

call symput('file',path);

x "rm -rf &file";

run;

 

but before I submit the command I wanted to test what I'm sending to the x window with something like this:

 

data _null_;
set work.files;
call symput('file',path);
%put &file;
run;

 

But I'm just getting one line in the log. what can I be possibly be doing wrong?


Accepted Solutions
Solution
‎09-21-2017 04:06 PM
Super User
Posts: 5,513

Re: delete files with sas Data step

[ Edited ]
Posted in reply to iscgonzalez

You would do better to use CALL SYMPUTX instead of CALL SYMPUT.  That way, the softtware automatically removes leading and trailing blanks from the macro variable it creates.  Even if you get the %PUT statement to execute the way you intend, it won't necessarily show leading and trailing blanks.

 

%PUT statements are never part of a DATA step.  You could easily use a PUT statement:

 

put path;

 

To use %PUT you would need to jump through quite a few hoops:  find out the number of paths in your data set, write a macro loop that goes through a DATA step reading just one observation, then executes %put.  If that's worth it, we can look at some details.

 

 One final thought about a trick that may work:  try changing the %PUT statement to read:

 

call execute('%put &file;');

 

CALL EXECUTE will execute macro statements immediately, so may be able to track the current value of &FILE.

 

Update:  Confirmed that the last idea above does actually work.

View solution in original post


All Replies
PROC Star
Posts: 7,480

Re: delete files with sas Data step

Posted in reply to iscgonzalez

Your macro variable won't resolve until the end of the datastep.

 

I would just create a variable and run it within the datastep (e.g (untested):

 

data _null_;
  set work.archivos2;
  length commd $80;

  let commd=catx(' ','x "rm -rf',path,'"');

  call execute(commd);

run;

 

Art, CEO, AnalystFinder.com

 

Super Contributor
Posts: 440

Re: delete files with sas Data step

Posted in reply to iscgonzalez

No need to shell out to the operating system e.g.

 

data have;
	length path $50;
	infile datalines;
	input path;
	datalines;
/folders/myshortcuts/Dropbox/Test/file1.txt
/folders/myshortcuts/Dropbox/Test/file2.txt
/folders/myshortcuts/Dropbox/Test/file3.txt
;
run;

data _null_;
	set have;	
	fname="temp";
    rc=filename(fname,path);
    if rc = 0 and fexist(fname) then
       rc=fdelete(fname);
    rc=filename(fname);
   
run;
Occasional Contributor
Posts: 5

Re: delete files with sas Data step

Posted in reply to ChrisBrooks

I think this might not be 100% useful for my case, because in the list that I have I also have directories with subdirectories and files in there.

 

I wanted to shell out because I wanted to use the -rf option on rm command.

 

Super User
Posts: 19,822

Re: delete files with sas Data step

Posted in reply to iscgonzalez

FDELETE is a better option, because X command can be disabled on many systems and the commands will vary based on OS, whereas FDELETE will not change.

Solution
‎09-21-2017 04:06 PM
Super User
Posts: 5,513

Re: delete files with sas Data step

[ Edited ]
Posted in reply to iscgonzalez

You would do better to use CALL SYMPUTX instead of CALL SYMPUT.  That way, the softtware automatically removes leading and trailing blanks from the macro variable it creates.  Even if you get the %PUT statement to execute the way you intend, it won't necessarily show leading and trailing blanks.

 

%PUT statements are never part of a DATA step.  You could easily use a PUT statement:

 

put path;

 

To use %PUT you would need to jump through quite a few hoops:  find out the number of paths in your data set, write a macro loop that goes through a DATA step reading just one observation, then executes %put.  If that's worth it, we can look at some details.

 

 One final thought about a trick that may work:  try changing the %PUT statement to read:

 

call execute('%put &file;');

 

CALL EXECUTE will execute macro statements immediately, so may be able to track the current value of &FILE.

 

Update:  Confirmed that the last idea above does actually work.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 230 views
  • 7 likes
  • 5 in conversation