Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
rogerjdeangelis opened this issue Apr 19, 2025 · 0 comments

Comments

@rogerjdeangelis
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant