DATA Step, Macro, Functions and more

Check a dataset exist or not

Reply
Contributor
Posts: 48

Check a dataset exist or not

Hi all, 

 

I would like to check whether a dataset exist first then join the dataset with another dataset.

Below is my code:

if (exist(ot_reclus_kw_kc)) then do;
    data semi_ot_result; 
	 	set ot_reclus_kw_kc semi_ot_result; 
	 run;
end;

Is there any errors/mistakes?

 

Thanks.

Super User
Posts: 22,857

Re: Check a dataset exist or not

You can't execute conditional code like that in SAS. 

You'll have to use macro programming and %if/%then

 

If you search on here or lexjansen.com you'll find a lot of examples on how to do this.

Contributor
Posts: 48

Re: Check a dataset exist or not

Is there a missing in hyperlink @Reeza
Super User
Posts: 22,857

Re: Check a dataset exist or not


R_Chung wrote:
Is there a missing in hyperlink @Reeza

No, I assumed that google is sufficient. 

Use terms "SAS macro check if data exists"

 

Returns:

http://support.sas.com/kb/24/670.html

 

 

Super User
Posts: 13,066

Re: Check a dataset exist or not

Until you are very familiar with the possible errors in SAS, I suggest that you do NOT combine data sets or use the structure:

 

data someset;

    set someset ;

 

as if there is an error you may corrupt or modify you dataset someset in such a way that you lose or destroy some existing data and have to recover the original version from somewhere else. I would recomend testing with

 

data combined;

    set ot_reclus_kw_kt semi_ot_result;

run;

 

You may find that you want a merge or update instead of set and if you make the wrong choice ...

Frequent Contributor
Posts: 113

Re: Check a dataset exist or not

[ Edited ]

Try this......

 

Submit your code, check the sas log.  

 

Check your DATA and SET statements.  Changing the position of the tables in your SET statement will create the difference unless the tables have identical structure.

 

If the structure is identical then better use proc append or proc sql.

 

Creating a macro program like this:

 

 

%MACRO <MACRONAME>;
%if %sysfunc(exist(<DATASET_NAME>)) %then %do;
<PUT YOUR DATA STEP HERE>
%end; 
%MEND;
 
%<MACRONAME>;

 

 

Hope this helps.

 

Super User
Posts: 9,594

Re: Check a dataset exist or not


R_Chung wrote:

Hi all, 

 

I would like to check whether a dataset exist first then join the dataset with another dataset.

Below is my code:

if (exist(ot_reclus_kw_kc)) then do;
    data semi_ot_result; 
	 	set ot_reclus_kw_kc semi_ot_result; 
	 run;
end;

Is there any errors/mistakes?

 

Thanks.


Yes, there are Smiley Wink

if - then - else is data step code (and some procedures also allow such syntax, eg proc report), and can't be used outside of a data step.

Also keep in mind that data or procedure steps cannot be nested.

Such problems are best solved by using the macro preprocessor.

Method 1: do the logic in a data step and save the result to a macro variable:

data _null_;
if (exist("ot_reclus_kw_kc"))
then call symput('add_file',"ot_reclus_kw_kc");
else call symput('add_file','');
run;

data semi_ot_result; 
set
  &add_file
  semi_ot_result
;
run;

Method 2: use a macro:

%macro add_file(testfile);
%if %sysfunc(exist(&testfile))
%then %do;
&testfile
%end;
%mend;

data semi_ot_result; 
set
  %add_file(ot_reclus_kw_kc)
  semi_ot_result
;
run;

As others have mentioned, rewriting a dataset in the same step can be dangerous and lead to data loss that necessitates a rerun of all previous code.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Ask a Question
Discussion stats
  • 6 replies
  • 351 views
  • 0 likes
  • 5 in conversation