DATA Step, Macro, Functions and more

macro program to check a data-set exists in library

Reply
Regular Contributor
Posts: 190

macro program to check a data-set exists in library

macro program to check a data-set exists in library?

 

Regular Contributor
Posts: 190

Re: macro program to check a data-set exists in library

%let dsname=data1;
%let dslib=work;
%let outputflag=isexist;
%let isexist=0;
%dsexist(dsname=data1,dslib=work,outputflag=isexist);
Super User
Super User
Posts: 7,407

Re: 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

 

Frequent Contributor
Posts: 130

Re: macro program to check a data-set exists in library

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);
Regular Contributor
Posts: 190

Re: macro program to check a data-set exists in library

can i add these variables to the code
%let dsname=data1;
%let dslib=work;
%let outputflag=isexist;
%let isexist=0;
%dsexist(dsname=data1,dslib=work,outputflag=isexist);
will it work ?? in ur code
Super User
Super User
Posts: 7,407

Re: macro program to check a data-set exists in library

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.

Regular Contributor
Posts: 190

Re: macro program to check a data-set exists in library

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?
Regular Contributor
Posts: 190

Re: macro program to check a data-set exists in library

and can i use %let sysfunc or % to display in d log a msg does the DS exist or not
Super User
Super User
Posts: 7,407

Re: macro program to check a data-set exists in library

%put Dataset (1=Exists, 0=Not Exists): %sysfunc(exist(sashelp.class));

Regular Contributor
Posts: 190

Re: macro program to check a data-set exists in library

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.

Regular Contributor
Posts: 190

Re: macro program to check a data-set exists in library

which logic do i apply with these above given parameters.
Super User
Super User
Posts: 6,502

Re: macro program to check a data-set exists in library

[ Edited ]

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)

 

Regular Contributor
Posts: 190

Re: macro program to check a data-set exists in library

RW( i cannot understand your code Smiley Surprised
Frequent Contributor
Posts: 130

Re: macro program to check a data-set exists in library

[ Edited ]

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?

Regular Contributor
Posts: 190

Re: macro program to check a data-set exists in library

Log if data set doesn't exist: "work.data1 outputflag = 0".
Log if data set does exist: "work.data1 outputflag = 1". yes
Ask a Question
Discussion stats
  • 15 replies
  • 335 views
  • 0 likes
  • 4 in conversation