Skip to main content

Stata DTA 文件处理器

本讨论重点关注 Stata DTA 编解码器。为了将 SheetJS 集成到 Stata 扩展中,有一个 专用演示

¥This discussion focuses on the Stata DTA codec. For integrating SheetJS in a Stata extension, there is a dedicated demo.

Stata 是一个统计软件包。计量经济学家和社会科学家已使用 Stata 进行数据处理和统计分析。许多旧数据集仅在 Stata DTA 数据文件中可用。

¥Stata is a statistical software package. Econometricians and social scientists have used Stata for data processing and statistical analysis. Many legacy datasets are only available in Stata DTA data files.

SheetJS DTA 编解码器使网站和自动化数据管道能够集成 DTA 文件中的数据。

¥The SheetJS DTA codec enables websites and automated data pipelines to integrate data from DTA files.

源代码和项目文档托管在位于 https://git.sheetjs.com/sheetjs/sheetjs/src/branch/master/packages/dta 的 SheetJS git 服务器上

¥Source code and project documentation are hosted on the SheetJS git server at https://git.sheetjs.com/sheetjs/sheetjs/src/branch/master/packages/dta

DTA 支持被认为是实验性的。

优秀的开源软件会随着用户测试和报告而不断成长。如有问题应报告给 问题跟踪器

¥Great open source software grows with user tests and reports. Issues should be reported to the issue tracker.

局限性

按照 Stata 的说法,支持版本 102-105、108、110-115 和 117-121。这对应于 Stata 18 之前所有版本的已知文件版本。

¥In Stata parlance, versions 102-105, 108, 110-115, and 117-121 are supported. This corresponds to the known file versions across all releases through Stata 18.

与电子表格软件的限制一致,提取了前 1048576 个观测值和 16384 个变量。

¥Consistent with spreadsheet software limitations, the first 1048576 observations and 16384 variables are extracted.

不处理别名变量(DTA 版本 120-121 中支持)。

¥Alias variables (supported in DTA versions 120-121) are not processed.

在线演示

¥Live Demo

此演示获取 示例 DTA 文件,使用 SheetJS DTA 编解码器解析数据,并使用 sheet_to_html 方法 [^1] 在 HTML 表中显示数据。

¥This demo fetches a sample DTA file, parses the data using the SheetJS DTA Codec and displays the data in an HTML table using the sheet_to_html method[^1].

文件输入元素可用于解析计算机上的文件。

¥The file input element can be used to parse a file on your computer.

所有工作都在网络浏览器中完成!数据永远不会离开你的机器!

¥All work is performed in the web browser! Data never leaves your machine!

如果你在测试中发现任何意外结果或错误,请在 问题跟踪器 报告问题。

¥If you find any unexpected results or errors in testing, please report an issue at the issue tracker.

Result
Loading...
Live Editor
function SheetJSDTA() {
  const [__html, setHTML] = React.useState("");
  const [text, setText] = React.useState("");

  const process = (ab) => {
    try {
      /* Initial Setup */
      if(typeof DTA == "undefined") return setText("ERROR: Reload this page!");
      DTA.set_utils(XLSX.utils);

      /* Parse DTA */
      const wb = DTA.parse(new Uint8Array(ab));

      /* Generate HTML */
      setHTML(XLSX.utils.sheet_to_html(wb.Sheets[wb.SheetNames[0]]));
      setText("");
    } catch(e) { setText("ERROR: " + (e && e.message || e)); }
  };

  /* Fetch test file on load */
  React.useEffect(() => { (async() => {
    process(await (await fetch("/dta/pres.dta")).arrayBuffer());
  })(); }, []);

  const good = { backgroundColor: "#C6EFCE", color: "#006100" };
  const bad = { backgroundColor: "#FFC7CE", color: "#9C0006" };
  return ( <>
    <input type="file" onChange={async(e) => {
      process(await e.target.files[0].arrayBuffer());
    }}/>
    {text && <code style={/ERROR/.test(text)?bad:good}>{text}</code>}
    <div dangerouslySetInnerHTML={{ __html }}/>
  </> );
}

[^1]: 见 sheet_to_html 于 "实用工具"

¥See sheet_to_html in "Utilities"