tags:

views:

1496

answers:

3

Is there a way to test if a variable will fail the INPUT conversion process in SAS ? Or alternatively, if the resulting "NOTE: Invalid argument" message can be avoided?

data null;
format test2 date9.;
input test ;
test2=INPUT(PUT(test,8.),yymmdd8.);
if error =1 then do;
error=0;
test2=INPUT(PUT(test-1,8.),yymmdd8.);
end;
put test2=;
cards;
20270229
run;

+1  A: 

Instead of doing it this way, you could treat your variable as a character variable at first (since you know some of the values are not real dates anyway), then use the macro provided by AFHood to help you find the invalid values, then fix those values, then convert the variable to a numeric var once all the data are clean.

Louisa Grey
Thanks for thisHowever the date comes directly from a series of large, column seperated flat files - and if I were to read the data twice this would significantly add to the processing time.Also, (I may be wrong) but wouldn't the above be more efficient in terms of conversion resource?
Bazil
+2  A: 

Just include "??" before the format name. Your example has been modified below...

data null;
format test2 date9.;
input test ;
test2=INPUT(PUT(test,8.),?? yymmdd8.);
if error =1 then do;
error=0;
test2=INPUT(PUT(test-1,8.), ?? yymmdd8.);
end;
put test2=;
cards;
20270229
run;
Jay Stevens
I think it would be better to use a single ? instead of double ?? in the input function. This way he can still have access to the _error_ variable to use for the rest of the logic in the data step. Otherwise _error_ will remain at 0.
cmjohns
A: 

You could check using something like the following:

data null; format test2 date9.; input test ; test2=INPUT(PUT(test,8.),yymmdd8.); if test ne '' and nmiss(test2) then test2=INPUT(PUT(test-1,8.),yymmdd8.); put test2=; cards; 20270229 run;