Help using Base SAS procedures

how to copy files from sub directory..

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

how to copy files from sub directory..

Hello friends,

Recently I'm working on a task where I need to take backup of all files which are generated as output from the main program to a new folder like Backup_2015 etc.

my folder structure is like below -

E:\TEST DATA\2015

folder 2015 contains following files in it -
1. adomsg.sas7bdat
2. ADOMSG.XPT
3. Report_2015.rtf
4. DOMAIN_XPT.LOG and
5. QC folder which again contains 3 files -
a. notes.txt
b. QC_report.xlsx
c. Report.pdf

now my task is to take a backup these files by making a new folder in 2015 folder naming like Backup_2015(current year) and copy the contents of 2015 folder to it.

I have achieved the half milestone by below program however I'm not able to create "QC" folder and copy it's contents to newly created folder i.e. Backup_2015.

 

/* SET THE OPTIONS */
OPTIONS MLOGIC MPRINT SYMBOLGEN;


%macro copyit(in=,out=);
filename in "&in";
filename out "&out";

/* copy the file byte-for-byte  */
data _null_;
  length filein 8 fileid 8;
  filein = fopen('in','I',1,'B');
  fileid = fopen('out','O',1,'B');
  rec = '20'x;
  do while(fread(filein)=0);
     rc = fget(filein,rec,1);
     rc = fput(fileid, rec);
     rc =fwrite(fileid);
  end;
  rc = fclose(filein);
  rc = fclose(fileid);
run;
 
filename in clear;
filename out clear;
%mend copyit;
/* Path for the source folder */
%let infolder=E:\TEST DATA\2015;
%let prot=backup; 
data dnam; 
rc=filename("mydir","&infolder"); /* create fileref */ 
did =dopen("mydir"); /* open folder */ 
f_name1=catx("_","&prot",(put(year(today()),best.)));
outfolder=dcreate(f_name1,"&infolder");/* create backup_<year> folder */
if did > 0 then
do i=1 to dnum(did);
file_name= dread(did,i);
infolder=catx("\","&infolder",dread(did,i)); /* read item name */
outfolder=catx("\","&infolder",f_name1,file_name) ;
output;
end;
rc=dclose(did); /* close folder */  
run;

/* create macro variable for input and out folder */
data _null_(drop=rc did f_name1 i);
	set dnam
	(where=(upcase(compress(f_name1))^=upcase(compress(file_name)))) end=eof;
	cnt+1;
	call symputx('infile'||LEFT(PUT(_N_,8.)),infolder);
	call symputx('outfile'||LEFT(PUT(_N_,8.)),outfolder);
	IF EOF THEN CALL SYMPUTX('CNT',PUT(_N_,8.));
run;

/* copy file one by one */
%macro doit;
	%do i=1 %to &cnt;
	%copyit(in=&&infile&i,out=&&outfile&i);
	%end;
%mend doit;

%doit

Please help me to get this rigth.

 

Thanks in Advance!

- Nikhil


Accepted Solutions
Solution
‎11-27-2015 05:30 AM
Regular Contributor
Posts: 161

Re: how to copy files from sub directory..

As mentioned yesterday for the script, this code will work if you have and run the sas program in the same directory as the folder named 2015. If not, you need to code the entire path  in the %str() instead of referring to the current directory using a "."

 

Hope this makes sense.  You may also need to refer to Unix help online just to get an idea of the Unix commands and the parameters used. This may help getting started... 

 

https://kb.iu.edu/d/afsk

 

There are plenty of online resources that can help learning Unix commands.

 

Good Luck...!!!

Kannan Deivasigamani

View solution in original post


All Replies
Regular Contributor
Posts: 161

Re: how to copy files from sub directory..

Is the directory structure you have mapped as "E-drive"  a NAS mount with an underlying Unix environment or is this a pure windows environment?  If it is Unix, a simple shell script could accomplish the task without a need for a lengthy and complex coding. 

Kannan Deivasigamani
New Contributor
Posts: 3

Re: how to copy files from sub directory..

Hi Kannad,
thanks for you quick response. I'm testing these codes on Windows only however I need to release final SAS program to work on Unix environment. I will be very grateful if you explain me how to achive it with simple shell scripting.
Thanks.
- Nikhil
Regular Contributor
Posts: 161

Re: how to copy files from sub directory..

I'm not an expert on Unix but this should work... The following would be the contents of your .sh file

 

echo "starting backup process"
mkdir ./2015bk
mv ./2015/* ./2015bk
echo "Backup complete. The files have been copied. Please verify !"

Note: This just creates a directory called 2015bk and copies the originals.  The original copies remain in the source as well.  If you need the original files or folders removed, you need to modify this to include a line performing the "rm" command to clear the files.

 

Hope this helps... Good Luck...!!!

 

Kannan Deivasigamani
Regular Contributor
Posts: 161

Re: how to copy files from sub directory..

forgot to mention....The script assumes that the folder 2015 is available in the directory where the script is located and running... It will create a 2015bk folder in the same location where you have 2015 folder. If you need to run the copy from a different location, you need to specify the entire path in place of the "."

Kannan Deivasigamani
New Contributor
Posts: 3

Re: how to copy files from sub directory..

Hey Kannad,
Your help is really appreciated, but I want to ask that can we added these codes in SAS program instead running it in Unix script or run manually?

I want SAS to do this work for me as we are running single SAS program like
cd dmenv/dmwork/abc/ sas myprogram.sas

Also I'm not much familiar with UNIX and scripting etc.

I hope you understand it Smiley Happy.

Thanks again!
- Nikhil
Regular Contributor
Posts: 161

Re: how to copy files from sub directory..

Nikhil - refer to the URL below, you'll get a better idea as it gives some theoritical background on how to do the same...

 

http://support.sas.com/documentation/cdl/en/hostunx/61879/HTML/default/viewer.htm#xcomm.htm

 

Focus on this example in  the document specifically that calls the macro pwdls: You can clone this to your requirement with the commands I gave you in the sh script earlier...

 

%macro pwdls;
%sysexec %str(pwd;ls -l);
%mend pwdls;
%pwdls;

Replace this line 

%sysexec %str(pwd;ls -l);

to  show something like 

%sysexec %str(mkdir ./2015bk;mv ./2015/* ./2015bk);

Hope this helps... Good Luck...!!! 

Kannan Deivasigamani
Solution
‎11-27-2015 05:30 AM
Regular Contributor
Posts: 161

Re: how to copy files from sub directory..

As mentioned yesterday for the script, this code will work if you have and run the sas program in the same directory as the folder named 2015. If not, you need to code the entire path  in the %str() instead of referring to the current directory using a "."

 

Hope this makes sense.  You may also need to refer to Unix help online just to get an idea of the Unix commands and the parameters used. This may help getting started... 

 

https://kb.iu.edu/d/afsk

 

There are plenty of online resources that can help learning Unix commands.

 

Good Luck...!!!

Kannan Deivasigamani
Super User
Super User
Posts: 7,405

Re: how to copy files from sub directory..

Alternatively, use version control software - which is what its created for.  After each run commit the folder to the repository and it is there for good.  Why go through all the effort of creating code to create multiple back up folders (which may or may not work depending on how robust your code is), then have to faff about with lots of folders/files including duplicates etc. when there is pre-built software to do it for you?  At worst case scenario your IT group should have a backup setup already.

☑ This topic is SOLVED.

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

Discussion stats
  • 8 replies
  • 599 views
  • 3 likes
  • 3 in conversation