Hi,
I've many years of data I need to combine using SQL specifically. The data names are not in numerical order (e.g. 2011, 2012, 2013.. 2016.). Instead its in the year format of 1112, 1213, 1314, ....1516, 1617. My first year of data in the example is 2013 and last year of data is 2016.
Below is the 4 datsets and a code that I'd spell out for each year. The do loop marcro I wrote didn't work. Can you point the errors for me? Much appreciated.
data havedata1314Q4; input id date; datalines; 1 2005 2 2006 ; data havedata1415Q4; input id date; datalines; 1 2005 4 2010 ;data havedata1516Q4; input id date; datalines; 2 2006 6 2012 ;data havedata1617Q4; input id date; datalines; 7 2013 8 2015 ;
*Code 1 - spelling out steps; %let lastyr2=16; *16 as latest year of data - 2016; %let lastyr3= %sysfunc(putn(&lastyr2,z2.))%sysfunc(putn(%eval(&lastyr2+1),z2.)); *eg yr=1617; proc sql; create table combined as select distinct ID, Date from HaveData1314Q4 union select distinct ID, Date from HaveData1415Q4 union select distinct ID, Date from HaveData1516Q4 union select distinct ID, Date from HaveData&lastyr3.Q4; create table want as select distinct ID, date from combined; quit;
*Code 2 using do loop; %let lastyr2=16; *16 as latest year of data - 2016; %let lastyr3= %sysfunc(putn(&lastyr2,z2.))%sysfunc(putn(%eval(&lastyr2+1),z2.)); *eg yr=1617; %let prevyr=20%sysfunc(putn(%eval(&lastyr2-1),z2.)); *get prevoius year 2015; %macro get; proc sql; create table out1 as %do year=2013 %to &prevyr; %let year1= %sysfunc(putn(&year.,z2.))%sysfunc(putn(%eval(&year.+1),z2.)); select distinct ID, date from HaveData&year1.Q4 union %end; select distinct ID, date from HaveData&lastyr3.Q4 create table want as select distinct ID, date from combined; quit; %mend get; %get;
... View more