BookmarkSubscribeRSS Feed
RTelang
Fluorite | Level 6

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

 

15 REPLIES 15
RTelang
Fluorite | Level 6
%let dsname=data1;
%let dslib=work;
%let outputflag=isexist;
%let isexist=0;
%dsexist(dsname=data1,dslib=work,outputflag=isexist);
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

 

dcruik
Lapis Lazuli | Level 10

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);
RTelang
Fluorite | Level 6
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
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

RTelang
Fluorite | Level 6
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?
RTelang
Fluorite | Level 6
and can i use %let sysfunc or % to display in d log a msg does the DS exist or not
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

RTelang
Fluorite | Level 6

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
Fluorite | Level 6
which logic do i apply with these above given parameters.
Tom
Super User Tom
Super User

@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)

 

RTelang
Fluorite | Level 6
RW( i cannot understand your code 😮
dcruik
Lapis Lazuli | Level 10

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?

RTelang
Fluorite | Level 6
Log if data set doesn't exist: "work.data1 outputflag = 0".
Log if data set does exist: "work.data1 outputflag = 1". yes

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 15 replies
  • 8074 views
  • 0 likes
  • 4 in conversation