Hi , please will someone be able to assist me - i have the following code (Code will follow) . i need this code to read 12 months of data the data sets are as follow: how will i create a Macro to read the code with all these data sets.
werkwm.martaccount202210
werkwm.martaccount202211
werkwm.martaccount202212
werkwm.martaccount202301
werkwm.martaccount202302
werkwm.martaccount202303
werkwm.martaccount202304
werkwm.martaccount202305
werkwm.martaccount202306
werkwm.martaccount202307
werkwm.martaccount202308
werkwm.martaccount- these data sets are for the prev months as its not labled- but will update at month end with 202309
Werkwm.martvtraxapplications202210
Werkwm.martvtraxapplications202211
Werkwm.martvtraxapplications202212
Werkwm.martvtraxapplications202301
Werkwm.martvtraxapplications202302
Werkwm.martvtraxapplications202303
Werkwm.martvtraxapplications202304
Werkwm.martvtraxapplications202305
Werkwm.martvtraxapplications202306
Werkwm.martvtraxapplications202307
Werkwm.martvtraxapplications202308
Werkwm.martvtraxapplications-these data sets are for the prev months as its not labled- but will update at month end with 202309
WerkWM.MARTVTRAXDEALERS202210
WerkWM.MARTVTRAXDEALERS202211
WerkWM.MARTVTRAXDEALERS202212
WerkWM.MARTVTRAXDEALERS202301
WerkWM.MARTVTRAXDEALERS202302
WerkWM.MARTVTRAXDEALERS202303
WerkWM.MARTVTRAXDEALERS202304
WerkWM.MARTVTRAXDEALERS202305
WerkWM.MARTVTRAXDEALERS202306
WerkWM.MARTVTRAXDEALERS202307
WerkWM.MARTVTRAXDEALERS202308
WerkWM.MARTVTRAXDEALERS- these data sets are for the prev months as its not labled- but will update at month end with 202309
The code:
It's a bit hard to know your exact requirement, but if you need a list of datasets for a 12 month period, here's some (untested) code you can start with. If you want to fully automate you can use &sysdate in the beginning to specify your start period. Since I didn't know how you'll select the 12 month period I didn't include that in my logic.
%let start=%sysfunc(intnx(month,%sysfunc(inputn(&end.01,yymmdd8.)),-12), yymmddn8.);
%put &start;
proc sql;
select catx('.',libname,memname) into: martaccounts separated by ' '
from dictionary.tables
where memname between "MARTACCOUNT&start" and "MARTACCOUNT&end"
and libname = 'WERKWM';
select catx('.',libname,memname) into: martvtraxapplications separated by ' '
from dictionary.tables
where memname between "MARTVTRAXAPPLICATIONS&start" and "MARTVTRAXAPPLICATIONS&end"
and libname = 'WERKWM';
select catx('.',libname,memname) into: martvtraxdealers separated by ' '
from dictionary.tables
where memname between "MARTVTRAXDEALERS&start" and "MARTVTRAXDEALERS&end"
and libname = 'WERKWM';
quit;
Do you really need a macro to read in that datasets? Why not to use "data sets list", see example:
libname werkwm (work);
data
werkwm.martaccount202210
werkwm.martaccount202211
werkwm.martaccount202212
werkwm.martaccount202301
werkwm.martaccount202302
werkwm.martaccount202303
werkwm.martaccount202304
werkwm.martaccount202305
werkwm.martaccount202306
werkwm.martaccount202307
werkwm.martaccount202308
werkwm.martaccount
;
set sashelp.class;
run;
options noDSNFERR; /* to use data sets list (e.g., SET A1-A111;)
and don't get error if any from the list is missing */
data ALL;
set
werkwm.martaccount202210-werkwm.martaccount202308 werkwm.martaccount
;
run;
options DSNFERR;
[EDIT:]
The SET statement with a "data sets list" of the form A1-A5 usually expects that all datasets A1, A2, A3, A4, and A5 exist. In you case the range
werkwm.martaccount202210-werkwm.martaccount202308
for sure will have some "gaps" (we don expect to have 202214,202215,etc.) that's why the noDSNFERR
option solves the problem, basicall it says to SAS: "if a data set from the range does not exist ignore that and go to the next one"
Bart
Then how about something like this:
libname werkwm (work);
data
werkwm.martaccount202210
werkwm.martaccount202211
werkwm.martaccount202212
werkwm.martaccount202301
werkwm.martaccount202302
werkwm.martaccount202303
werkwm.martaccount202304
werkwm.martaccount202305
werkwm.martaccount202306
werkwm.martaccount202307
werkwm.martaccount202308
werkwm.martaccount
;
set sashelp.class;
run;
%macro ListFiles(lib,ds,date);
%local start end;
%let end = %sysfunc(intnx(Month,"&date."d,-1 ,S),yymmn6.);
%let start= %sysfunc(intnx(Month,"&date."d,-12,S),yymmn6.);
&lib..&ds.&start. - &lib..&ds.&end. &lib..&ds.
%mend;
options noDSNFERR; /* to use data sets list (e.g., SET A1-A111;)
and don't get error if any from the list is missing */
data ALL;
set
%ListFiles(werkwm,martaccount,1sep2023);
;
run;
options DSNFERR;
The code still uses "noDSNFERR" option.
The "date" parameter should be set for "month with no date in dataset name", in this case September 2023.
Add "options Mprint;" to see how it is resolved under the hood.
Bart
It's a bit hard to know your exact requirement, but if you need a list of datasets for a 12 month period, here's some (untested) code you can start with. If you want to fully automate you can use &sysdate in the beginning to specify your start period. Since I didn't know how you'll select the 12 month period I didn't include that in my logic.
%let start=%sysfunc(intnx(month,%sysfunc(inputn(&end.01,yymmdd8.)),-12), yymmddn8.);
%put &start;
proc sql;
select catx('.',libname,memname) into: martaccounts separated by ' '
from dictionary.tables
where memname between "MARTACCOUNT&start" and "MARTACCOUNT&end"
and libname = 'WERKWM';
select catx('.',libname,memname) into: martvtraxapplications separated by ' '
from dictionary.tables
where memname between "MARTVTRAXAPPLICATIONS&start" and "MARTVTRAXAPPLICATIONS&end"
and libname = 'WERKWM';
select catx('.',libname,memname) into: martvtraxdealers separated by ' '
from dictionary.tables
where memname between "MARTVTRAXDEALERS&start" and "MARTVTRAXDEALERS&end"
and libname = 'WERKWM';
quit;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.