BookmarkSubscribeRSS Feed
Babloo
Rhodochrosite | Level 12

 

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.  

8 REPLIES 8
ballardw
Super User

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
Rhodochrosite | Level 12
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.
ballardw
Super User

@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?

Babloo
Rhodochrosite | Level 12
Start year is fixed and it is 2016. End year to be determined by the
current date.
iscgonzalez
Obsidian | Level 7

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);

Babloo
Rhodochrosite | Level 12
Is there a way to dynamically define the end year value instead of
hardcoding it as 2018?
iscgonzalez
Obsidian | Level 7

you can use a macrovariable 

 

%let sysyear= %sysfunc(year("&sysdate"d));

mkeintz
PROC Star

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_.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1557 views
  • 1 like
  • 4 in conversation