Help using Base SAS procedures

Error handling in Proc Sql

Reply
Occasional Contributor
Posts: 13

Error handling in Proc Sql

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

Valued Guide
Posts: 580

Re: Error handling in Proc Sql

Posted in reply to Pradeepbanu

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

Super User
Super User
Posts: 9,599

Re: Error handling in Proc Sql

Posted in reply to Pradeepbanu

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.

Super User
Posts: 13,583

Re: Error handling in Proc Sql

[ Edited ]
Posted in reply to Pradeepbanu

@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);

 

Occasional Contributor
Posts: 13

Re: Error handling in Proc Sql

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);

 

 

Ask a Question
Discussion stats
  • 4 replies
  • 333 views
  • 0 likes
  • 4 in conversation