BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
user_
Calcite | Level 5

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!!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

7 REPLIES 7
Astounding
PROC Star

A good start: 

 

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Shmuel
Garnet | Level 18

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.

ballardw
Super User

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.

mkeintz
PROC Star

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
user_
Calcite | Level 5

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!!

 

Tom
Super User Tom
Super User

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

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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