docs.sheetjs.com/docz/docs/12-constellation/21-dta.md
2023-11-13 06:23:12 -05:00

2.7 KiB

title sidebar_label hide_table_of_contents
Stata DTA File Processor Stata DTA Codec true
<head> </head>

:::info pass

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

:::

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.

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

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

:::caution DTA support is considered experimental.

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

:::

Live Demo

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 method1.

:::tip pass

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.

:::

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

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

      /* Parse DTA */
      const wb = DTA.parse(u8);

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

  React.useEffect(() => { (async() => {
    /* Fetch file */
    process(new Uint8Array(await (await fetch("/dta/pres.dta")).arrayBuffer()));
  })(); }, []);
  const goodstyle = { backgroundColor: "#C6EFCE", color: "#006100" };
  const badstyle = { backgroundColor: "#FFC7CE", color: "#9C0006" };

  return ( <>
    {/* Import Button */}
    <input type="file" onChange={async(e) => {
      process(new Uint8Array(await e.target.files[0].arrayBuffer()));
    }}/>
    {text && <code style={/ERROR/.test(text)?badstyle:goodstyle}>{text}</code>}
    <div dangerouslySetInnerHTML={{ __html }}/>
  </> );
}