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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.