Setting Numeric Field Widths (eg spssSetVarWriteFormat?)

Jul 1, 2012 at 5:10 PM


There's an earlier thread on this topic but it was not resolved so I am asking again. I have two SAV files, each with a different set of variables (largely categorical variables).

I am using the wrapper/SPSIO32.dll library to creat a new SAV file which merges both the source files, copying the variable definitions directly from the definitions in the two source files:


if (s is SpssNumericVariable) SpssNumericVariable t = new SpssNumericVariable();

When I open open the new SAV file, the variables are all Width=8, Decimal=0 even if the source data is Width=1, Decimal=0. I have tried adjusting the variable's column width (eg t.ColumnWidth = 2) without changing the data width of the variable in the new file.

The Measurement Level is inherited from the source SAV, but I was wondering if forcing the variable to be Nominal or Ordinal would reduce the size of the variable.

Alternatively, I see that in the IBM Input/Output PDF that there are two methods (spssGetVarWriteFormat and spssSetVarWriteFormat) that sound suspiciously like what I need - do they set the variable widths in a SAV file? If so, are these two methods available through the wrapper and how are they called?

In summary, does anyone know how can I change the data width of a variable using the wrapper and SPSSIO32.dll?



t.Name = s.Name; t.Alignment = s.Alignment; t.ColumnWidth = s.ColumnWidth;

t.Label = s.Label; t.MeasurementLevel = s.MeasurementLevel;}

Feb 15, 2013 at 9:04 AM
Hi sydneyfixie,

Could you resolve your problem?
I've almost got the same prolem like you. I'd like to change the width and the decimal of a datevariable to 20 and 0,
but it's always 28 (width) and 4 (decimal).
If I create a stringvariable, I can change the width only.

I use the same library like you.

Thanks for help.
Feb 15, 2013 at 9:48 AM

I'm not sure it's what you're after but it worked for me. I ended up using the the PrintWidth and PrintDecimal members available after casting the Spss variable into type SpssNumericVariable.

It's not clear in the documentation, but I think the total width must include the decimal point so a print width/decimal spec of (5.3) would represent a number in the format x.nnn. The following code is an extract (so it doesn't have all the definitions) of how I ended up making sure that all my integer variables had no decimal places and the all values (integer and float) were set to the width needed to accomodate one decimal place.

foreach (SpssVariable s in sourceDoc.Variables)
            //snip - setting biggestVal (type=double) to largest value found in all cases for variable s

            if (s is SpssNumericVariable)
                  SpssNumericVariable u = (SpssNumericVariable)s;

                    //set default number of decimal places
                    u.PrintDecimal = 3;
                    if (integersOnly) u.PrintDecimal = 0;                            //could use if(){statement1} else {statement2}

                    u.PrintWidth = 12;
                    if (biggestVal < 10.0) { u.PrintWidth = u.PrintDecimal + 2; }
                    else if (biggestVal < 100.0) {u.PrintWidth = u.PrintDecimal + 3; }
                    else if (biggestVal < 1000.0) {u.PrintWidth = u.PrintDecimal + 4; }
                    else if (biggestVal < 10000.0) {u.PrintWidth = u.PrintDecimal + 5; }
                    else if (biggestVal < 100000.0) {u.PrintWidth = u.PrintDecimal + 6;} 
                    else if (biggestVal < 1000000.0) { u.PrintWidth = u.PrintDecimal + 7; }
                AddVariable(u,destDoc);                 //custom method to handle special conditions 

Hope it helps.