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!!
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
A good start:
Post the code that might generate a message about invalid data.
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.
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.
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.
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.
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!!
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.