11-18-2015 04:31 AM
11-18-2015 05:47 AM
Have a look at the SAS documentation, exists() function is the one you are looking for:
11-18-2015 11:50 AM
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);
11-19-2015 12:38 AM
11-19-2015 04:22 AM
Why, the sysntax is:
Or if for some reason you have to have a macro:
%macro dsexist (dsname=,dslib=);
Note that the missing semicolon after the %sysfunc is deliberate so that it can be used directly in code.
11-19-2015 05:18 AM
11-19-2015 09:14 AM
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.
%let isexist;->its default value is 0 if 1 then dataset exist if 0 DS doest not exist.
11-19-2015 12:18 PM - edited 11-19-2015 12:19 PM
1>dsexist is my macro.
%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:
Or like this if you prefer to call with named parameters.
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)
11-19-2015 09:19 AM - edited 11-19-2015 11:31 AM
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?