mafalda
mafalda

Reputation: 31

SAS Append datasets only if they exist

I have many datasets for each month with the same name, changing just the end with specific month so for instance my datasets that i am calling with this code:

TEMPCAAD.LIFT_&NOME_MODELO._&VERSAO_MODELO._'!! put(cur_month,yymmn6.));

are called "TEMPCAAD.LIFT_MODEL_V1_202021", "TEMPCAAD.LIFT_MODEL_V1_202022" and so on...

I am trying to append all datasets but some of them doesn't exist, so when i run the following code I get the error

Dataset "TEMPCAAD.LIFT_MODEL_V1_202022" does not exist.

%let currentmonth = &anomes_scores;
%let previousyearmonth = &anomes_x12;


data _null_;
length string $1000;
cur_month = input("&previousyearmonth.01",yymmdd8.);
do until (cur_month > input("&currentmonth.01",yymmdd8.));
  string = catx(' ',trim(string),'TEMPCAAD.LIFT_&NOME_MODELO._&VERSAO_MODELO._'!! put(cur_month,yymmn6.));
  cur_month = intnx('month',cur_month,1,'b');
end;
call symput('mydatasets',trim(string));
%put &mydatasets;
run;


data WORK.LIFTS_U6M;
set &mydatasets.;
run;

How can I append only existing datasets?

Upvotes: 0

Views: 529

Answers (1)

Kermit
Kermit

Reputation: 3117

Instead of looping on every file to see whether it exist or not, why don't you just extract all the dataset names from dictionary.tables?

libname TEMPCAAD "/home/kermit/TEMPCAAD";

data tempcaad.lift_model_v1_202110 tempcaad.lift_model_v1_202111 tempcaad.lift_model_v1_202112;
id = 1;
output tempcaad.lift_model_v1_202110;
id = 2;
output tempcaad.lift_model_v1_202111;
id = 3;
output tempcaad.lift_model_v1_202112;
run;

%let nome_modelo = MODEL;
%let versao_modelo = V1;

proc sql;
  select strip("TEMPCAAD."||memname) into :dataset separated by " "
    from dictionary.tables
    where libname="TEMPCAAD" and memname like "LIFT_&NOME_MODELO._&VERSAO_MODELO.%";
quit;

data want;
  set &dataset.;
run;

You can easily tweak the where statement to only extract the data that you wish to append. Just remember to put double quotes if you specify a macro-variable in it.

enter image description here

Upvotes: 2

Related Questions