BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SM3
Calcite | Level 5 SM3
Calcite | Level 5
Hi,

I have a report date which is end of every qtr like 31Dec2015 and Using a variable created from report date ,I have 9 datasets like data_2015Q4, data_2015Q3.....data_2013Q3.Finally I want to append these 9 datasets to create a dataset like inp_2013q3_2015q4.but the issue is this should be done dynamically.For ex, in next qtr i,e 31mar2016, I need to append datasets going back 9 quarters i,e from 2014q1 to 2016q1. How do I do this?
1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Get the dataset names from dictionary.tables and filter according to your requirements:

 

data data_2015Q4; x=1; run;
data data_2015Q3; x=2; run;
data data_2015Q2; x=3; run;
data data_2015Q1; x=4; run;
data data_2014Q4; x=5; run;
data data_2014Q3; x=6; run;
data data_2014Q2; x=7; run;
data data_2014Q1; x=8; run;
data data_2013Q4; x=9; run;
data data_2013Q3; x=10; run;
data data_2013Q2; x=11; run;
data data_2013Q1; x=12; run;

%let rptDate=31Dec2015;

proc sql noprint;
create table rptList as
select memname
from dictionary.tables
where libname="WORK" and
    memname like "DATA_____Q_" and
    intnx("QTR",input(scan(memname,2,"_"), ? yyq6.),0,"END") between
    intnx("QTR", "&rptDate"d, -8, "END") and
    "&rptDate"d;
select memname 
into:setList separated by " "
from rptList order by memname desc;
select catx("_", "inp", min(scan(memname,2,"_")), max(scan(memname,2,"_")))
into:setName trimmed
from rptList; 
drop table rptList, &setName;
quit;

data &setName;
set &setList;
run;

proc print; run;
PG

View solution in original post

7 REPLIES 7
Reeza
Super User

I would change your process so that it appends the dataset to the 'master' dataset at the end of the process.

 

Alternatives are to store all the output with a consistent naming structure in a library. 

 

If you use a prefix that isn't used with any other datsets you can refer to it with a colon to append all datasets with the same prefix. 

Ie this would append all datasets starting with IMP.

 

data want;
set imp: ;
run;
Ksharp
Super User





data have;
input date date9.;
cards;
31Dec2015 
31mar2016
;
run;
data temp;
 set have;
 length list $ 400;
 do i=-1 to -9 by -1;
  list=catx(' ',list,'data_'||put(intnx('qtr',date,i),yyq.));
 end;
 data=catx('_','inp',put(intnx('qtr',date,-9),yyq.),put(date,yyq.));
 drop i;
run;

data _null_;
 set temp;
 call execute(catx(' ','data',data,';set',list,';run;'));
run;



SM3
Calcite | Level 5 SM3
Calcite | Level 5
Thank u so much Ksharp & Reeza for ur reply. But the report date is an user input every quarter like % let enddate="31dec2015".Using these date, sas has to go 9 qtr back dynamically and append datasets in a library with a fixed naming structure like data_2015Q1,data_2014q4....Tge library might have more than 9 qtrs of data but it sud append till 9 qtrs back starting from enddate.
Appreciate ur help.
Ksharp
Super User
Still the same thing :




%let date=31dec2015;


data temp;
 length list $ 400;
 do i=-1 to -9 by -1;
  list=catx(' ',list,'data_'||put(intnx('qtr',"&date"d,i),yyq.));
 end;
 data=catx('_','inp',put(intnx('qtr',"&date"d,-9),yyq.),put("&date"d,yyq.));
 drop i;
run;

data _null_;
 set temp;
 call execute(catx(' ','data',data,';set',list,';run;'));
run;

Ksharp
Super User
Still the same thing :


%let date=31dec2015;
data temp;
 length list $ 400;
 do i=-1 to -9 by -1;
  list=catx(' ',list,'data_'||put(intnx('qtr',"&date"d,i),yyq.));
 end;
 data=catx('_','inp',put(intnx('qtr',"&date"d,-9),yyq.),put("&date"d,yyq.));
 drop i;
run;

data _null_;
 set temp;
 call execute(catx(' ','data',data,';set',list,';run;'));
run;

PGStats
Opal | Level 21

Get the dataset names from dictionary.tables and filter according to your requirements:

 

data data_2015Q4; x=1; run;
data data_2015Q3; x=2; run;
data data_2015Q2; x=3; run;
data data_2015Q1; x=4; run;
data data_2014Q4; x=5; run;
data data_2014Q3; x=6; run;
data data_2014Q2; x=7; run;
data data_2014Q1; x=8; run;
data data_2013Q4; x=9; run;
data data_2013Q3; x=10; run;
data data_2013Q2; x=11; run;
data data_2013Q1; x=12; run;

%let rptDate=31Dec2015;

proc sql noprint;
create table rptList as
select memname
from dictionary.tables
where libname="WORK" and
    memname like "DATA_____Q_" and
    intnx("QTR",input(scan(memname,2,"_"), ? yyq6.),0,"END") between
    intnx("QTR", "&rptDate"d, -8, "END") and
    "&rptDate"d;
select memname 
into:setList separated by " "
from rptList order by memname desc;
select catx("_", "inp", min(scan(memname,2,"_")), max(scan(memname,2,"_")))
into:setName trimmed
from rptList; 
drop table rptList, &setName;
quit;

data &setName;
set &setList;
run;

proc print; run;
PG
SM3
Calcite | Level 5 SM3
Calcite | Level 5
This is awesome. Thanks for ur help😊

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.

Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 7 replies
  • 1693 views
  • 1 like
  • 4 in conversation