UCLA Academic Technology Services HomeServicesClassesContactJobs

SAS Code Fragments
How can I generate automated filenames in SAS?

If you are creating multiple datasets in SAS, you may wish to name them in an automated manner.  The code below demonstrates how to create a filename that is based on the date and timestamp.  Such a file naming process will 1) prevent you from giving the same filename to two different datasets and 2) allow you to see when files were first created. 

The code below presents an example. We first generate a dataset to be saved, tsave. Then, we use another data step to create the string that we will use to name the files and save it as a macro variable "mytime".  Then, in the last data step, we refer to the macro variable "mytime" in naming the saved dataset.   


data tsave;
  do i = 1 to 10;
    do time = 1 to 5;
	   y = rannor(1232+i + time);
       output;
  	end;
  end;
run;
data _null_;
  cdate =  "&SYSDATE9";
  ctime = "&SYSTIME";
  time_string = cdate||"_"||translate(ctime, "_", ":");
  call symput('mytime', time_string);
run;
data "c:\temp\newdata_&mytime";
  set tsave;
run;

How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.