Hello
I want to combine a list of data sets and it might happen that some of them doesnt exist.
What is the way to change the following statements in order to add the condition ONLY IF EXISTS
%macro sset;
%do j=1 %to &m.;
%let mon=%scan(&vector.,&j.,+);
LGD&mon. /*How to add the condition only if data set exists*/
%end;
%mend sset;
%put %sset;
Data LGD2101;
Input ID X;
CARDS;
1 10
2 20
3 30
;
Run;
Data LGD2102;
Input ID X;
CARDS;
4 10
5 20
7 30
;
Run;
Data LGD2105;
Input ID X;
CARDS;
8 60
9 40
;
Run;
%let vector=2101+2102+2103+2104+2105;
%let m=5;
%macro sset;
%do j=1 %to &m.;
%let mon=%scan(&vector.,&j.,+);
LGD&mon.
%end;
%mend sset;
%put %sset;
data LGD_ALL;
SET %sset_LGD;
Run;
data LGD_ALL;
SET %sset;
Run;
Isn't that what other macro does ?
Here is how I would code it:
%macro if_exist(prefix,list);
%local j dsn;
%do j=1 %to %sysfunc(countw(&list,+));
%let dsn=&prefix.%scan(&list.,&j.,+);
%if %sysfunc(exist(&dsn)) %then &dsn;
%end;
%mend if_exist;
%let vector=2101+2102+2103+2104+2105;
data wanted;
set %if_exist(prefix=LGD,list=&vector) ;
run;
Build actual data set names.
Search for them in SASHELP.VTABLE or Dictionary.Tables. Remember Memname and Library have to be upper case.
We have already discussed how to determine if a SAS data set exists in your last thread.
So you don't want to use a method that would work (SASHELP.VTABLE). Why?
Do you also want to not use the method we discussed yesterday? Why?
Typically, most people (including me) are happy to know two ways to accomplish a task, in fact one way is enough for me in most cases.
If you have some particular constraints based upon your exact problem, please explain so we don't provide solutions that won't match the problem.
If you want to test if a dataset exists use the EXIST() function.
%macro sset;
%local j dsn;
%if not %symexist(vector) %then %let vector=;
%do j=1 %to %sysfunc(countw(&vector,+));
%let dsn=LGD%scan(&vector.,&j.,+);
%if %sysfunc(exist(&dsn)) %then &dsn;
%end;
%mend sset;
I am looking for a solution that is siamilar to this solution,
But in my program it is a bit different because user should define a macro var called vector that contrain dates (YYMM) that need to be set ....May anyone help to adjust the code to my task
%macro if_exists(varlist);
%if %sysfunc(exist(%scan(&varlist,1))) %then %scan(&varlist,1);
%mend;
data wanted;
SET
%if_exists(LGD2101)
%if_exists(LGD2102)
%if_exists(LGD2103)
%if_exists(LGD2104)
%if_exists(LGD2105);
run;
Isn't that what other macro does ?
Here is how I would code it:
%macro if_exist(prefix,list);
%local j dsn;
%do j=1 %to %sysfunc(countw(&list,+));
%let dsn=&prefix.%scan(&list.,&j.,+);
%if %sysfunc(exist(&dsn)) %then &dsn;
%end;
%mend if_exist;
%let vector=2101+2102+2103+2104+2105;
data wanted;
set %if_exist(prefix=LGD,list=&vector) ;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.