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?
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.
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
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;
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.
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.
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.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.