Using Haven for SAS Data with Special Missing Numeric

Oct 27, 2019 R SAS

Make Data to Test With

Run this in SAS code to make the data:


proc format;
    value mis
        .d = "Don’t Know (DK)"
        .r = "Refused (REF)"
        .u = "Unreadable"
        .m = "Missed"
        .n = "Not Applicable (NA)";
run;

data "c:\blah\blah.sas7bdat";
format favoriteNumber mis.;
input subject favoriteNumber;
datalines;
0 42
1 .d
2 .r
3 .u
4 .m
5 .n
6 .
7 242
;
run;

Load the data in R with haven:

library(haven)
blah <- read_sas("../../blah.sas7bdat") 

Notice that there is no indication that the special missing values were loaded:

blah
# A tibble: 8 x 2
  favoriteNumber subject
           <dbl>   <dbl>
1             42       0
2             NA       1
3             NA       2
4             NA       3
5             NA       4
6             NA       5
7             NA       6
8            242       7

The print method for is_tagged_na run on a tibble gives disinformation:

is_tagged_na(blah)
[1] FALSE FALSE

The print method for print_tagged_na run on a tibble fails:

print_tagged_na(blah)
Error: `x` must be a double vector

The special values are actually there:

is_tagged_na(blah$favoriteNumber)
[1] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE
is_tagged_na(blah$favoriteNumber, "u")
[1] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
print_tagged_na(blah$favoriteNumber)
[1]    42 NA(d) NA(r) NA(u) NA(m) NA(n)    NA   242