BookmarkSubscribeRSS Feed
Mike_Davis
Fluorite | Level 6

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;

9 REPLIES 9
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Mike_Davis
Fluorite | Level 6

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;

PaigeMiller
Diamond | Level 26

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?

--
Paige Miller
Mike_Davis
Fluorite | Level 6

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

Tom
Super User Tom
Super User

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;

Astounding
PROC Star

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.

Ksharp
Super User

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

Mike_Davis
Fluorite | Level 6

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

Mike_Davis
Fluorite | Level 6

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 9 replies
  • 1435 views
  • 6 likes
  • 5 in conversation