Hello
Let's say there there are many data sets called "tblDDMMYY".
Let's say that I want to set all data sets between 2 dates (31JAN2021 and 14MAR2021).
I have some requests:
1- In data set "Data_sets_list" there will be a column with potential data sets that I want to combine (SET).
2- In the list of potential data sets ,there are some data sets that are not existing so I need to write a code that combine the data sets without getting an error.
3-In wanted data set I want to add a column called "source" with date of source data set
data tbl14032021;
input x y;
cards;
1 10
;
run;
data tbl17032021;
input x y;
cards;
2 11
;
run;
data tbl28032021;
input x y;
cards;
5 18
;
run;
data tbl17022021;
input x y;
cards;
6 16
;
run;
%let start_date=31JAN2021;
%let end_date=14MAR2021;
data Data_sets_list (where=(date>"&start_date"d));
date="&start_date"d;
do while (date<="&end_date"d);
output;
date=intnx('day', date, 1);
date_ddmmyyyy=put(date,ddmmyyn8.);
tbl_name=CATS("tbl",date_ddmmyyyy);
end;
format date date9.;
run;
Data wanted;
set data_Sets_in_column_tbl_name_in_dataSet_Data_sets_list;
source=date_from_name_of_source_data_set;
Run;
You really need to consider library names when ever building lists of data sets. Always.
You never know when your data set names might match something in the SASHELP library.
Here is one way that uses the supplied set names and parses them for dates instead of building a list of names that do not exist.
%let start_date=31JAN2021; %let end_date=14MAR2021; data temp; set sashelp.vtable (keep=libname memname); where libname='WORK' and upcase(substr(memname,1,3)) = 'TBL'; namedate= input(substr(memname,4),ddmmyy10.) ; length filename $ 41; filename = catx('.',libname,memname); if "&start_date."d le namedate le "&end_date."d; run; proc sql noprint; select strip(filename) into : setlist separated by ' ' from temp ; quit; data want; set &setlist. indsname=inset; source=inset; run;
The SASHELP.VTABLE, or Dictionary.tables if using SQL, has the names of all of the datasets currently known to your SAS session. This parses the names of the ones in the WORK library that might match your names. If you have things like TBLJUNK the above will throw invalid data warnings because the JUNK part cannot be turned into a date but the resulting data set should be appropriate anyway.
The INDSNAME option is how you get the name of the data set that contributes a specific record. However that creates a temporary variable. So another variable has to be assigned the value.
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 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: