How to execute a different group of statements depending on which variable contains invalid value?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 11
Accepted Solution

How to execute a different group of statements depending on which variable contains invalid value?

If a data error occours, SAS will

- write  in the log an invalid data note, information about the variable for which there is the invalid value and the observation containing the invalid value,

- set the variable _ERROR_ to 1.

 

I would like to execute a different group of statements depending on which variable contains the invalid value, for example: 

 

if the invalid value is for var1 then
   do;
      ...;
   end;
else if the invalid value is for var2 then
   do;
      ...;
   end;
else 
   do;
      ...;
   end;

 

How could I do it?

Any help is appreciated.

 

Thanks!!


Accepted Solutions
Solution
‎02-24-2017 03:11 AM
Super User
Super User
Posts: 6,364

Re: How to execute a different group of statements depending on which variable contains invalid valu

Read the data as text strings and then convert it your self.

data want;
 length s1 s2 $30 timestamp_1 timestamp_2 8;
 format timestamp_1 timestamp_2 DATETIME19.;
 infile datalines; 
 input s1 s2 ;
 array s s1-s2;
 array t timestamp_1-timestamp_2 ;
 do i=1 to dim(s);
   t(i) = input(s(i),??e8601dz30.);
   if missing(t(i)) then t(i)=input(s(i),??anydtdtm30.);
 end;
 put (_all_) (=/);
datalines;
2013-07-29T11:55:21Z 2016-07-29T11:35:21Z
2014-08-04T22:12:49+02:00 2011-11-15
2018-11-1 2016-07-29T11:35:21Z
;

Results

s1=2013-07-29T11:55:21Z
s2=2016-07-29T11:35:21Z
timestamp_1=29JUL2013:11:55:21
timestamp_2=29JUL2016:11:35:21
i=3

s1=2014-08-04T22:12:49+02:00
s2=2011-11-15
timestamp_1=04AUG2014:20:12:49
timestamp_2=15NOV2011:00:00:00
i=3

s1=2018-11-1
s2=2016-07-29T11:35:21Z
timestamp_1=01NOV2018:00:00:00
timestamp_2=29JUL2016:11:35:21
i=3

View solution in original post


All Replies
Respected Advisor
Posts: 4,991

Re: How to execute a different group of statements depending on which variable contains invalid valu

A good start: 

 

Post the code that might generate a message about invalid data.

Esteemed Advisor
Esteemed Advisor
Posts: 7,232

Re: How to execute a different group of statements depending on which variable contains invalid valu

Test data - in the form of a datastep, and required output.  It depends on what you are checking, personally I would avoid checking the SAS output and pre-validate yourself.  Also note, it may be tidier to have a select() statement - but again depends on your data/logic.

Super User
Posts: 1,238

Re: How to execute a different group of statements depending on which variable contains invalid valu

[ Edited ]

What do you mean by invalid value ?

Is it a numeric or a character type variable? 

Are you expecting a numeric value in a char type variable ?

Do you have a table or informat to check validity of the variables?

 

You need give more information about your variables, expected values and 

how to define invalid values.

Grand Advisor
Posts: 10,218

Re: How to execute a different group of statements depending on which variable contains invalid valu

Are you looking for this only for a program that reads external data? If so I suspect that you will need to modify your input statement into something like:

 

input var1 @;

if _error_ then do;

<code>;

end;

input var2 @;

if _error_ then do;

<code>;

end;

As when the _error_ flag is thrown it applies to a statement. If you have multiple variables with issues then you don't know which one.

HOWEVER if the data is 100% always and forever supposed to have some value assigned you could use :

If missing(var1) then do;

...

end;

After the current input statement.

Super User
Posts: 789

Re: How to execute a different group of statements depending on which variable contains invalid valu

I think you have to reset _ERROR_=0 within each do group.   Otherwise all do groups subsequent to the first variable provoking an error will be executed.

Occasional Contributor
Posts: 11

Re: How to execute a different group of statements depending on which variable contains invalid valu

[ Edited ]

You are right... please find below the code generating error:

 

 data want;
 infile datalines;
 informat timestamp_1 E8601DZ26.;
 format timestamp_1 DATETIME.;
 informat timestamp_2 E8601DZ26.;
 format timestamp_2 DATETIME.;

 input 
   timestamp_1
   timestamp_2;

 datalines;
 2013-07-29T11:55:21Z 2016-07-29T11:35:21Z 
 2014-08-04T22:12:49+02:00 2011-11-15
 2018-11-1 2016-07-29T11:35:21Z
 ;

 

It follows that the dates are not imported.

I have already tried to import everything by informat ANYDTDTM. but timestamps are not always correctly imported.

I would like to be able to import both the timestamps and dates correctly. I thought to set some condition to import by informat E8601DZ26. by default, and switch to ANYDTDTM. when the field of the variable contains a date.

Any other suggestion is welcome.

 

Thanks!!

 

Solution
‎02-24-2017 03:11 AM
Super User
Super User
Posts: 6,364

Re: How to execute a different group of statements depending on which variable contains invalid valu

Read the data as text strings and then convert it your self.

data want;
 length s1 s2 $30 timestamp_1 timestamp_2 8;
 format timestamp_1 timestamp_2 DATETIME19.;
 infile datalines; 
 input s1 s2 ;
 array s s1-s2;
 array t timestamp_1-timestamp_2 ;
 do i=1 to dim(s);
   t(i) = input(s(i),??e8601dz30.);
   if missing(t(i)) then t(i)=input(s(i),??anydtdtm30.);
 end;
 put (_all_) (=/);
datalines;
2013-07-29T11:55:21Z 2016-07-29T11:35:21Z
2014-08-04T22:12:49+02:00 2011-11-15
2018-11-1 2016-07-29T11:35:21Z
;

Results

s1=2013-07-29T11:55:21Z
s2=2016-07-29T11:35:21Z
timestamp_1=29JUL2013:11:55:21
timestamp_2=29JUL2016:11:35:21
i=3

s1=2014-08-04T22:12:49+02:00
s2=2011-11-15
timestamp_1=04AUG2014:20:12:49
timestamp_2=15NOV2011:00:00:00
i=3

s1=2018-11-1
s2=2016-07-29T11:35:21Z
timestamp_1=01NOV2018:00:00:00
timestamp_2=29JUL2016:11:35:21
i=3
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 144 views
  • 0 likes
  • 7 in conversation