BookmarkSubscribeRSS Feed
Pradeepbanu
Obsidian | Level 7

I doing like this

libname DZY 'Path';
Proc sql;
select * from DZ.some_table;
run; 

Here I have to add an error handling like if something goes wrong in select statement or within the block I have to write error message to an separate text file in the folder. This is what I tried

%macro sortclass;
 Proc sql;
    select * from DZ.some_table;
    run; 
%if &SQLRC gt 0 %then %goto error;
%error:
proc export data=""
run;

%exit:
%mend; 
%sortclass;

I am trying to do an try catch like error handling.., How can do this in effective way. Thanks In advance

4 REPLIES 4
andreas_lds
Jade | Level 19

You should not do this, because you have to re-invent what the sas interpreter already does: checking the syntax of your code.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Why?  You can check the existence of dataset, which is something that maybe necessary.  However with such simple code you should not need to check?  This: 

%macro sortclass;

Sounds to me like your trying to re-write SAS functionality which is both a waste of time, and less stable.

ballardw
Super User

@Pradeepbanu wrote:

I doing like this

libname DZY 'Path';
Proc sql;
select * from DZ.some_table;
run; 

Here I have to add an error handling like if something goes wrong in select statement or within the block I have to write error message to an separate text file in the folder. This is what I tried

%macro sortclass;
 Proc sql;
    select * from DZ.some_table;
    run; 
%if &SQLRC gt 0 %then %goto error;
%error:
proc export data=""
run;

%exit:
%mend; 
%sortclass;

I am trying to do an try catch like error handling.., How can do this in effective way. Thanks In advance


Your proc export is going to throw an error as you are missing ; and you are missing an OUTFILE option.

Since %exit is not referenced that will generate errors in the log. Also since you do not have anything that skips your  proc export it will execute every time. See this code for examples:

Wrong as it output the value regardless of the value of the test.

%macro dummy(value);
%if &value= 3 %then %goto error;

%error: %Put the value of value is &value;
%mend;

%dummy(3);
%dummy(5);

Marginally better as only has the output when the test is true:

 

%macro dummy2(value);
%if &value=3 %then %goto error;
%else %goto exit;
%error: %Put the value of value is &value;
%exit:
%mend;

%dummy2(3);
%dummy2(5);

 

Pradeepbanu
Obsidian | Level 7

Thanks this is an excellent solution.., I have a question in writing file??

 

libname DZ 'Dataset';
%macro dummy2(value);
proc sql;
select * from DZ.HighV_Trans /* misspelled */ 
run;

%if &value=3 %then %goto error;
%else %goto exit;
%error:

/* here how do I write error in this file??  */

proc export data = ??
outfile ="C/Pradeep/Log.txt"
/*dbms = csv replace;*/
run;
%Put the value of value is &value;
%exit:
%mend;


%dummy2(3);
%dummy2(5);

 

 

SAS Innovate 2025: Call for Content

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!

Submit your idea!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 4739 views
  • 0 likes
  • 4 in conversation