macro program to check a data-set exists in library?
Have a look at the SAS documentation, exists() function is the one you are looking for:
https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000210903.htm
Here's a simple macro program to do what you want. Just need to modify the code based on what you want to do if the data set exists, and what you want to do if the data set doesn't exist. Hope this helps:
%macro check(dsn);
%if %sysfunc(exist(&dsn)) %then %do;
/***Insert code on what to do if data set exists***/
proc print data=&dsn;
run;
%end;
%else %do;
/***Insert code on what to do if data set doesn't exist***/
%put "Data set &dsn does not exist!";
%end;
%mend;
%check(datasetname);
Why, the sysntax is:
%let dsname=data1;
%let dslib=work;
%let isexist=%sysfunc(exist(&dslib..&dsname.));
Or if for some reason you have to have a macro:
%macro dsexist (dsname=,dslib=);
%sysfunc(exist(&dslib..&dsname.)
%mend dsexist;
data want;
isexist=%dsexist(dslib=work,dsname=your_dataset);
output;
run;
Note that the missing semicolon after the %sysfunc is deliberate so that it can be used directly in code.
%put Dataset (1=Exists, 0=Not Exists): %sysfunc(exist(sashelp.class));
hello RW9 thanks for d reply but
this issue how can i solve-> so how can i incorporate this in your code?
1>dsexist is my macro.
2>variables defined->
%let dsname=data1;
%let dslib=work;
%let outputflag=isexist;
%let isexist;->its default value is 0 if 1 then dataset exist if 0 DS doest not exist.
@RTelang wrote:
1>dsexist is my macro.
2>variables defined->
%let dsname=data1;
%let dslib=work;
%let outputflag=
%let isexist;->default value is 0 if 1 then dataset exist 0 ds doest not exist.
so how can i incorporate this in your code?
There is no need to define additional macro varaibles beyond the parameters to the macro. It sounds like you want a macro that will set the value into the macro variable named in one of its parameters.
%macro dsexist(_dsn,_lib,_mvar);
%if (&_lib=) %then %let _lib=WORK;
%if (&_mvar=) %then %let _mvar=DSEXIST ;
%if not %symexist(&_mvar) %then %global &_mvar;
%let &_mvar=%sysfunc(exist(&_lib..&dsn));
%mend dsexist;
So if you want to set ISEXIST to indicate if WORK.DATA1 exists then you would call it like this:
%dsexist(data1,work,isexist)
Or like this if you prefer to call with named parameters.
%dsexist(_dsn=data1,_lib=work,_mvar=isexist)
Now if you already have the member name and libref in macro variables then use those in the call instead.
%let dsname=data1;
%let dslib=work;
%let outputflag=isexist;
%dsexist(&dsname,&dslib,&outputflag)
You can modified the code to include whatever variables you want. I'm not sure exactly what you're looking to do with your outputflag and isexist macros though. If you're just wanting to know if the data set exists and if it does or doesn't, display that in the log, you can do it by utilizing the exist() function as RW9 showed. The value is either a 0 or 1 returned depending if the data set exists or not, so I don't think you would need to hardcode your outputflag and isexist values. Below would be the macro modified with your variables if all you want to do is display in the log whether the data set exists or not:
%macro dsexist(dsname=,dslib=);
%put &dslib..&dsname outputflag = %sysfunc(exist(&dslib..&dsname));
%mend;
%dsexist(dsname=data1,dslib=work);
Log if data set doesn't exist: "work.data1 outputflag = 0".
Log if data set does exist: "work.data1 outputflag = 1".
Is this what you're trying to achieve?
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.