selik / xport

Python reader and writer for SAS XPORT data transport files.
MIT License
49 stars 24 forks source link

Field length #98

Open DCasoPTS opened 1 year ago

DCasoPTS commented 1 year ago

I'm having an issue with the length, I want to set it to the number I want but it is always fixed to the max length of the values inside the field. I'm using a similar example to this:

import pandas as pd
import xport
import xport.v56

df = pd.DataFrame({
    'alpha': [10, 20, 30],
    'beta': ['x', 'y', 'z'],
})

...  # Analysis work ...

ds = xport.Dataset(df, name='DATA', label='Wonderful data')

# SAS variable names are limited to 8 characters.  As with Pandas
# dataframes, you must change the name on the dataset rather than
# the column directly.
ds = ds.rename(columns={k: k.upper()[:8] for k in ds})

# Other SAS metadata can be set on the columns themselves.
for k, v in ds.items():
    v.label = k.title()
    if v.dtype == 'object':
        v.format = '$CHAR20.'
    else:
        v.format = '10.2'

   v.length = 11

# Libraries can have multiple datasets.
library = xport.Library({'DATA': ds})

with open('example.xpt', 'wb') as f:
    xport.v56.dump(library, f)

v.length is doing nothing as I see in the result

selik commented 1 year ago

Are you trying to truncate your strings?

selik commented 1 year ago

If you want to truncate strings, do so in Python, first. If you're trying to set a longer FORMAT length, it's probably a bug.

DCasoPTS commented 1 year ago

Are you trying to truncate your strings?

Not at all, I just want to handle the field length so that if for example I put the values ["ABC", "ABCD", "AB"] instead of being length = 4 (max length in the values), I can put a higher one. I know it's possible in the XPT files because I have seen examples where it happens (not created by Python).

selik commented 1 year ago

If it's not working, that's a bug in thexport module. It used to work. I'm not sure when I'll have time to look at it. Happy to have the help if you want to poke around.

DCasoPTS commented 1 year ago

If it's not working, that's a bug in thexport module. It used to work. I'm not sure when I'll have time to look at it. Happy to have the help if you want to poke around.

Going inside the package, I have found the issue, I was using v.length and it must be v.width I think it could help for others to include this in the documentation.

Thank you for your help, and for your quickly response.

selik commented 1 year ago

I'd be happy to receive a pull request fixing the documentation.