Skip to content

read_sas fails to import sas character missing values as r missing values #775

@rogerjdeangelis

Description

@rogerjdeangelis

Greate Package.

Issue with: Converting imported sas character missings to r missing values using haven package

CONTENTS

 1 ISSUE (R DOES NOT RCOGNIZE SAS CHARACTER MISSING VALUES)
 2 CORRECTION (using dplyr language)

github (for a beeter view)
https://tinyurl.com/5cxkxfz2
https://github.com/rogerjdeangelis/utl-converting-imported-sas-character-missings-to-r-missing-values-using-haven-package

R version 4.4.1 (2024-06-14 ucrt) -- "Race for Your Life"
Copyright (C) 2024 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64

I tried to paste the issue in this comment box, but it mangled the text. If interested see github

INPUT
=====

CHR ISMISSING

a 0
1
b 0
1

options validvarname=upcase;
libname sd1 "d:/sd1";
data sd1.have ;
input chr $1.;
ismissing=missing(chr);
cards4;
a
.
b
.
;;;;
run;quit;

 PROCESS
 =======

1 ISSUE

%utl_rbeginx;
parmcards4;
library(haven)
library(dplyr)
have<-read_sas("d:/sd1/have.sas7bdat")
have$R_MISSING<-is.na(have$CHR)
print(have)
str(have)
;;;;
%utl_rendx;


2 CORRECTION

%utl_rbeginx;
parmcards4;
library(haven)
library(dplyr)
have<-read_sas("d:/sd1/have.sas7bdat")
%>% mutate(across(where(is.character)
,~na_if(.,"")))
have$R_MISSING<-is.na(have$CHR)
print(have)
str(have)
;;;;
%utl_rendx;

    OUTPUT
    =======

R DOES NOT HONOR SAS MISSING

    SAS

CHR ISMISSING R_MISSING
========= =========
1 "a" 0 FALSE
2 "" 1 FALSE WRONG
3 "b" 0 FALSE
4 "" 1 FALSE WRONG


   SAS

CHR ISMISSING R_MISSING
========= =========
a 0 FALSE
1 TRUE CORRECTED
b 0 FALSE
1 TRUE CORRECTED

RUNNING IN SAS GUI

LOG (DOSE RUN IN R GUI_

have<-read_sas("d:/sd1/have.sas7bdat")
have$R_MISSING<-is.na(have$CHR)
print(have)

A tibble: 4 × 3

CHR ISMISSING R_MISSING

1 "a" 0 FALSE
2 "" 1 FALSE
3 "b" 0 FALSE
4 "" 1 FALSE

str(have)
tibble [4 × 3] (S3: tbl_df/tbl/data.frame)
$ CHR : chr [1:4] "a" "" "b" ""
$ ISMISSING: num [1:4] 0 1 0 1
$ R_MISSING: logi [1:4] FALSE FALSE FALSE FALSE

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions