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

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

1 ACCEPTED SOLUTION

Accepted Solutions
LouiseH2010
Fluorite | Level 6

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.

View solution in original post

9 REPLIES 9
jklaverstijn
Rhodochrosite | Level 12

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.

Kurt_Bremser
Super User

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.

LouiseH2010
Fluorite | Level 6

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

Kurt_Bremser
Super User

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;
LouiseH2010
Fluorite | Level 6

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.

jklaverstijn
Rhodochrosite | Level 12

 Out of cusiosity and always looking for a learning opportunity, could you share your solution with us?

 

- Jan.

LouiseH2010
Fluorite | Level 6

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

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
  • 9 replies
  • 1011 views
  • 0 likes
  • 3 in conversation