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.
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 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
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 ...
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.
@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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.