Help using Base SAS procedures

How to protect dataset after error?

Reply
Regular Contributor
Posts: 222

How to protect dataset after error?

When run a SAS using interactive mode, when error happened,how to stop the code and not override the rest dataset with an empty dataset?

if we select the follow three datasteps and submit them together, in the first step,dataset 'a' would be created ,

then in the second ,there will be a error bacuse no dataset named sashelp.class_notexist .

and then in the third step, dataset 'a' will be destroyed .

How to specify an option to protect dataset 'a' after error happen? so when an error happen SAS  will stop do any other thing and keep dataset 'a' as the first step.

data a;

set sashelp.class;

run;

data one;

set sashelp.class_notexist;

run;

data a;

set one;

run;

Trusted Advisor
Posts: 1,615

Re: How to protect dataset after error?

There are many solutions.

One is simply using different programming practice, where you don't reuse the names of datasets in a program. Then data a never gets overwritten because the last data step is named data b;

The other way that comes to mind is to check the number of observations in data one, and if you find out that data one has zero observations, you don't perform the next step. I'm pretty sure you would have to do this with macros.

Regular Contributor
Posts: 222

Re: How to protect dataset after error?

The code is just an example; I want to find a mothod deal with error,and when error happend not go ahead to anything,

another example:After the following code is finshed I don't want sashelp.class be replaced.

Thanks

data errorone;/*This dataset just for generate an error */

set noexist;

run;

/*when error happened next step should not be run,----------how to do it?? */

data sashelp.class;

set errorone;

run;

Trusted Advisor
Posts: 1,615

Re: How to protect dataset after error?

Yes, I understand your code is an example. But now you have provided more code, which is also an example, and ignored my suggestions.

As I said, the simplest way is to not re-use dataset names in your code. Then sashelp.class doesn't get replaced. Is that not a valid solution for you?

As I said, you can check to see how many observations are in errorone, and if it is zero, do not perform the next step. Is that not a valid solution for you?

Regular Contributor
Posts: 222

Re: How to protect dataset after error?

Thank you ,

but I think you must mis-understand my question.

1. I need the dataset to be override by another dataset  if there was no error happen.

2.As you said ,you can check error and not perform next if error there,this sounds like the solution I need ,but what is it? I will be very appreciate if you would show me the detail.

Thanks!

Mike

Super User
Super User
Posts: 6,500

Re: How to protect dataset after error?

To get the most control you would need to wrap your code into a macro. Then you can use macro logic at key points to stop processing.  Using you simple example you could do something like:

%macro a;

data a;

  set sashelp.class;

run;

data one;

  set sashelp.class_notexist;

run;


%if not &syserr %then %do;

  data a;

    set one;

  run;

%end

%mend a;

%a;

Super User
Posts: 5,082

Re: How to protect dataset after error?

Mike,

You can check for specific conditions (using macro language).  But you cannot check for "any sort of error" until after the DATA step runs.  Here are a few tools that may come in handy.

fileexist checks for the existence of a folder or raw data file

exist checks for the existence of a SAS data set

So you could (inside a macro) code:

%if %sysfunc(exist(mylib.my_dataset)) %then %do;

Then the %do group code would only run when your SAS data set exists.

Is that the sort of thing you are looking for?

Good luck.

Super User
Posts: 9,681

Re: How to protect dataset after error?

You need a special dataset option.

REPEMPTY= Data Set Option

Specifies whether a new, empty data set can overwrite an existing SAS data set that has the same

name.

Ksharp

Regular Contributor
Posts: 222

Re: How to protect dataset after error?

Thanks,

Also ,For both the convenience of replacing existing data sets with new ones that contain data and the protection of not overwriting existing data sets with new empty ones that are created by accident, set REPLACE=YES and REPEMPTY=NO.

one thing need to be mentioned, system options will replace only permernent dataset,

temperory dataset need to use replace=  dataset option

Regular Contributor
Posts: 222

Re: How to protect dataset after error?

How about the following code ,please advise me,

Thanks!

Mike

data one;

set notexist;

run;

%let ee=&syserr;

%macro diablo3(monk);

data a(%if &monk=1 %then %do; replace=no %end; %else %do; replace=yes %end);

set one;

run;

%mend diablo3;

%diablo3(monk=&ee);

Ask a Question
Discussion stats
  • 9 replies
  • 281 views
  • 6 likes
  • 5 in conversation