【问题标题】:failed to parse using R package haven read_sas使用 R 包 Have read_sas 解析失败
【发布时间】:2021-05-17 05:40:42
【问题描述】:

我正在使用 (.sas7bcat ) 文件到 R 使用 Have 的包命令“read_sas”。在自己的工作中导入 sas 文件就好了。但是,当我尝试使用以下代码导入格式文件时,导入格式文件 sas7bcat 我收到以下错误消息:

pri <- read_sas (path = "Datasets/pri.sas7bdat",
                 path.cat = "Datasets/formats.sas7bcat")

df_parse_sas_file(spec_data, spec_cat, encoding = encoding, catalog_encoding = catalog_encoding, 解析失败.../formats.sas7bcat:文件无效,或文件具有不受支持的功能。

我不知道问题出在哪里。我在 SAS 文件中有近 250 个变量,它们都有标签和格式(在 SAS 术语中)。

我已阅读有关堆栈溢出问题的所有内容。我认为问题可能在于格式文件本身,但我无法弄清楚问题所在。

我正在使用最新版本的 Haven 2.3.1 : 和 R v. 4.0.3 (2020-10-10),在 Windows 10 上使用 RStudio v.1.3.1093。

This link has the SAS code for the formats

This link has the .sas7bcat library

【问题讨论】:

  • 您的 R studio 会话使用的是哪个版本的 R 本身?您提供的数字仅用于 R studio 界面,而不是 R studio 用于执行 R 代码的实际 R 应用程序。
  • v.4.0.3,我会更新问题
  • 您知道其中的格式是图片格式还是多级格式或其他复杂的SAS格式?
  • @Tom 我已经检查过了,但我不太确定。我认为没有。但是,我已将格式的 SAS 代码和 sas7bcat 库附加到这个问题。
  • 我在 Mundhöhle 的文本中看到了一些非 7 位的 ASCII 码。创建格式目录时使用了什么编码?

标签: r sas r-haven


【解决方案1】:
sessionInfo()
#> R version 4.0.4 (2021-02-15)
#> Platform: i386-w64-mingw32/i386 (32-bit)
#> Running under: Windows 10 x64 (build 17763)
#> 
#> other attached packages:
#> [1] haven_2.3.1

我在 SAS 中创建了一个数据库class

data temp.class;
  set sashelp.class;
  if sex = "M" then sexnum = 1; else sexnum = 2;
  format sexnum sexfmt.;
run;

请注意,您的语法不适用于我:

haven::read_sas (path = "d:/temp/class.sas7bdat", 
                 path.cat = "d:/temp/formats.sas7bcat")
#> Error in read_sas(path = "d:/temp/class.sas7bdat", path.cat = "d:/temp/formats.sas7bcat") : 
#>   unused arguments (path = "d:/temp/class.sas7bdat", path.cat = "d:/temp/formats.sas7bcat")

然后我使用了以下语法,得到了和你一样的信息:

haven::read_sas (data_file = "d:/temp/class.sas7bdat", 
                 catalog_file = "d:/temp/formats.sas7bcat")
#> Error in df_parse_sas_file(spec_data, spec_cat, encoding = encoding, catalog_encoding = catalog_encoding,  : 
#>   Failed to parse D:/temp/formats.sas7bcat: Invalid file, or file has unsupported features.

或者如果language for message 设置为fr

haven::read_sas (data_file = "d:/temp/class.sas7bdat", 
                 catalog_file = "d:/temp/formats.sas7bcat")
#> Error in df_parse_sas_file(spec_data, spec_cat, encoding = encoding, catalog_encoding = catalog_encoding,  : 
#>   Failed to parse D:/temp/formats.sas7bcat: Unable to allocate memory.

这是由于rekof 格式没有价值。删除它没关系:

haven::read_sas (data_file = "d:/temp/class.sas7bdat", 
                 catalog_file = "d:/temp/formats.sas7bcat")
#> # A tibble: 19 x 6
#>    Name    Sex     Age Height Weight     sexnum
#>    <chr>   <chr> <dbl>  <dbl>  <dbl>  <dbl+lbl>
#>  1 Alfred  M        14   69    112.  1 [Male]  
#>  2 Alice   F        13   56.5   84   2 [Female]
#>  3 Barbara F        13   65.3   98   2 [Female]
#>  4 Carol   F        14   62.8  102.  2 [Female]
#>  5 Henry   M        14   63.5  102.  1 [Male]  
#>  6 James   M        12   57.3   83   1 [Male]  
#>  7 Jane    F        12   59.8   84.5 2 [Female]
#>  8 Janet   F        15   62.5  112.  2 [Female]
#>  9 Jeffrey M        13   62.5   84   1 [Male]  
#> 10 John    M        12   59     99.5 1 [Male]  
#> 11 Joyce   F        11   51.3   50.5 2 [Female]
#> 12 Judy    F        14   64.3   90   2 [Female]
#> 13 Louise  F        12   56.3   77   2 [Female]
#> 14 Mary    F        15   66.5  112   2 [Female]
#> 15 Philip  M        16   72    150   1 [Male]  
#> 16 Robert  M        12   64.8  128   1 [Male]  
#> 17 Ronald  M        15   67    133   1 [Male]  
#> 18 Thomas  M        11   57.5   85   1 [Male]  
#> 19 William M        15   66.5  112   1 [Male]  

要删除rekof 格式,您可以:

  • 删除格式目录
  • 将该行注释为/* value rekof ; */
  • 重新生成格式目录(执行proc format

或使用以下 SAS 代码:

proc catalog catalog=lcoc.formats; 
  delete rekof (et=format);
run;

产生错误的最小示例

在 SAS 中

libname temp "d:/temp";
option fmtsearch=(temp);

proc format lib=temp;
  value test;
run;

data temp.class;
  set sashelp.class;
run;

在R中

haven::read_sas (data_file = "d:/temp/class.sas7bdat", 
                 catalog_file = "d:/temp/formats.sas7bcat")
#> Error in df_parse_sas_file(spec_data, spec_cat, encoding = encoding, catalog_encoding = catalog_encoding,  : 
#>   Failed to parse D:/temp/formats.sas7bcat: Unable to allocate memory.

问候,

【讨论】:

  • 如何去除rekof的格式?它到底是什么?
  • 格式目录中有do nothing格式是否总是失败?还是必须在要转换的数据集中使用格式才能触发错误?
  • 我更新了我的消息来解释如何删除格式 rekof。 @Tom:在要转换的数据集中不能使用该格式来触发错误。如果什么都不做格式在格式目录中,它总是会失败。我添加了一个产生错误的最小示例。
猜你喜欢
  • 2022-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多