Help using Base SAS procedures

Appending yearly data

Reply
Super Contributor
Posts: 535

Appending yearly data

 

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.  

Super User
Posts: 12,148

Re: Appending yearly data

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.

Super Contributor
Posts: 535

Re: Appending yearly data

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.
Super User
Posts: 12,148

Re: Appending yearly data


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?

Highlighted
Super Contributor
Posts: 535

Re: Appending yearly data

Start year is fixed and it is 2016. End year to be determined by the
current date.
Contributor
Posts: 24

Re: Appending yearly data

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

Super Contributor
Posts: 535

Re: Appending yearly data

Posted in reply to iscgonzalez
Is there a way to dynamically define the end year value instead of
hardcoding it as 2018?
Contributor
Posts: 24

Re: Appending yearly data

you can use a macrovariable 

 

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

Trusted Advisor
Posts: 1,155

Re: Appending yearly data

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

Ask a Question
Discussion stats
  • 8 replies
  • 213 views
  • 1 like
  • 4 in conversation