5

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;  
Triad sou.
  • 2,969
  • 3
  • 23
  • 27
Allan Bowe
  • 12,306
  • 19
  • 75
  • 124

3 Answers3

5

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
  • 5,863
  • 9
  • 44
  • 67
  • 2
    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 Mar 11 '09 at 17:38
1

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.

Community
  • 1
  • 1
Lauren Samuels
  • 2,419
  • 3
  • 19
  • 20
  • Thanks for this However 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? – Allan Bowe Feb 19 '09 at 10:29
0

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;
Triad sou.
  • 2,969
  • 3
  • 23
  • 27