BookmarkSubscribeRSS Feed
iSAS
Quartz | Level 8

I need to move a sas data set from /workspace/data to /workspace/data/archive when it hasn't been used for more than 2 weeks. I need to run the program daily. May I know how to code this?

3 REPLIES 3
tomrvincent
Rhodochrosite | Level 12
Cycle thru each of the datasets and check the modify date. Move it if it's old enough.
ballardw
Super User

Define "used". Date Created, modified or read are different ways of defining "used".

 

You can easily get the first two from SAS with code such as:

proc sql;
   create table work.junk as
   select memname, libname, crdate, modate
   from dictionary.tables
   where libname='WORK'
   ;
quit;

You can use DATEPART on either of CRDATE or MODATE (both are datetime values) and compare to the TODAY() function value to create code to copy the data sets to a different library.

 

I don't think SAS keeps a "last read" anywhere which would lead to using the external file functions such as DOPEN, DNUM, DREAD, FOPEN, FOPTNUM, FOPTNAME and FINFO.

Jagadishkatam
Amethyst | Level 16

Please try the below code, check the comments for code understanding, i tested it and it worked

 

But you need to run this everyday manually to move the files, please test and let me know if it helps.

 


/*use the x command in sas */
x 'ls -l --time-style="+%d/%m/%Y" ~path_of_files*.txt > ~path_of_sample_file_with_list_of_files/sample.txt';

/*get the files that need to be moved to other location*/
proc import datafile='~path_of_sample_file_with_list_of_files/sample.txt' out=test dbms=tab replace;
getnames=no;
run;

data test2;
length files filenames transfer $200.;
set test;
filedate=input(substr(var1,36,10),ddmmyy10.); /*get the file dates*/
filenames=scan(substr(var1,46),8,'/');/*get the filename*/
files=substr(var1,46);/*get the file path*/
today=input("&sysdate9.",date9.);/*todays date*/
format filedate today date9.;
if (today-filedate)>14 then flag=1;/*flag those records where the files are more than 2 weeks old i.e. more than 14 days */
transfer='mv '||strip(files)||" ~new_path_of_files_/"||strip(filenames); /*create a variable with mv i.e., move command to move the flagged files from present location to a differnt location*/
if flag=1 then do;
call execute('x '||strip(transfer)||';');/*call execute to use the x command to move the files*/
end;
run;

 

 

 

 

 

 

 

Thanks,
Jag

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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