BookmarkSubscribeRSS Feed
R_Chung
Quartz | Level 8

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.

6 REPLIES 6
Reeza
Super User

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.

R_Chung
Quartz | Level 8
Is there a missing in hyperlink @Reeza
Reeza
Super User

@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

 

 

ballardw
Super User

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 ...

ShiroAmada
Lapis Lazuli | Level 10

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.

 

Kurt_Bremser
Super User

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

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 6 replies
  • 28314 views
  • 1 like
  • 5 in conversation