Differences between revisions 8 and 18 (spanning 10 versions)
Revision 8 as of 2020-07-04 14:40:13
Size: 3523
Comment:
Revision 18 as of 2023-06-13 05:00:25
Size: 2443
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
## page was renamed from SPSSFormats
= SPSS Formats =
SPSS supports numeric variables and strings, with conversion options and some automatic conversion functionality. There are several formats on top of numerics that impact visualization, not storage.
= SPSS Data Types =

SPSS exposes numeric and string data types.

Certain specialized forms of data are handled by formats that impact visualization and export, not storage. The primary example of this is date and time data.
Line 11: Line 13:
== Datetime == == Numeric Data ==
Line 13: Line 15:
=== A warning about data input === Numeric data is stored as double-precision floating point.
Line 15: Line 17:
SPSS tries to be clever about reading data.

Under the format `TIME`, all of these are read in as `"01:02"`, even though the format is a minimum of 5-wide:
 * `"1:2"`
 * `"01 2"`
 * `"01:02"`

Under the format `DATE`, all of these are read in as `"28-OCT-90"`:
 * `"28-OCT-90"`
 * `"28/10/1990"`
 * `"28.OCT.90"`
 * `"28 October, 1990"`



=== Datetime formats ===

||'''Format''' ||'''Appears as...''' ||'''Note''' ||
||DATETIME20 ||`dd-MMM-yyyy hh:mm:ss` ||`MMM` is as JAN, not 001||
||DATETIME18 ||`dd-MMM-yyyy hh:mm` ||`MMM` is as JAN, not 001||
||DATE11 ||`dd-MMM-yyyy` ||`MMM` is as JAN, not 001||
||DATE7 ||`dd-MMM-yy` ||`MMM` is as JAN, not 001||
||TIME8 ||`hh:mm:ss` || ||
||TIME5 ||`hh:mm` || ||
||ADATE10 ||`mm/dd/yyyy` || ||
||ADATE8 ||`mm/dd/yy` || ||
||EDATE10 ||`dd.mm.yyyy` || ||
||EDATE8 ||`dd.mm.yy` || ||
Generally, numeric data uses either a numeric format (`Fw.d` where `w` is the field width and `d` is the number of visible decimal places) or a restricted numeric format (`Nw`) for displaying data. See [[SPSS/DataFormats#Print_Formats|here]] for further information. The default format, which will be applied to any implicitly declared variables, is `F8.2`.
Line 47: Line 22:
=== Parse Timestamps === === Date and Time Data ===
Line 49: Line 24:
A macro for timestamp conversion, from `dd/mm/yyyy hh:mm` to SPSS `DATETIME20`: Dates are stored as the number of seconds from midnight, October 14, 1582 to midnight on the specified date. Generally the `DATE10` format is used for displaying data.
Line 51: Line 26:
{{{
define !str2datetime (in=!tokens(1) / out=!cmdend)
  numeric !out (DATETIME20).
  compute !out =
   date.mdy(
    number(char.substr(!in,1,2), f2),
    number(char.substr(!in,4,2), f2),
    number(char.substr(!in,7,4), f4)
   )
   + time.hms(
    number(char.substr(!in,12,2), f2),
    number(char.substr(!in,15,2), f2),
    0
   )
  .
!enddefine.
Datetimes are stored as the number of seconds to the specified time on the specified date. Generally the `DATETIME20` format is used for displaying data.
Line 68: Line 28:
!str2datetime in=mytimestampvar out=mydatetimevar.
}}}
Times are stored as the number of seconds. This ''can'' be imagined as the number of seconds from midnight to the specified time, but that is not a ''necessary'' construct. Generally the `TIME8` format is used for displaying data.
Line 71: Line 30:
Keep in mind that 1 day = 60 (seconds) * 60 (minutes) * 24 (hours) = 86400 seconds.
Line 72: Line 32:
For more information about date and time formats, see [[SPSS/DataFormats#Print_Formats|here]].
Line 73: Line 34:
=== Create Timestamps ===

This is unfortunately long-winded.

{{{
data list free / a.
begin data.
1
end data.
dataset name TIMESTAMP.
execute.

string tmp_date (A11) datetime (A20).
compute tmp_date = $date.
alter type tmp_date (date11).
compute tmp_time = $time.

* Built-in formats.
compute datetime=string(tmp_date, ADATE10).
execute.

* Manual formats.
compute datetime=concat(
  char.substr(string(xdate.year(tmp_date),N4),3,2),
  string(xdate.month(tmp_date),N2),
  string(xdate.mday(tmp_date),N2),
  "_",
  string(xdate.hour(tmp_time),N2),
  string(xdate.minute(tmp_time),N2),
).
execute.

do if $casenum=1.
  write outfile 'timestamp.sps' /1 "define !timestamp() " datetime " !enddefine.".
end if.
dataset close TIMESTAMP.
execute.

insert file='timestamp.sps'.
execute.
}}}
See [[SPSS/DatetimeFunctions|here]] for the built-in library of date and time functions.
Line 119: Line 40:
== Numerics == == String Data ==
Line 121: Line 42:
Formats only truncate the displayed value, or the value translated into data files. SPSS always retains data to original precision. String data is stored at a fixed length.
Line 123: Line 44:
Given 123.45... A string variable can only be explicitly declared, as by the [[SPSS/String|STRING]] command.
Line 125: Line 46:
||'''Format''' ||'''Using...''' ||'''Appears as...''' ||
||Fw ||F8 ||123 ||
||Fw.d ||F8.1 ||123.4 ||
||Nw ||N8 ||00000123 ||
||Nw.d ||N8.1 ||000123.4 ||
Generally, string data uses the string format (`Aw` where `w` is the field width) for displaying data. See [[SPSS/DataFormats#Print_Formats|here]] for further information.
Line 131: Line 48:
---- See [[SPSS/StringFunctions|here]] for the built-in library of string functions.
Line 135: Line 52:
== Strings == === String Literals ===
Line 137: Line 54:
String variables are only formatted to a length. The format is specified as `AN` where `N` is the width. String literals are declared by wrapping a string value in quotes, either single (`'`) or double (`"`).

Quote marks within strings can be handled in one of two ways: either use the opposite quote mark to wrap the string, or escape the quote mark by doubling it.

{{{
variable label var1 "Say ""Hello!""".
variable label var2 'Don''t say "Goodbye!"'.
}}}

SPSS Data Types

SPSS exposes numeric and string data types.

Certain specialized forms of data are handled by formats that impact visualization and export, not storage. The primary example of this is date and time data.


Numeric Data

Numeric data is stored as double-precision floating point.

Generally, numeric data uses either a numeric format (Fw.d where w is the field width and d is the number of visible decimal places) or a restricted numeric format (Nw) for displaying data. See here for further information. The default format, which will be applied to any implicitly declared variables, is F8.2.

Date and Time Data

Dates are stored as the number of seconds from midnight, October 14, 1582 to midnight on the specified date. Generally the DATE10 format is used for displaying data.

Datetimes are stored as the number of seconds to the specified time on the specified date. Generally the DATETIME20 format is used for displaying data.

Times are stored as the number of seconds. This can be imagined as the number of seconds from midnight to the specified time, but that is not a necessary construct. Generally the TIME8 format is used for displaying data.

Keep in mind that 1 day = 60 (seconds) * 60 (minutes) * 24 (hours) = 86400 seconds.

For more information about date and time formats, see here.

See here for the built-in library of date and time functions.


String Data

String data is stored at a fixed length.

A string variable can only be explicitly declared, as by the STRING command.

Generally, string data uses the string format (Aw where w is the field width) for displaying data. See here for further information.

See here for the built-in library of string functions.

String Literals

String literals are declared by wrapping a string value in quotes, either single (') or double (").

Quote marks within strings can be handled in one of two ways: either use the opposite quote mark to wrap the string, or escape the quote mark by doubling it.

variable label var1 "Say ""Hello!""".
variable label var2 'Don''t say "Goodbye!"'.


CategoryRicottone

SPSS/DataTypes (last edited 2023-06-13 05:00:25 by DominicRicottone)