xport icon indicating copy to clipboard operation
xport copied to clipboard

Field length

Open DCasoPTS opened this issue 3 years ago • 6 comments

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

DCasoPTS avatar Oct 05 '22 14:10 DCasoPTS

Are you trying to truncate your strings?

selik avatar Oct 05 '22 15:10 selik

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.

selik avatar Oct 05 '22 15:10 selik

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

DCasoPTS avatar Oct 05 '22 15:10 DCasoPTS

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.

selik avatar Oct 05 '22 15:10 selik

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.

DCasoPTS avatar Oct 05 '22 15:10 DCasoPTS

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

selik avatar Oct 05 '22 19:10 selik