MarieT
MarieT

Reputation: 57

SAS - Missing value replacement "." by 0 for numeric but not for date

I have a SAS macro:

%MACRO missing_values (ds);
    DATA &ds. (drop=_i);
        SET &ds. ;
        ARRAY A_VarNum[*] _NUMERIC_;
        DO _i = 1 TO dim(A_VarNum);
            IF A_VarNum (_i)=. THEN A_VarNum (_i)=0 ;
        END;
    RUN;
%MEND;

which replaces . by 0, but I would like that dates stay the way they are, so that they are not replaced by 01/01/1960.

Is this possible?

Upvotes: 0

Views: 1634

Answers (2)

Jetzler
Jetzler

Reputation: 797

Do the date variables have a format assigned to them? Are they all the same? In that case you could check for the formatted value '01/01/1960' using the VVALUE function and change the values back to missing. Not very efficient, but SAS is not very good at checking for dates.

data test;
format e ddmmyy10.;
    do i = 1 to 10;
     e = .;
     f =.;
     output;
    end;
run;

%MACRO missing_values (ds);
    DATA &ds. (drop=_i);
        SET &ds. ;
        ARRAY A_VarNum[*] _NUMERIC_;
        DO _i = 1 TO dim(A_VarNum);
            IF A_VarNum (_i)=. THEN DO;
                A_VarNum (_i)=0;
                IF VVALUE(A_VarNum (_i))='01/01/1960' THEN A_VarNum (_i)=.;
            END ;
        END;
    RUN;
%MEND;
%missing_values(test);

Upvotes: 0

user667489
user667489

Reputation: 9569

If you never expect to encounter 1st January 1960 in your date variable, another option would be to define a custom date format that displays missing values for that date, e.g.

proc format;
 value mydate
  low--1 = [yymmdd10.]
   0     = '.'
   1-high= [yymmdd10.]
   ;
run;

data _null_;
 format datevar mydate10.;
 do datevar = -1,0, 24000;
  put datevar=;
 end;
run;

Output:

 datevar=1959-12-31
 datevar=.
 datevar=2025-09-16

Upvotes: 1

Related Questions