I need to copy mutiple files from one location to another where the date within the filename changes on a daily basis and I would need to copy a whole months worth at a time. There could also be other files with a different month within the date part that I don't want to copy.
Typical file names would be File_01May2016.zip, File_07May2016.zip, File_09May2016.zip, File_11May2016.zip, File_01Jun2016.zip. I would want to copy tghe four files containing the value May2016 (which I have set in a macro field called &MONTH) into a location where the directory is called 'MAY2016.
Once they have been copied to the correct location I need to unzip all the files within that new location.
Any advice on how to code this would be much appreciated.
Thanks
Louise
Hi All,
Thanks to everyone that posted a suggestion - we have managed to solve the issue ourselves in the end but all suggestions much appreciated.
Hi Louise,
SAS can do a lot, and most of it is done very well indeed. But in this scenario SAS may not be the best tool. I would consider a script of some sort on the operating system (we would like to know which one anyways) as the way to go. That script could in turn be executed from SAS if you like but SAS would add very little benefit. And if copying is done by SAS then the unzipping would be a real challenge.
Cheers,
- Jan.
Look at this:
cd sourcelocation DATE=$1 mkdir targetlocation/${DATE} for FILE in *${DATE}*.zip;do cp $FILE targetlocation/${DATE} unzip targetlocation/${DATE}/${FILE} done
This is a sample UNIX shell script that does what you want. Doing the same in SAS would be much more complicated.
Use the right tool for the job.
Thanks for the responses guys but I really need to do it in SAS as the final program needs to put into an automated production for a monthly report. I should be able to use the wzinzip and copy functions but it's just identifying the filenames that contain the month of May, or whatever it would be at the time of the scheduled run.
Thanks
Louise
So which system is it? UNIX or WIndows?
The system we use is Windows
Ok, let's try
Initialize:
%let sourcepath=where_your_files_are;
%let targetpath=where_you_want_them;
%let month=MAY2016;
Create a dataset with file names:
filename oscmd pipe "dir &sourcepath.\*&month.*.zip";
data filenames;
length fname $254;
infile oscmd;
input fname;
run;
Now act on that
data _null_;
set filenames;
if _n_ = 1 then call system("mkdir &targetpath.\&month");
call system("copy " !! fname !! " &targetpath.\&month");
fname = scan(fname,-1,'\');
call system("winzip32 -e &targetpath.\&month.\" !! fname);
run;
Hi All,
Thanks to everyone that posted a suggestion - we have managed to solve the issue ourselves in the end but all suggestions much appreciated.
Out of cusiosity and always looking for a learning opportunity, could you share your solution with us?
- Jan.
As requested:
/*set file locations from and to*/
%let statement_fm = &wr\XXXX\XXXX;
%let statement_to = &wr\XXXX\XXXX\&MONTH;
Data List_files (keep = filevar);
rc=FILENAME('stmnts',"&file_fm");
did=DOPEN('stmnts');
memcnt=DNUM(did);
*count number of members - including subfolders;
DO i=1 TO memcnt;
*for each member, test extension and store filename;
filevar=(DREAD(did,i));
IF LOWCASE(SCAN(filevar,-1,".")) EQ LOWCASE("zip") THEN
OUTPUT;
END;
rc=DCLOSE(did);
rc=FILENAME('stmnts');
Run;
proc sql;
select
filevar, count(*)
into: file1-,: obs
from work.list_files(where=(lowcase(filevar) like "%%&monthname.%"));
quit;
%macro copy_files;
%do i=1 %to &obs.;
%put &&file&i.;
%sysexec move "&file_fm\&&file&i." "&file_to\&&file&i.";
%end;
%mend;
%copy_files;
%macro unzip_files;
%do i=1 %to &obs.;
%put &&file&i.;
%sysexec wzunzip "&file_to\&&file&i." "&file_to\" -d;
%end;
%mend;
%unzip_files;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.