Why do you have data in the dataset name? Now this isn't necessarily you, it seems that quite a few people insist on putting date information in dataset names, but it is the direct cause of making you have this type of problem. Use one large dataset with a variable for date, this then trivialises the problem as code can be written for the one structure/name, and subsetting is simply where clausing.
As for coding:
data xyz_201406; a=1; run; data xyz_201405; a=2; run; data tmp; set sashelp.vtable (where=(libname="WORK" and substr(memname,1,3)="XYZ")); dt=scan(memname,2,"_"); run; proc sort data=tmp; by dt; run;
This shows how you can use the sashelp tables to identify certain datasets, however it is still patching the actual problem of file setup.
First yo need a way to get a specific (the prior month initially).
This is accomplished with the INTNX function, which you could call from a data step, or via %sysfunc in open code/macro.
The you need to verify that the specific month data set exists.
Here you got a lot of options, like DICTIONARY.TABLES in SQ, SASHELP.VSTABLE from other SAS programs, data step SAS file functions.
If the specific month doesn't exist, you need to adjust the month with INTNX again. Depending on the how dynamic you wish to this, us %IF %THEN %ELSE logic, or some kind of loop logic.
This would get you started, get back when you specific questions a long the way.
I did give you some code to try. Have you tried it?
No need Macro. data data_201608; set sashelp.class; run; data data_201609; set sashelp.class; run; proc sql; select memname into : want from dictionary.members having libname='WORK' and memtype='DATA' and input(scan(memname,-1,'_'),best.)= max(input(scan(memname,-1,'_'),best.)); quit; %put &want ;
Why do you have data in the dataset name? Now this isn't necessarily you, it seems that quite a few people insist on putting date information in dataset names, but it is the direct cause of making you have this type of problem. Use one large dataset with a variable for date, this then trivialises the problem as code can be written for the one structure/name, and subsetting is simply where clausing.
As for coding:
data xyz_201406; a=1; run; data xyz_201405; a=2; run; data tmp; set sashelp.vtable (where=(libname="WORK" and substr(memname,1,3)="XYZ")); dt=scan(memname,2,"_"); run; proc sort data=tmp; by dt; run;
This shows how you can use the sashelp tables to identify certain datasets, however it is still patching the actual problem of file setup.
%let Run_Date = %sysfunc(today());
%let Last_Month = %sysfunc(intnx(MONTH, &Run_Date, -1), yymmn6.);
data want;
set have_&Last_Month;
run;
By adding the two macro statements to the top of your program, the last month macro variable suffix can be used on any dataset that follows.
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.