Hello,
I want to create a new data set using set statement.
IF the data set dosn't exit then I want that nothing will be done(data set will not created) and there will not be an error.
The error is
NOTE: Line generated by the invoked macro "IF_EXIST_OLD". 31 r_r.Panel ____________________________ 557 ERROR: DATA STEP Component Object failure. Aborted during the COMPILATION phase. ERROR 557-185: Variable r_r is not an object.
%macro if_exist_old;
%if %sysfunc(exist(r_r.Panel)) %then r_r.Panel;
%mend;
Data r_r.Backup;
%if_exist_old;
Run;
You don't have a SET statement in your code.
This is why we advise people to create working SAS code without macros before you try to write a macro. In this case, you need working SAS code for the case where the data set exists, and also working code for the case where the data set doesn't exist. If you had this, you would have a much better chance of achieving your goal of creating code for these situations.
You can do it without a macro nowadays:
data r_r.backup;
set
%if %sysfunc(exist(r_r.panel))
%then %do;
r_r.panel
%end;
;
run;
@Kurt_Bremser wrote:
You can do it without a macro nowadays:
data r_r.backup; set %if %sysfunc(exist(r_r.panel)) %then %do; r_r.panel %end; ; run;
I don't believe that the OP has SAS 9.4 TS1M5 which I think is required for this to work.
@Kurt_Bremser wrote:
You can do it without a macro nowadays:
data r_r.backup; set %if %sysfunc(exist(r_r.panel)) %then %do; r_r.panel %end; ; run;
@Kurt_Bremser You definitely haven't tested this code. In my environment an "empty" set statement uses _last_ - and using EG there is always a _last_. And with SET also in the macro condition an empty data set gets created.
I was only showing use of macro code, without regard for the validity of the created code.
I am sure you can adept the solution provided provided in your older thread: https://communities.sas.com/t5/SAS-Programming/SET-data-sets-IF-exits/m-p/754937#M238173
Hello,
The target is :
IF data set ABC exist then create a new data set called wanted (using SET statement).
IF data set ABC doesnt exist then do nothing! (No create empty data set).
I run this code and get a strange results.
Data set ABC doesn't exist but I get a data set wanted with following columns
%macro if_exist_old;
%if %sysfunc(exist(ABC)) %then ABC;
%mend;
Data wanted;
SET
%if_exist_old;
Run;
You need to enclose the whole data step in the macro %IF %THEN %DO - %END block.
You already made such code yourself in your other thread (way 1).
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.