You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Is there any plan to support direct writing to sas7bdat files? #205
@hpoettker I have added write_sas7bdat . It would be nice if people could test. Particularly confirming that it can be opened in SAS. Also if suggestions on arguments that would make sense.
Another thing I noticed when updating Readstat is that now for Xport date formats are read with a ending dot (like YYMMDD.), but I think for sas7bdat it is still lacks the dot.
@ofajardo I've had a look so far at the 64bit output, and it looks promising. But there are some rough edges.
I'm currently investigating 2 problems: Compression and formats. I optimistic that we can fix the format issue but less so about the compression issue.
Compression
The SAS data file is not readable with compression enabled. So this option should maybe be removed from the API.
There is also the problem that SAS data files have three compression types: no compression, CHAR and BINARY while ReadStat only supports CHAR (or row) compression at the moment.
If we get CHAR compression working in a way that produces readable SAS data files, we can then consider whether the API should accept a boolean value or rather an enum corresponding to readstat_compress_t. If we don't get it working we should for the moment just omit the compress option.
Formats
There is definitely an issue here. It works well if I write a SAS data file with format YYMMDD but not with YYMMDD10., which produces files that are unreadable with SAS.
I'm optimistic that this can be fixed. I'll investigate and report back.
hi @hpoettker thanks for looking into this!. Regarding compression, I agree, we can just remove the argument until it is usable in the future. If one of the compression works then I would add an argument that accepts all compressions, but fails immediately for those not supported, so that in the future we do not need to change the name or signature of the argument.
For Formats: if you use YYMMDD10 (withouth end dot) does it work? I am asking because when using read_sas7bdat we get YYMMDD10 if I am not mistaken, so it would make sense that for writing it has to be also without the dot. The not so nice thing here is that for XPORT the latest changes include the end dot for both reading and writing, so now sas7bdat and xport behave differently.
One problem is that the reader does not produce the expected format names (including the final .).
The other problem is that for a format like DOLLAR12.2 the information in the data file is split into DOLLAR, 12, and 2 and needs to be written into different places. But ReadStat currently just writes DOLLAR12.2 where only DOLLAR is expected.
@ofajardo Your intuition about the difference between YYMMDD10 and YYMMDD10. was exactly right.
If WizardMac/ReadStat#419 is merged, the write API will accept both and the reader will return YYMMDD10. in both cases.
Based on my current testing, the change is good to go if the reference PR is merged, and we keep to 64bit uncompressed files, which would mean removing the is_64bit and the compress parameter from write_sas7bdat.
I plan to have a look at both compression and 32bit files but I won't make any promises about it. I would declare the two topics out of scope of this issue and open up a new one for them.
Sounds good ... excellent actually, this feature has being waited for years thanks!
I will update Resdstat sources as soon as they merge your PR and remove the compression and 32 bits arguments here. Let me know in case you have more suggestions.
My bug list regarding write_sas7bdat is now empty, and it should be possible to add a compression argument to write_sas7bdat.
I've tested the bug fixes so far mostly individually. I'll try to do some integrated testing of write_sas7bdat in the upcoming week and let you know whether I've found anything else.
hi @hpoettker, thanks a lot! I have updated the Readstat sources. I also added an argument row_compress for write_sas7bdat. I am mimicking what I did for write_sav where I have row_compress for row compression and compress for binary compression. The other option would be to have a single argument with and make it fail if the users try to do binary compression.
great news! Is there any other important feature/fix coming soon to Readstat from your side that would be good to get in before doing a release? Otherwise I would do a release soon.
I've opened one more pull request. But it's about a separate niche issue and currently waiting for feedback from the person who raised the issue some time ago. I wouldn't wait for that to be merged.
I have no other things currently planned for ReadStat. So a release would be great!
Solving this issue was really a collaborative effort. I only built on the foundations that were laid by @evanmiller by his great work on ReadStat overall and WizardMac/ReadStat@2d942c3 in particular, and by @hadley with WizardMac/ReadStat#398, which cleared the last road block.
Activity
ofajardo commentedon Jul 25, 2022
Readstat does write sas7bdat files, but those files cannot be opened in SAS. WizardMac/ReadStat#98
Until that issue is solved, there will be no supoort for this feature in pyreadstat
hpoettker commentedon Oct 20, 2026
I think this issue can be re-opened.
This PR in ReadStat seems to have achieved the last step: WizardMac/ReadStat#398
I've round-tripped some SAS sample files with ReadStat. And it's looking very good!
ofajardo commentedon Oct 20, 2026
Awesome news! Thanks a lot for bringing this up!
ofajardo commentedon Oct 25, 2026
@hpoettker I have added write_sas7bdat . It would be nice if people could test. Particularly confirming that it can be opened in SAS. Also if suggestions on arguments that would make sense.
Another thing I noticed when updating Readstat is that now for Xport date formats are read with a ending dot (like YYMMDD.), but I think for sas7bdat it is still lacks the dot.
hpoettker commentedon Oct 26, 2026
@ofajardo I've had a look so far at the 64bit output, and it looks promising. But there are some rough edges.
I'm currently investigating 2 problems: Compression and formats. I optimistic that we can fix the format issue but less so about the compression issue.
Compression
The SAS data file is not readable with compression enabled. So this option should maybe be removed from the API.
There is also the problem that SAS data files have three compression types: no compression,
CHARandBINARYwhile ReadStat only supportsCHAR(or row) compression at the moment.If we get
CHARcompression working in a way that produces readable SAS data files, we can then consider whether the API should accept a boolean value or rather an enum corresponding toreadstat_compress_t. If we don't get it working we should for the moment just omit thecompressoption.Formats
There is definitely an issue here. It works well if I write a SAS data file with format
YYMMDDbut not withYYMMDD10., which produces files that are unreadable with SAS.I'm optimistic that this can be fixed. I'll investigate and report back.
ofajardo commentedon Oct 26, 2026
hi @hpoettker thanks for looking into this!. Regarding compression, I agree, we can just remove the argument until it is usable in the future. If one of the compression works then I would add an argument that accepts all compressions, but fails immediately for those not supported, so that in the future we do not need to change the name or signature of the argument.
For Formats: if you use YYMMDD10 (withouth end dot) does it work? I am asking because when using read_sas7bdat we get YYMMDD10 if I am not mistaken, so it would make sense that for writing it has to be also without the dot. The not so nice thing here is that for XPORT the latest changes include the end dot for both reading and writing, so now sas7bdat and xport behave differently.
hpoettker commentedon Oct 26, 2026
There are two problems with the formats.
One problem is that the reader does not produce the expected format names (including the final .).
The other problem is that for a format like
DOLLAR12.2the information in the data file is split intoDOLLAR,12, and2and needs to be written into different places. But ReadStat currently just writesDOLLAR12.2where onlyDOLLARis expected.Expect some PRs in ReadStat soon.
ofajardo commentedon Oct 26, 2026
Ok cool, please let me know here when ready so that I refresh the Resdstat sources on dev
hpoettker commentedon Oct 26, 2026
@ofajardo Your intuition about the difference between
YYMMDD10andYYMMDD10.was exactly right.If WizardMac/ReadStat#419 is merged, the write API will accept both and the reader will return
YYMMDD10.in both cases.Based on my current testing, the change is good to go if the reference PR is merged, and we keep to 64bit uncompressed files, which would mean removing the
is_64bitand thecompressparameter fromwrite_sas7bdat.I plan to have a look at both compression and 32bit files but I won't make any promises about it. I would declare the two topics out of scope of this issue and open up a new one for them.
ofajardo commentedon Oct 27, 2026
Sounds good ... excellent actually, this feature has being waited for years thanks!
I will update Resdstat sources as soon as they merge your PR and remove the compression and 32 bits arguments here. Let me know in case you have more suggestions.
5 remaining items
hpoettker commentedon Nov 1, 2026
The PRs for the row compression WizardMac/ReadStat#423 and the alignment of format names between sas7bdat and sas7bcat WizardMac/ReadStat#424 are now merged in ReadStat.
My bug list regarding
write_sas7bdatis now empty, and it should be possible to add a compression argument towrite_sas7bdat.I've tested the bug fixes so far mostly individually. I'll try to do some integrated testing of
write_sas7bdatin the upcoming week and let you know whether I've found anything else.ofajardo commentedon Nov 2, 2026
hi @hpoettker, thanks a lot! I have updated the Readstat sources. I also added an argument row_compress for write_sas7bdat. I am mimicking what I did for write_sav where I have row_compress for row compression and compress for binary compression. The other option would be to have a single argument with and make it fail if the users try to do binary compression.
hpoettker commentedon Nov 10, 2026
I had previously only tested with small files and now switched to larger files for the final testing. That turned out to be quite productive. 😄
We need a lot more changes in ReadStat: WizardMac/ReadStat#432
I'll let you know when the PR is merged. But after that the write feature should be good to go.
hpoettker commentedon Nov 10, 2026
WizardMac/ReadStat#432 is now merged. 🚀
ofajardo commentedon Nov 11, 2026
hi @hpoettker thanks a lot for the thorough testing and PRs to Readstat! the most recent changes from Readstat are now in dev!
hpoettker commentedon Nov 11, 2026
Thanks for updating dev!
We might get to revisit the API topic around row (RLE) and binary (RDC) compression. 😄
I've opened WizardMac/ReadStat#433 to add the binary compression to the writer.
hpoettker commentedon Nov 11, 2026
WizardMac/ReadStat#433 is now merged!
Compression is no longer a binary issue when writing sas7bdat files. 😄
ofajardo commentedon Nov 12, 2026
Dev is updated with the latest Resdstat sources and binary compression is exposed as an argument (compress)
hpoettker commentedon Nov 12, 2026
Thanks! I tested the latest commit, and produced my sample files. SAS is happy with them!
I'd say that the issue can be closed. 🚀
ofajardo commentedon Nov 13, 2026
great news! Is there any other important feature/fix coming soon to Readstat from your side that would be good to get in before doing a release? Otherwise I would do a release soon.
hpoettker commentedon Nov 13, 2026
I've opened one more pull request. But it's about a separate niche issue and currently waiting for feedback from the person who raised the issue some time ago. I wouldn't wait for that to be merged.
I have no other things currently planned for ReadStat. So a release would be great!
ofajardo commentedon Nov 14, 2026
Released! Thanks a lot for solving this issue hanging for years!
hpoettker commentedon Nov 14, 2026
Thanks for the release!
Solving this issue was really a collaborative effort. I only built on the foundations that were laid by @evanmiller by his great work on ReadStat overall and WizardMac/ReadStat@2d942c3 in particular, and by @hadley with WizardMac/ReadStat#398, which cleared the last road block.