I've the following datasets in SAS and for the reporting I use only the previous year dataset (test_2017). Now I've to create a report which has data from 2016 to current year. It means I've to append the datasets (test_2016,test_2017 & test_2018) to create master dataset (test_2016_to_curr_year) which I will use for my reporting.
test_2018
test_2017
test_2016
test_2015
test_2014
Appreciate if someone guide me to write the code dynamically to achieve my requirement.
You don't say how you currently combine the data but this should work:
Data want;
set test_2016 - test_2018;
run;
If there is a permanent library associated then don't forget to use the library.
The "dynamic" would have to be based on something to identify the start and end set but you don't really gain much if this is the only place the "dynamic" element would be used.
@Babloo wrote:
I just want to append the data and also I don't want to hard code the
current year value(i.e. 2018) and the I want the code to find it
dynamically.
So how do you determine the current year? Todays date or something else. Note this is not a snarky question because depending on who you work with "year" could be a fiscal year, School year which for some places a school year of "2017" means August 2016 to June 2017, or other "year" type (are you familiar with a "water year") which does not coincide with a calendar year.
If your data "year" names do not coincide with calendars then an explicit description would be needed to map a current date available from the Today function.
The second part would be how to determine the "start year". Is it fixed or do you want something that allows you to very going back 2, 3, 4 or more years?
I'm thinking on a macro where you can set initial and end years
data test_2016;
input var;
datalines;
6
6
6
;
run;
data test_2017;
input var;
datalines;
7
7
7
7
;
run;
data test_2018;
input var;
datalines;
8
8
8
8
;
run;
%macro appendtab(start,end);
%do i=&start %to &end;
proc append base=master data=test_&i.;
run;
%end;
%mend;
%appendtab(2016,2018);
you can use a macrovariable
%let sysyear= %sysfunc(year("&sysdate"d));
If all your datasets have a common name root, appended with a 4-digit year (as in test_2015 test_2016 ....), you can read them all without knowing the actual year ranges, with:
data want;
set mylib.test_: ;
run;
which will read all datasets whose names begin with TEST_.
This assumes there are no unwanted datasets in MYLIB whose name begins with TEST_.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.