DATA Step, Macro, Functions and more

Set dataset based on the date in file name

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Set dataset based on the date in file name

Hi

 

I would like to automatically set the dataset with the newest date.

 

My problem is the following:

 

I have a folder / directory which contains datasets with a date in the file name. The date indicates when the dataset is generated. It could for instance be called a_08AUG16 or a_09AUG16. I want to be able to set the latest dataset automatically based on the date in the file name. Is that possible?

 

Best regards

 

Aske


Accepted Solutions
Solution
‎08-09-2016 08:18 AM
Super User
Super User
Posts: 7,942

Re: Set dataset based on the date in file name

Let me start by saying its never a good idea to put "data" into the structural part of the dataset - either table details or column details, it makes programming so much harder, and runs up problems like this.  Put data in the rows of the dataset.

 

Now you can use the metadata files from sashelp.vtable, basically that has the structural parts as data elements.  

data _20JAN2016;
  set sashelp.class;
run;
data _14FEB2016;
  set sashelp.cars;
run;

proc sql noprint;
  select  put(max(DTE),date9.)
  into    :DTE
  from    (select *,input(strip(compress(MEMNAME,"_","")),date9.) as DTE from SASHELP.VTABLE where libname="WORK" and substr(MEMNAME,1,1)="_");
quit;

data want;
  set work._&DTE.;
run;

 

View solution in original post


All Replies
Solution
‎08-09-2016 08:18 AM
Super User
Super User
Posts: 7,942

Re: Set dataset based on the date in file name

Let me start by saying its never a good idea to put "data" into the structural part of the dataset - either table details or column details, it makes programming so much harder, and runs up problems like this.  Put data in the rows of the dataset.

 

Now you can use the metadata files from sashelp.vtable, basically that has the structural parts as data elements.  

data _20JAN2016;
  set sashelp.class;
run;
data _14FEB2016;
  set sashelp.cars;
run;

proc sql noprint;
  select  put(max(DTE),date9.)
  into    :DTE
  from    (select *,input(strip(compress(MEMNAME,"_","")),date9.) as DTE from SASHELP.VTABLE where libname="WORK" and substr(MEMNAME,1,1)="_");
quit;

data want;
  set work._&DTE.;
run;

 

Super User
Posts: 7,764

Re: Set dataset based on the date in file name

The best solution, hands down, is to use a date format in the dataset names that sorts itself, ie YYYY-MM-DD.

With the current situation, you will have to read the dataset names from sashelp.vtable (or dictionary.tables), convert the date in the dataset name to a SAS date, and retrieve the wanted observation with max(date).

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
New Contributor
Posts: 2

Re: Set dataset based on the date in file name

Thanks for the help!

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 262 views
  • 1 like
  • 3 in conversation